# rehype-stringify
[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
**[rehype][]** plugin to add support for serializing HTML.
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`unified().use(rehypeStringify[, options])`](#unifieduserehypestringify-options)
* [Syntax](#syntax)
* [Syntax tree](#syntax-tree)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Contribute](#contribute)
* [Sponsor](#sponsor)
* [License](#license)
## What is this?
This package is a [unified][] ([rehype][]) plugin that defines how to take a
syntax tree as input and turn it into serialized HTML.
When it’s used, HTML is serialized as the final result.
See [the monorepo readme][rehype] for info on what the rehype ecosystem is.
## When should I use this?
This plugin adds support to unified for serializing HTML.
You can alternatively use [`rehype`][rehype-core] instead, which combines
unified, [`rehype-parse`][rehype-parse], and this plugin.
When you’re in a browser, trust your content, don’t need formatting options, and
value a smaller bundle size, you can use
[`rehype-dom-stringify`][rehype-dom-stringify] instead.
This plugin is built on [`hast-util-to-html`][hast-util-to-html], which turns
[hast][] syntax trees into a string.
rehype focusses on making it easier to transform content by abstracting such
internals away.
## Install
This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
```sh
npm install rehype-stringify
```
In Deno with [Skypack][]:
```js
import rehypeStringify from 'https://cdn.skypack.dev/rehype-stringify@9?dts'
```
In browsers with [Skypack][]:
```html
```
## Use
Say we have the following module `example.js`:
```js
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkGfm from 'remark-gfm'
import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify'
main()
async function main() {
const file = await unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeStringify)
.process('# Hi\n\n*Hello*, world!')
console.log(String(file))
}
```
…running that with `node example.js` yields:
```html
Hi
Hello, world!
```
## API
This package exports no identifiers.
The default export is `rehypeStringify`.
### `unified().use(rehypeStringify[, options])`
Add support for serializing HTML.
Options are passed to [`hast-util-to-html`][hast-util-to-html].
##### `options`
Configuration (optional).
###### `options.entities`
Define how to create character references (`Object`, default: `{}`).
Configuration is passed to [`stringify-entities`][stringify-entities].
You can use the fields `useNamedReferences`, `useShortestReferences`, and
`omitOptionalSemicolons`.
You cannot use the fields `escapeOnly`, `attribute`, or `subset`).
###### `options.upperDoctype`
Use a `
one
two
`, 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.
###### `options.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.
###### `options.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.
###### `options.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.
###### `options.tightSelfClosing`
Do not use an extra space when closing self-closing elements: `` instead
of `` (`boolean`, default: `false`).
> 👉 **Note**: only used if `closeSelfClosing: true` or
> `closeEmptyElements: true`.
###### `options.tightCommaSeparatedLists`
Join known comma-separated attribute values with just a comma (`,`), instead of
padding them on the right as well (`,␠`, where `␠` represents a space)
(`boolean`, default: `false`).
###### `options.tightAttributes`
Join attributes together, without whitespace, if possible: get
`class="a b"title="c d"` instead of `class="a b" title="c d"` 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).
###### `options.tightDoctype`
Drop unneeded spaces in doctypes: `` instead of ``
to save bytes (`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).
###### `options.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).
###### `options.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).
###### `options.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.
###### `options.allowDangerousHtml`
Allow `raw` nodes and insert them as raw HTML.
When falsey, encodes `raw` nodes (`boolean`, default: `false`).
> ⚠️ **Danger**: only set this if you completely trust the content.
###### `options.space`
Which space the document is in (`'svg'` or `'html'`, default: `'html'`).
When an `