# nanohtml

> HTML template strings for the Browser with support for Server Side Rendering in Node.

Latest version **1.10.0** (published 2022-04-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install nanohtml
pnpm add nanohtml
yarn add nanohtml
bun add nanohtml
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.10.0 |
| Published | 2022-04-21 |
| First published | 2017-12-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 77.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 688 |
| Maintainers | benlyn, s3ththompson, substack, yoshuawuyts, bret, lrlna, yerkopalma, juliangruber, timwis, ahdinosaur, toddself, sethvincent, maxogden, shama, freeman-lab, feross, emilbayes, colingourlay, jameskyburz, almost, slaskis, ungoldman, graforlock, tornqvist, amongiants, goto-bus-stop, mafintosh |
| Keywords | choo, node, html, template-string, strings, template, string, lit-html, yo-yo, choo.js, es6, HTML, DOM, diff, render, multi, line, tagged, native, hyperhtml, hyperdom, fast, small, lite, tiny, nano |

## Links

- npm: https://www.npmjs.com/package/nanohtml
- Repository: https://github.com/choojs/nanohtml
- Homepage: https://github.com/choojs/nanohtml#readme
- Issues: https://github.com/choojs/nanohtml/issues
- npm.io page: https://npm.io/package/nanohtml

## Dependencies (11)

- [hyperx](https://npm.io/package/hyperx.md) ^2.5.0
- [through2](https://npm.io/package/through2.md) ^2.0.3
- [nanobench](https://npm.io/package/nanobench.md) ^2.1.0
- [acorn-node](https://npm.io/package/acorn-node.md) ^1.8.2
- [camel-case](https://npm.io/package/camel-case.md) ^3.0.0
- [nanoassert](https://npm.io/package/nanoassert.md) ^1.1.0
- [transform-ast](https://npm.io/package/transform-ast.md) ^2.4.0
- [convert-source-map](https://npm.io/package/convert-source-map.md) ^1.5.1
- [is-boolean-attribute](https://npm.io/package/is-boolean-attribute.md) 0.0.1
- [normalize-html-whitespace](https://npm.io/package/normalize-html-whitespace.md) ^0.2.0
- [estree-is-member-expression](https://npm.io/package/estree-is-member-expression.md) ^1.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.10.0 (latest) — 2022-04-21
- 1.9.1 — 2020-02-24
- 1.9.0 — 2019-10-25
- 1.8.1 — 2019-09-04
- 1.8.0 — 2019-08-19
- 1.7.0 — 2019-08-12
- 1.6.3 — 2019-05-31
- 1.6.2 — 2019-05-13
- 1.6.1 — 2019-04-26
- 1.6.0 — 2019-04-25
- 1.5.0 — 2019-03-13
- 1.4.0 — 2018-12-13
- 1.3.1 — 2018-12-06
- 1.3.0 — 2018-11-16
- 1.2.6 — 2018-10-03
- … 9 more at https://npm.io/package/nanohtml/versions

## README

# nanohtml
[![npm version][2]][3] [![build status][4]][5]
[![downloads][8]][9] [![js-standard-style][10]][11]

HTML template strings for the Browser with support for Server Side
Rendering in Node.

## Installation
```sh
$ npm install nanohtml
```

## Usage
### Browser
```js
var html = require('nanohtml')

var el = html`
  <body>
    <h1>Hello planet</h1>
  </body>
`

document.body.appendChild(el)
```

### Node
Node doesn't have a DOM available. So in order to render HTML we use string
concatenation instead. This has the fun benefit of being quite efficient, which
in turn means it's great for server rendering!

```js
var html = require('nanohtml')

var el = html`
  <body>
    <h1>Hello planet</h1>
  </body>
`

console.log(el.toString())
```

### Node with custom DOM
Modules like [`jsdom`](https://github.com/jsdom/jsdom) implement (parts of)
the DOM in pure JavaScript. If you don't really need the performance of
string concatenation, or use nanohtml components that modify the raw DOM, use
`nanohtml/dom` to give nanohtml a custom Document.

```js
var JSDOM = require('jsdom').JSDOM
var nanohtml = require('nanohtml/dom')
var jsdom = new JSDOM()

var html = nanohtml(jsdom.window.document)
var el = html`
  <body>
    <h1>Hello planet</h1>
  </body>
`
el.appendChild(html`<p>A paragraph</p>`)

el.outerHTML === '<body><h1>Hello planet</h1><p>A paragraph</p></body>'
```

### Interpolating unescaped HTML
By default all content inside template strings is escaped. This is great for
strings, but not ideal if you want to insert HTML that's been returned from
another function (for example: a markdown renderer). Use `nanohtml/raw` for
to interpolate HTML directly.

```js
var raw = require('nanohtml/raw')
var html = require('nanohtml')

var string = '<h1>This a regular string.</h1>'
var el = html`
  <body>
    ${raw(string)}
  </body>
`

document.body.appendChild(el)
```

### Attaching event listeners
```js
var html = require('nanohtml')

var el = html`
  <body>
    <button onclick=${onclick}>
      Click Me
    </button>
  </body>
`

document.body.appendChild(el)

function onclick (e) {
  console.log(`${e.target} was clicked`)
}
```

### Multiple root elements

If you have more than one root element they will be combined with a [DocumentFragment](https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment).

```js
var html = require('nanohtml')

var el = html`
  <li>Chashu</li>
  <li>Nori</li>
`

document.querySelector('ul').appendChild(el)
```

## Static optimizations
Parsing HTML has significant overhead. Being able to parse HTML statically,
ahead of time can speed up rendering to be about twice as fast.

### Browserify

#### From the command line
```sh
$ browserify -t nanohtml index.js > bundle.js
```

#### Programmatically
```js
var browserify = require('browserify')
var nanohtml = require('nanohtml')
var path = require('path')

var b = browserify(path.join(__dirname, 'index.js'))
  .transform(nanohtml)

b.bundle().pipe(process.stdout)
```

#### In package.json
```json
{
  "name": "my-app",
  "private": true,
  "browserify": {
    "transform": [
      "nanohtml"
    ]
  },
  "dependencies": {
    "nanohtml": "^1.0.0"
  }
}
```

## Bundling

### Webpack
At the time of writing there's no Webpack loader yet. We'd love a contribution!

### Babel / Parcel

Add nanohtml to your `.babelrc` config.

Without options:

```js
{
  "plugins": [
    "nanohtml"
  ]
}
```

With options:

```js
{
  "plugins": [
    ["nanohtml", {
      "useImport": true
    }]
  ]
}
```

#### Options

 - `useImport` - Set to true to use `import` statements for injected modules.
    By default, `require` is used.
 - `appendChildModule` - Import path to a module that contains an `appendChild`
    function. Defaults to `"nanohtml/lib/append-child"`.

### Rollup

Use the [@rollup/plugin-commonjs](https://github.com/rollup/plugins/tree/master/packages/commonjs#using-with-rollupplugin-node-resolve) plugin with [@rollup/plugin-node-resolve](https://github.com/rollup/plugins/tree/master/packages/node-resolve). Explicitly import the browser or server entrypoint in your application. E.g.:

```
import html from 'nanohtml/lib/browser';
```

## Attributions
Shout out to [Shama](https://github.com/shama) and
[Shuhei](https://github.com/shuhei) for their contributions to
[Bel](https://github.com/shama/bel),
[yo-yoify](https://github.com/shama/yo-yoify) and
[pelo](https://github.com/shuhei/pelo). This module is based on their work, and
wouldn't have been possible otherwise!

## See Also
- [choojs/nanomorph](https://github.com/choojs/nanomorph)

## License
[MIT](./LICENSE)

[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
[2]: https://img.shields.io/npm/v/nanohtml.svg?style=flat-square
[3]: https://npmjs.org/package/nanohtml
[4]: https://img.shields.io/travis/choojs/nanohtml/master.svg?style=flat-square
[5]: https://travis-ci.org/choojs/nanohtml
[6]: https://img.shields.io/codecov/c/github/choojs/nanohtml/master.svg?style=flat-square
[7]: https://codecov.io/github/choojs/nanohtml
[8]: http://img.shields.io/npm/dt/nanohtml.svg?style=flat-square
[9]: https://npmjs.org/package/nanohtml
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[11]: https://github.com/feross/standard

---
_Source: https://npm.io/package/nanohtml · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
