8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** hast** ] [ hast ] utility to serialize to HTML.
11
+ [ hast] [ ] utility to serialize hast as HTML.
12
12
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.
14
51
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
17
53
18
- [ npm] [ ] :
54
+ This package is [ ESM only] [ esm ] .
55
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
19
56
20
57
``` sh
21
58
npm install hast-util-to-html
22
59
```
23
60
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
+
24
75
## Use
25
76
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
+
26
85
``` js
27
86
import {h } from ' hastscript'
28
87
import {toHtml } from ' hast-util-to-html'
@@ -45,36 +104,24 @@ Yields:
45
104
46
105
## API
47
106
48
- This package exports the following identifiers: ` toHtml ` .
107
+ This package exports the identifier ` toHtml ` .
49
108
There is no default export.
50
109
51
110
### ` toHtml(tree[, options]) `
52
111
53
- Serialize the given [ ** hast** ] [ hast ] [ * tree * ] [ tree ] (or list of nodes) .
112
+ Serialize hast ( [ ` Node ` ] [ node ] , ` Array<Node> ` ) as HTML .
54
113
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 `
59
115
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).
62
117
63
118
###### ` options.entities `
64
119
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 ` ).
78
125
79
126
###### ` options.upperDoctype `
80
127
@@ -99,8 +146,8 @@ Not used in the SVG space.
99
146
###### ` options.omitOptionalTags `
100
147
101
148
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.
104
151
The first because it’s followed by another ` li ` , the last because it’s followed
105
152
by nothing.
106
153
@@ -110,10 +157,11 @@ Not used in the SVG space.
110
157
111
158
Collapse empty attributes: get ` class ` instead of ` class="" ` (` boolean ` ,
112
159
default: ` false ` ).
113
- ** Note** : boolean attributes, such as ` hidden ` , are always collapsed.
114
160
115
161
Not used in the SVG space.
116
162
163
+ > 👉 ** Note** : boolean attributes (such as ` hidden ` ) are always collapsed.
164
+
117
165
###### ` options.closeSelfClosing `
118
166
119
167
Close self-closing nodes with an extra slash (` / ` ): ` <img /> ` instead of
@@ -135,72 +183,126 @@ Not used in the HTML space.
135
183
136
184
Do not use an extra space when closing self-closing elements: ` <img/> ` instead
137
185
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 ` .
139
189
140
190
###### ` options.tightCommaSeparatedLists `
141
191
142
192
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)
144
194
(` boolean ` , default: ` false ` ).
145
195
146
196
###### ` options.tightAttributes `
147
197
148
198
Join attributes together, without whitespace, if possible: get
149
199
` class="a b"title="c d" ` instead of ` class="a b" title="c d" ` to save bytes
150
200
(` boolean ` , default: ` false ` ).
151
- ** Note** : creates invalid (but working) markup.
152
201
153
202
Not used in the SVG space.
154
203
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
+
155
207
###### ` options.tightDoctype `
156
208
157
209
Drop unneeded spaces in doctypes: ` <!doctypehtml> ` instead of ` <!doctype html> `
158
210
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).
160
214
161
215
###### ` options.bogusComments `
162
216
163
217
Use “bogus comments” instead of comments to save byes: ` <?charlie> ` instead of
164
218
` <!--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).
166
222
167
223
###### ` options.allowParseErrors `
168
224
169
225
Do not encode characters which cause parse errors (even though they work), to
170
226
save bytes (` boolean ` , default: ` false ` ).
171
- ** Note** : creates invalid (but working) markup.
172
227
173
228
Not used in the SVG space.
174
229
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
+
175
233
###### ` options.allowDangerousCharacters `
176
234
177
235
Do not encode some characters which cause XSS vulnerabilities in older browsers
178
236
(` 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.
180
239
181
240
###### ` options.allowDangerousHtml `
182
241
183
242
Allow ` raw ` nodes and insert them as raw HTML.
184
243
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.
186
290
187
291
## Security
188
292
189
293
Use of ` hast-util-to-html ` can open you up to a
190
294
[ 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.
192
296
193
297
## Related
194
298
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
199
301
200
302
## Contribute
201
303
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.
204
306
See [ ` support.md ` ] [ support ] for ways to get help.
205
307
206
308
This project has a [ code of conduct] [ coc ] .
@@ -241,28 +343,40 @@ abide by its terms.
241
343
242
344
[ npm ] : https://docs.npmjs.com/cli/install
243
345
346
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
347
+
348
+ [ esmsh ] : https://esm.sh
349
+
350
+ [ typescript ] : https://www.typescriptlang.org
351
+
244
352
[ license ] : license
245
353
246
354
[ author ] : https://wooorm.com
247
355
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
249
359
250
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
360
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
251
361
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
253
369
254
370
[ html-void-elements ] : https://github.com/wooorm/html-void-elements
255
371
256
372
[ stringify-entities ] : https://github.com/wooorm/stringify-entities
257
373
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
261
375
262
- [ hast ] : https://github.com/syntax-tree/hast
376
+ [ hast-util-from-parse5 ] : https://github.com/syntax-tree/hast-util-from-parse5
263
377
264
- [ element ] : https://github.com/syntax-tree/hast#element
378
+ [ parse5 ] : https://github.com/inikulin/parse5
265
379
266
- [ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
380
+ [ rehype-stringify ] : https://github.com/rehypejs/rehype/tree/main/packages/rehype-stringify#readme
267
381
268
- [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
382
+ [ xast ] : https://github.com/syntax-tree/xast
0 commit comments