hast utility to serialize to HTML
npm install hast-util-to-html[![Build][badge-build-image]][badge-build-url]
[![Coverage][badge-coverage-image]][badge-coverage-url]
[![Downloads][badge-downloads-image]][badge-downloads-url]
[![Size][badge-size-image]][badge-size-url]
[hast][github-hast] utility to serialize hast as HTML.
* What is this?
* When should I use this?
* Install
* Use
* API
* [toHtml(tree[, options])](#tohtmltree-options)
* CharacterReferences
* Options
* Quote
* Space
* Syntax
* Types
* Compatibility
* Security
* Related
* Contribute
* License
This package is a utility that turns a hast tree into a string of HTML.
You can use this utility when you want to get the serialized HTML that is
represented by the syntax tree,
either because you’re done with the syntax tree,
or because you’re integrating with another tool that does not support syntax
trees.
This utility has many options to configure how the HTML is serialized.
These options help when building tools that make output pretty (such as
formatters) or ugly (such as minifiers).
The utility [hast-util-from-html][github-hast-util-from-html] does the
inverse of this utility.
It turns HTML into hast.
The rehype plugin [rehype-stringify][github-rehype-stringify] wraps this
utility to also serialize HTML at a higher-level (easier) abstraction.
This package is [ESM only][github-gist-esm].
In Node.js (version 16+),
install with [npm][npmjs-install]:
``sh`
npm install hast-util-to-html
In Deno with [esm.sh][esmsh]:
`js`
import {toHtml} from 'https://esm.sh/hast-util-to-html@9'
In browsers with [esm.sh][esmsh]:
`html`
`js
import {h} from 'hastscript'
import {toHtml} from 'hast-util-to-html'
const tree = h('.alpha', [
'bravo ',
h('b', 'charlie'),
' delta ',
h('a.echo', {download: true}, 'foxtrot')
])
console.log(toHtml(tree))
`
Yields:
`html`bravo charlie delta foxtrot
This package exports the identifier [toHtml][api-to-html].
There is no default export.
Serialize hast as HTML.
###### Parameters
* treeNode
([][github-hast-nodes] or Array)options
— tree to serialize
* Options
([][api-options], optional)
— configuration
###### Returns
Serialized HTML (string).
How to serialize character references (TypeScript type).
##### Fields
###### useNamedReferences
Prefer named character references (&) where possibleboolean
(, default: false).
###### omitOptionalSemicolons
Whether to omit semicolons when possible
(boolean, default: false).
> ⚠️ Note:
> this creates what HTML calls “parse errors” but is otherwise still valid HTML:
> don’t use this except when building a minifier;
> omitting semicolons is possible for certain named and numeric references in
> some cases.
###### useShortestReferences
Prefer the shortest possible reference,
if that results in less bytes
(boolean, default: false).
> ⚠️ Note:
> useNamedReferences can be omitted when using useShortestReferences.
Configuration (TypeScript type).
##### Fields
###### allowDangerousCharacters
Do not encode some characters which cause XSS vulnerabilities in older browsers
(boolean, default: false).
> ⚠️ Danger:
> only set this if you completely trust the content.
###### allowDangerousHtml
Allow raw nodes and insert them as raw HTMLboolean
(, default: false).
When false,Raw nodes are encoded.
> ⚠️ Danger:
> only set this if you completely trust the content.
###### allowParseErrors
Do not encode characters which cause parse errors
(even though they work),
to save bytes
(boolean, default: false).
Not used in the SVG space.
> 👉 Note:
> intentionally creates parse errors in markup
> (how parse errors are handled is well defined,
> so this works but isn’t pretty).
###### bogusComments
Use “bogus comments” instead of comments to save byes: instead of boolean
(, default: false).
> 👉 Note:
> intentionally creates parse errors in markup
> (how parse errors are handled is well defined,
> so this works but isn’t pretty).
###### characterReferences
Configure how to serialize character references
([CharacterReferences][api-character-references], optional).
###### closeEmptyElements
Close SVG elements without any content with slash (/) on the opening tag
instead of an end tag: instead of boolean
(, default: false).
See tightSelfClosing to control whether a space is used before the slash.
Not used in the HTML space.
###### closeSelfClosing
Close self-closing nodes with an extra slash (/): instead of boolean
(, default: false).
See tightSelfClosing to control whether a space is used before the slash.
Not used in the SVG space.
###### collapseEmptyAttributes
Collapse empty attributes:
get class instead of class=""boolean
(, default: false).
Not used in the SVG space.
> 👉 Note:
> boolean attributes
> (such as hidden)
> are always collapsed.
###### omitOptionalTags
Omit optional opening and closing tags
(boolean, default: false).
For example,
in
,
both closing tags can be omitted.
The first because it’s followed by another li,
the last because it’s followed by nothing.Not used in the SVG space.
######
preferUnquotedLeave attributes unquoted if that results in less bytes
(
boolean, default: false).Not used in the SVG space.
######
quotePreferred quote to use
([
Quote][api-quote], default: '"').######
quoteSmartUse the other quote if that results in less bytes
(
boolean, default: false).######
spaceWhich space the document is in
([
Space][api-space], default: 'html').When an