├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | test 7 | test.js 8 | demo/ 9 | .npmignore 10 | LICENSE.md -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 Matt DesLauriers 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simple-html-index 2 | 3 | [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) 4 | 5 | A tiny through stream that returns a bare-bones HTML5 template with an optional 6 | `` and `` in the head and `<script>` entry-point in the body. 7 | 8 | ## Example 9 | 10 | In `html.js` 11 | 12 | ```js 13 | var html = require('simple-html-index') 14 | 15 | html({ title: 'hello', entry: 'bundle.js' }) 16 | .pipe(process.stdout) 17 | ``` 18 | 19 | Now run `node html.js > index.html` and you would end up with a file that looks like this: (after formatting) 20 | 21 | ```html 22 | <!DOCTYPE html> 23 | <html lang="en"> 24 | <head> 25 | <title>hello 26 | 27 | 28 | 29 | 30 | 31 | 32 | ``` 33 | 34 | ## Usage 35 | 36 | [![NPM](https://nodei.co/npm/simple-html-index.png)](https://www.npmjs.com/package/simple-html-index) 37 | 38 | #### `stream = html([opt])` 39 | 40 | Returns a read stream that writes a bare-bones HTML template, with the 41 | following optional features: 42 | 43 | - `title` whether to include a `` element 44 | - `entry` if specified, will add a `<script src={{entry}}>` element 45 | - `css` if specified will add a `<link rel="stylesheet" href={{css}}>` element 46 | - `favicon` if `true` the `favicon.ico` request [will be suppressed][1] 47 | - `lang` the value of the `lang` attribute in the root `<html>` element, default `'en'` 48 | - `base` if specified will add a `<base href={{base}}>` element 49 | 50 | ## Additional properties 51 | Combine `simple-html-index` with 52 | [`hyperstream`](https://github.com/substack/hyperstream) to add additional 53 | properties to html. An example how to add an extra `<script>` tag to the body 54 | tag: 55 | ```js 56 | const hyperstream = require('hyperstream') 57 | const html = require('simple-html-index') 58 | 59 | const htmls = html({ entry: 'static/bundle.js' }) 60 | const hs = hyperstream({ 61 | body: { _appendHtml: "<script>console.log('extra tags!')</script>" } 62 | }) 63 | 64 | htmls.pipe(hs).pipe(process.stdout) 65 | ``` 66 | 67 | ## License 68 | 69 | MIT, see [LICENSE.md](http://github.com/mattdesl/simple-html-index/blob/master/LICENSE.md) for details. 70 | 71 | [1]: http://stackoverflow.com/a/5568484/1541707 72 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fromString = require('from2-string') 2 | 3 | const favicon = '<link rel="shortcut icon"type="image/x-icon"' + 4 | ' href="data:image/x-icon;,">' 5 | 6 | module.exports = createHtml 7 | 8 | function createHtml (opt) { 9 | opt = opt || {} 10 | return fromString([ 11 | '<!DOCTYPE html>', 12 | '<html lang="' + (opt.lang || 'en') + '" dir="' + (opt.dir || 'ltr') + '">', 13 | '<head>', 14 | opt.title ? ('<title>' + opt.title + '') : '', 15 | '', 16 | opt.base ? ('') : '', 17 | opt.css ? ('') : '', 18 | opt.favicon ? favicon : '', 19 | '', 20 | opt.entry ? ('') : '', 21 | '', 22 | '' 23 | ].join('')) 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple-html-index", 3 | "version": "1.5.0", 4 | "description": "a simple HTML index through stream", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Matt DesLauriers", 9 | "email": "dave.des@gmail.com", 10 | "url": "https://github.com/mattdesl" 11 | }, 12 | "dependencies": { 13 | "from2-string": "^1.1.0" 14 | }, 15 | "devDependencies": { 16 | "concat-stream": "^1.5.0", 17 | "tape": "^4.0.0" 18 | }, 19 | "scripts": { 20 | "test": "node test.js" 21 | }, 22 | "keywords": [ 23 | "through", 24 | "stream", 25 | "index", 26 | "html", 27 | "page", 28 | "default" 29 | ], 30 | "repository": { 31 | "type": "git", 32 | "url": "git://github.com/mattdesl/simple-html-index.git" 33 | }, 34 | "homepage": "https://github.com/mattdesl/simple-html-index", 35 | "bugs": { 36 | "url": "https://github.com/mattdesl/simple-html-index/issues" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var html = require('./') 3 | var concat = require('concat-stream') 4 | 5 | test('should write html', function (t) { 6 | t.plan(8) 7 | run(t, undefined, '') 8 | run(t, { title: 'foo' }, 'foo') 9 | run(t, { title: 'foo', base: '/' }, 'foo') 10 | run(t, { title: 'foo', entry: 'blah.js' }, 'foo') 11 | run(t, { title: 'foo', css: 'bla.css' }, 'foo') 12 | run(t, { title: 'foo', favicon: true }, 'foo') 13 | run(t, { title: 'foo', favicon: true, lang: 'en-US' }, 'foo') 14 | run(t, { title: 'foo', favicon: true, lang: 'en-US', base: '/foo/' }, 'foo') 15 | }) 16 | 17 | function run (t, opt, expected) { 18 | html(opt).pipe(concat(function (body) { 19 | var str = body.toString() 20 | t.equal(str, expected) 21 | })) 22 | } 23 | --------------------------------------------------------------------------------