Skip to content

Commit d672d24

Browse files
committed
Add improved docs
1 parent a4c177b commit d672d24

File tree

2 files changed

+173
-55
lines changed

2 files changed

+173
-55
lines changed

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@
8080
},
8181
"remarkConfig": {
8282
"plugins": [
83-
"preset-wooorm"
83+
"preset-wooorm",
84+
[
85+
"remark-lint-no-html",
86+
false
87+
]
8488
]
8589
},
8690
"typeCoverage": {

readme.md

+168-54
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,80 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**hast**][hast] utility to serialize to HTML.
11+
[hast][] utility to serialize hast as HTML.
1212

13-
## Install
13+
## Contents
14+
15+
* [What is this?](#what-is-this)
16+
* [When should I use this?](#when-should-i-use-this)
17+
* [Install](#install)
18+
* [Use](#use)
19+
* [API](#api)
20+
* [`toHtml(tree[, options])`](#tohtmltree-options)
21+
* [Syntax](#syntax)
22+
* [Types](#types)
23+
* [Compatibility](#compatibility)
24+
* [Security](#security)
25+
* [Related](#related)
26+
* [Contribute](#contribute)
27+
* [License](#license)
28+
29+
## What is this?
30+
31+
This package is a utility that turns a hast tree into a string of HTML.
32+
33+
## When should I use this?
34+
35+
You can use this utility when you’re want to get the serialized HTML that is
36+
represented by the syntax tree, either because you’re done with the syntax tree,
37+
or because you’re integrating with
38+
another tool that does not support
39+
syntax trees.
40+
41+
This utility has many options to configure how the HTML is serialized.
42+
These options help when building tools that make output pretty (e.g.,
43+
formatters) or ugly (e.g., minifiers).
44+
45+
The utility [`hast-util-from-parse5`][hast-util-from-parse5] combined with
46+
[`parse5`][parse5] does the inverse of this utility.
47+
It turns HTML into hast.
48+
49+
The rehype plugin [`rehype-stringify`][rehype-stringify] wraps this utility to
50+
also serialize HTML at a higher-level (easier) abstraction.
1451

15-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
52+
## Install
1753

18-
[npm][]:
54+
This package is [ESM only][esm].
55+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
1956

2057
```sh
2158
npm install hast-util-to-html
2259
```
2360

61+
In Deno with [`esm.sh`][esmsh]:
62+
63+
```js
64+
import {toHtml} from "https://esm.sh/hast-util-to-html@8"
65+
```
66+
67+
In browsers with [`esm.sh`][esmsh]:
68+
69+
```html
70+
<script type="module">
71+
import {toHtml} from "https://esm.sh/hast-util-to-html@8?bundle"
72+
</script>
73+
```
74+
2475
## Use
2576

77+
<details><summary>Show install command for this example</summary>
78+
79+
```sh
80+
npm install hastscript hast-util-to-html
81+
```
82+
83+
</details>
84+
2685
```js
2786
import {h} from 'hastscript'
2887
import {toHtml} from 'hast-util-to-html'
@@ -45,36 +104,24 @@ Yields:
45104

46105
## API
47106

48-
This package exports the following identifiers: `toHtml`.
107+
This package exports the identifier `toHtml`.
49108
There is no default export.
50109

51110
### `toHtml(tree[, options])`
52111

53-
Serialize the given [**hast**][hast] [*tree*][tree] (or list of nodes).
112+
Serialize hast ([`Node`][node], `Array<Node>`) as HTML.
54113

55-
###### `options.space`
56-
57-
Whether the [*root*][root] of the [*tree*][tree] is in the `'html'` or `'svg'`
58-
space (enum, `'svg'` or `'html'`, default: `'html'`).
114+
##### `options`
59115

60-
If an `svg` element is found in the HTML space, `toHtml` automatically switches
61-
to the SVG space when entering the element, and switches back when exiting.
116+
Configuration (optional).
62117

63118
###### `options.entities`
64119

65-
Configuration for [`stringify-entities`][stringify-entities] (`Object`, default:
66-
`{}`).
67-
Do not use `escapeOnly`, `attribute`, or `subset` (`toHtml` already passes
68-
those, so they won’t work).
69-
However, `useNamedReferences`, `useShortestReferences`, and
70-
`omitOptionalSemicolons` are all fine.
71-
72-
###### `options.voids`
73-
74-
Tag names of [*elements*][element] to serialize without closing tag
75-
(`Array<string>`, default: [`html-void-elements`][html-void-elements]).
76-
77-
Not used in the SVG space.
120+
Define how to create character references (`Object`, default: `{}`).
121+
Configuration is passed to [`stringify-entities`][stringify-entities].
122+
You can use the fields `useNamedReferences`, `useShortestReferences`, and
123+
`omitOptionalSemicolons`.
124+
You cannot use the fields `escapeOnly`, `attribute`, or `subset`).
78125

79126
###### `options.upperDoctype`
80127

@@ -99,8 +146,8 @@ Not used in the SVG space.
99146
###### `options.omitOptionalTags`
100147

101148
Omit optional opening and closing tags (`boolean`, default: `false`).
102-
For example, in `<ol><li>one</li><li>two</li></ol>`, both `</li>`
103-
closing tags can be omitted.
149+
For example, in `<ol><li>one</li><li>two</li></ol>`, both `</li>` closing tags
150+
can be omitted.
104151
The first because it’s followed by another `li`, the last because it’s followed
105152
by nothing.
106153

@@ -110,10 +157,11 @@ Not used in the SVG space.
110157

111158
Collapse empty attributes: get `class` instead of `class=""` (`boolean`,
112159
default: `false`).
113-
**Note**: boolean attributes, such as `hidden`, are always collapsed.
114160

115161
Not used in the SVG space.
116162

163+
> 👉 **Note**: boolean attributes (such as `hidden`) are always collapsed.
164+
117165
###### `options.closeSelfClosing`
118166

119167
Close self-closing nodes with an extra slash (`/`): `<img />` instead of
@@ -135,72 +183,126 @@ Not used in the HTML space.
135183

136184
Do not use an extra space when closing self-closing elements: `<img/>` instead
137185
of `<img />` (`boolean`, default: `false`).
138-
**Note**: Only used if `closeSelfClosing: true` or `closeEmptyElements: true`.
186+
187+
> 👉 **Note**: only used if `closeSelfClosing: true` or
188+
> `closeEmptyElements: true`.
139189
140190
###### `options.tightCommaSeparatedLists`
141191

142192
Join known comma-separated attribute values with just a comma (`,`), instead of
143-
padding them on the right as well (`,·`, where `·` represents a space)
193+
padding them on the right as well (`,`, where `` represents a space)
144194
(`boolean`, default: `false`).
145195

146196
###### `options.tightAttributes`
147197

148198
Join attributes together, without whitespace, if possible: get
149199
`class="a b"title="c d"` instead of `class="a b" title="c d"` to save bytes
150200
(`boolean`, default: `false`).
151-
**Note**: creates invalid (but working) markup.
152201

153202
Not used in the SVG space.
154203

204+
> 👉 **Note**: intentionally creates parse errors in markup (how parse errors
205+
> are handled is well defined, so this works but isn’t pretty).
206+
155207
###### `options.tightDoctype`
156208

157209
Drop unneeded spaces in doctypes: `<!doctypehtml>` instead of `<!doctype html>`
158210
to save bytes (`boolean`, default: `false`).
159-
**Note**: creates invalid (but working) markup.
211+
212+
> 👉 **Note**: intentionally creates parse errors in markup (how parse errors
213+
> are handled is well defined, so this works but isn’t pretty).
160214
161215
###### `options.bogusComments`
162216

163217
Use “bogus comments” instead of comments to save byes: `<?charlie>` instead of
164218
`<!--charlie-->` (`boolean`, default: `false`).
165-
**Note**: creates invalid (but working) markup.
219+
220+
> 👉 **Note**: intentionally creates parse errors in markup (how parse errors
221+
> are handled is well defined, so this works but isn’t pretty).
166222
167223
###### `options.allowParseErrors`
168224

169225
Do not encode characters which cause parse errors (even though they work), to
170226
save bytes (`boolean`, default: `false`).
171-
**Note**: creates invalid (but working) markup.
172227

173228
Not used in the SVG space.
174229

230+
> 👉 **Note**: intentionally creates parse errors in markup (how parse errors
231+
> are handled is well defined, so this works but isn’t pretty).
232+
175233
###### `options.allowDangerousCharacters`
176234

177235
Do not encode some characters which cause XSS vulnerabilities in older browsers
178236
(`boolean`, default: `false`).
179-
**Note**: Only set this if you completely trust the content.
237+
238+
> ⚠️ **Danger**: only set this if you completely trust the content.
180239
181240
###### `options.allowDangerousHtml`
182241

183242
Allow `raw` nodes and insert them as raw HTML.
184243
When falsey, encodes `raw` nodes (`boolean`, default: `false`).
185-
**Note**: Only set this if you completely trust the content.
244+
245+
> ⚠️ **Danger**: only set this if you completely trust the content.
246+
247+
###### `options.space`
248+
249+
Which space the document is in (`'svg'` or `'html'`, default: `'html'`).
250+
251+
When an `<svg>` element is found in the HTML space, `rehype-stringify` already
252+
automatically switches to and from the SVG space when entering and exiting it.
253+
254+
> 👉 **Note**: hast is not XML.
255+
> It supports SVG as embedded in HTML.
256+
> It does not support the features available in XML.
257+
> Passing SVG might break but fragments of modern SVG should be fine.
258+
> Use [`xast`][xast] if you need to support SVG as XML.
259+
260+
###### `options.voids`
261+
262+
Tag names of elements to serialize without closing tag (`Array<string>`,
263+
default: [`html-void-elements`][html-void-elements]).
264+
265+
Not used in the SVG space.
266+
267+
> 👉 **Note**: It’s highly unlikely that you want to pass this, because hast is
268+
> not for XML, and HTML will not add more void elements.
269+
270+
##### Returns
271+
272+
Serialized HTML (`string`).
273+
274+
## Syntax
275+
276+
HTML is serialized according to WHATWG HTML (the living standard), which is also
277+
followed by browsers such as Chrome and Firefox.
278+
279+
## Types
280+
281+
This package is fully typed with [TypeScript][].
282+
It exports the additional type `Options`.
283+
284+
## Compatibility
285+
286+
Projects maintained by the unified collective are compatible with all maintained
287+
versions of Node.js.
288+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
289+
Our projects sometimes work with older versions, but this is not guaranteed.
186290

187291
## Security
188292

189293
Use of `hast-util-to-html` can open you up to a
190294
[cross-site scripting (XSS)][xss] attack if the hast tree is unsafe.
191-
Use [`hast-util-santize`][sanitize] to make the hast tree safe.
295+
Use [`hast-util-santize`][hast-util-sanitize] to make the hast tree safe.
192296

193297
## Related
194298

195-
* [`hast-util-sanitize`][sanitize]
196-
— Sanitize hast nodes
197-
* [`rehype-stringify`](https://github.com/rehypejs/rehype/tree/HEAD/packages/rehype-stringify)
198-
— Wrapper around this project for [**rehype**](https://github.com/wooorm/rehype)
299+
* [`hast-util-sanitize`](https://github.com/syntax-tree/hast-util-sanitize)
300+
— sanitize hast
199301

200302
## Contribute
201303

202-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
203-
started.
304+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
305+
ways to get started.
204306
See [`support.md`][support] for ways to get help.
205307

206308
This project has a [code of conduct][coc].
@@ -241,28 +343,40 @@ abide by its terms.
241343

242344
[npm]: https://docs.npmjs.com/cli/install
243345

346+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
347+
348+
[esmsh]: https://esm.sh
349+
350+
[typescript]: https://www.typescriptlang.org
351+
244352
[license]: license
245353

246354
[author]: https://wooorm.com
247355

248-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
356+
[health]: https://github.com/syntax-tree/.github
357+
358+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
249359

250-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
360+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
251361

252-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
362+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
363+
364+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
365+
366+
[hast]: https://github.com/syntax-tree/hast
367+
368+
[node]: https://github.com/syntax-tree/hast#nodes
253369

254370
[html-void-elements]: https://github.com/wooorm/html-void-elements
255371

256372
[stringify-entities]: https://github.com/wooorm/stringify-entities
257373

258-
[tree]: https://github.com/syntax-tree/unist#tree
259-
260-
[root]: https://github.com/syntax-tree/unist#root
374+
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
261375

262-
[hast]: https://github.com/syntax-tree/hast
376+
[hast-util-from-parse5]: https://github.com/syntax-tree/hast-util-from-parse5
263377

264-
[element]: https://github.com/syntax-tree/hast#element
378+
[parse5]: https://github.com/inikulin/parse5
265379

266-
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
380+
[rehype-stringify]: https://github.com/rehypejs/rehype/tree/main/packages/rehype-stringify#readme
267381

268-
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
382+
[xast]: https://github.com/syntax-tree/xast

0 commit comments

Comments
 (0)