@@ -34,9 +34,10 @@ This package is a utility that turns a hast tree into a string of HTML.
34
34
## When should I use this?
35
35
36
36
You can use this utility when you want to get the serialized HTML that is
37
- represented by the syntax tree, either because you’re done with the syntax
38
- tree, or because you’re integrating with another tool that does not support
39
- syntax trees.
37
+ represented by the syntax tree,
38
+ either because you’re done with the syntax tree,
39
+ or because you’re integrating with another tool that does not support syntax
40
+ trees.
40
41
41
42
This utility has many options to configure how the HTML is serialized.
42
43
These options help when building tools that make output pretty (such as
@@ -106,9 +107,11 @@ Serialize hast as HTML.
106
107
107
108
###### Parameters
108
109
109
- * ` tree ` ([ ` Node ` ] [ github-hast-nodes ] or ` Array<Node> ` )
110
+ * ` tree `
111
+ ([ ` Node ` ] [ github-hast-nodes ] or ` Array<Node> ` )
110
112
— tree to serialize
111
- * ` options ` ([ ` Options ` ] [ api-options ] , optional)
113
+ * ` options `
114
+ ([ ` Options ` ] [ api-options ] , optional)
112
115
— configuration
113
116
114
117
###### Returns
@@ -123,25 +126,28 @@ How to serialize character references (TypeScript type).
123
126
124
127
###### ` useNamedReferences `
125
128
126
- Prefer named character references (` & ` ) where possible ( ` boolean ` , default:
127
- ` false ` ).
129
+ Prefer named character references (` & ` ) where possible
130
+ ( ` boolean ` , default: ` false ` ).
128
131
129
132
###### ` omitOptionalSemicolons `
130
133
131
- Whether to omit semicolons when possible (` boolean ` , default: ` false ` ).
134
+ Whether to omit semicolons when possible
135
+ (` boolean ` , default: ` false ` ).
132
136
133
- > ⚠️ ** Note** : this creates what HTML calls “parse errors” but is otherwise
134
- > still valid HTML — don’t use this except when building a minifier.
135
- > Omitting semicolons is possible for certain named and numeric references in
137
+ > ⚠️ ** Note** :
138
+ > this creates what HTML calls “parse errors” but is otherwise still valid HTML:
139
+ > don’t use this except when building a minifier;
140
+ > omitting semicolons is possible for certain named and numeric references in
136
141
> some cases.
137
142
138
143
###### ` useShortestReferences `
139
144
140
- Prefer the shortest possible reference, if that results in less bytes
145
+ Prefer the shortest possible reference,
146
+ if that results in less bytes
141
147
(` boolean ` , default: ` false ` ).
142
148
143
- > ⚠️ ** Note** : ` useNamedReferences ` can be omitted when using
144
- > ` useShortestReferences ` .
149
+ > ⚠️ ** Note** :
150
+ > ` useNamedReferences ` can be omitted when using ` useShortestReferences ` .
145
151
146
152
### ` Options `
147
153
@@ -154,33 +160,44 @@ Configuration (TypeScript type).
154
160
Do not encode some characters which cause XSS vulnerabilities in older browsers
155
161
(` boolean ` , default: ` false ` ).
156
162
157
- > ⚠️ ** Danger** : only set this if you completely trust the content.
163
+ > ⚠️ ** Danger** :
164
+ > only set this if you completely trust the content.
158
165
159
166
###### ` allowDangerousHtml `
160
167
161
- Allow ` raw ` nodes and insert them as raw HTML (` boolean ` , default: ` false ` ).
168
+ Allow ` raw ` nodes and insert them as raw HTML
169
+ (` boolean ` , default: ` false ` ).
162
170
163
- When ` false ` , ` Raw ` nodes are encoded.
171
+ When ` false ` ,
172
+ ` Raw ` nodes are encoded.
164
173
165
- > ⚠️ ** Danger** : only set this if you completely trust the content.
174
+ > ⚠️ ** Danger** :
175
+ > only set this if you completely trust the content.
166
176
167
177
###### ` allowParseErrors `
168
178
169
- Do not encode characters which cause parse errors (even though they work), to
170
- save bytes (` boolean ` , default: ` false ` ).
179
+ Do not encode characters which cause parse errors
180
+ (even though they work),
181
+ to save bytes
182
+ (` boolean ` , default: ` false ` ).
171
183
172
184
Not used in the SVG space.
173
185
174
- > 👉 ** Note** : intentionally creates parse errors in markup (how parse errors
175
- > are handled is well defined, so this works but isn’t pretty).
186
+ > 👉 ** Note** :
187
+ > intentionally creates parse errors in markup
188
+ > (how parse errors are handled is well defined,
189
+ > so this works but isn’t pretty).
176
190
177
191
###### ` bogusComments `
178
192
179
- Use “bogus comments” instead of comments to save byes: ` <?charlie> ` instead of
180
- ` <!--charlie--> ` (` boolean ` , default: ` false ` ).
193
+ Use “bogus comments” instead of comments to save byes:
194
+ ` <?charlie> ` instead of ` <!--charlie--> `
195
+ (` boolean ` , default: ` false ` ).
181
196
182
- > 👉 ** Note** : intentionally creates parse errors in markup (how parse errors
183
- > are handled is well defined, so this works but isn’t pretty).
197
+ > 👉 ** Note** :
198
+ > intentionally creates parse errors in markup
199
+ > (how parse errors are handled is well defined,
200
+ > so this works but isn’t pretty).
184
201
185
202
###### ` characterReferences `
186
203
@@ -190,118 +207,144 @@ Configure how to serialize character references
190
207
###### ` closeEmptyElements `
191
208
192
209
Close SVG elements without any content with slash (` / ` ) on the opening tag
193
- instead of an end tag: ` <circle /> ` instead of ` <circle></circle> ` (` boolean ` ,
194
- default: ` false ` ).
210
+ instead of an end tag:
211
+ ` <circle /> ` instead of ` <circle></circle> `
212
+ (` boolean ` , default: ` false ` ).
195
213
196
214
See ` tightSelfClosing ` to control whether a space is used before the slash.
197
215
198
216
Not used in the HTML space.
199
217
200
218
###### ` closeSelfClosing `
201
219
202
- Close self-closing nodes with an extra slash (` / ` ): ` <img /> ` instead of
203
- ` <img> ` (` boolean ` , default: ` false ` ).
220
+ Close self-closing nodes with an extra slash (` / ` ):
221
+ ` <img /> ` instead of ` <img> `
222
+ (` boolean ` , default: ` false ` ).
204
223
205
224
See ` tightSelfClosing ` to control whether a space is used before the slash.
206
225
207
226
Not used in the SVG space.
208
227
209
228
###### ` collapseEmptyAttributes `
210
229
211
- Collapse empty attributes: get ` class ` instead of ` class="" ` (` boolean ` ,
212
- default: ` false ` ).
230
+ Collapse empty attributes:
231
+ get ` class ` instead of ` class="" `
232
+ (` boolean ` , default: ` false ` ).
213
233
214
234
Not used in the SVG space.
215
235
216
- > 👉 ** Note** : boolean attributes (such as ` hidden ` ) are always collapsed.
236
+ > 👉 ** Note** :
237
+ > boolean attributes
238
+ > (such as ` hidden ` )
239
+ > are always collapsed.
217
240
218
241
###### ` omitOptionalTags `
219
242
220
- Omit optional opening and closing tags (` boolean ` , default: ` false ` ).
243
+ Omit optional opening and closing tags
244
+ (` boolean ` , default: ` false ` ).
221
245
222
- For example, in ` <ol><li>one</li><li>two</li></ol> ` , both ` </li> ` closing tags
223
- can be omitted.
224
- The first because it’s followed by another ` li ` , the last because it’s followed
225
- by nothing.
246
+ For example,
247
+ in ` <ol><li>one</li><li>two</li></ol> ` ,
248
+ both ` </li> ` closing tags can be omitted.
249
+ The first because it’s followed by another ` li ` ,
250
+ the last because it’s followed by nothing.
226
251
227
252
Not used in the SVG space.
228
253
229
254
###### ` preferUnquoted `
230
255
231
- Leave attributes unquoted if that results in less bytes ( ` boolean ` , default:
232
- ` false ` ).
256
+ Leave attributes unquoted if that results in less bytes
257
+ ( ` boolean ` , default: ` false ` ).
233
258
234
259
Not used in the SVG space.
235
260
236
261
###### ` quote `
237
262
238
- Preferred quote to use ([ ` Quote ` ] [ api-quote ] , default: ` '"' ` ).
263
+ Preferred quote to use
264
+ ([ ` Quote ` ] [ api-quote ] , default: ` '"' ` ).
239
265
240
266
###### ` quoteSmart `
241
267
242
- Use the other quote if that results in less bytes (` boolean ` , default: ` false ` ).
268
+ Use the other quote if that results in less bytes
269
+ (` boolean ` , default: ` false ` ).
243
270
244
271
###### ` space `
245
272
246
- Which space the document is in ([ ` Space ` ] [ api-space ] , default: ` 'html' ` ).
273
+ Which space the document is in
274
+ ([ ` Space ` ] [ api-space ] , default: ` 'html' ` ).
247
275
248
- When an ` <svg> ` element is found in the HTML space, this package already
249
- automatically switches to and from the SVG space when entering and exiting it.
276
+ When an ` <svg> ` element is found in the HTML space,
277
+ this package already automatically switches to and from the SVG space when
278
+ entering and exiting it.
250
279
251
- > 👉 ** Note** : hast is not XML.
280
+ > 👉 ** Note** :
281
+ > hast is not XML.
252
282
> It supports SVG as embedded in HTML.
253
283
> It does not support the features available in XML.
254
284
> Passing SVG might break but fragments of modern SVG should be fine.
255
285
> Use [ ` xast ` ] [ github-xast ] if you need to support SVG as XML.
256
286
257
287
###### ` tightAttributes `
258
288
259
- Join attributes together, without whitespace, if possible: get
260
- ` class="a b"title="c d" ` instead of ` class="a b" title="c d" ` to save bytes
289
+ Join attributes together,
290
+ without whitespace,
291
+ if possible:
292
+ get ` class="a b"title="c d" ` instead of ` class="a b" title="c d" ` to save bytes
261
293
(` boolean ` , default: ` false ` ).
262
294
263
295
Not used in the SVG space.
264
296
265
- > 👉 ** Note** : intentionally creates parse errors in markup (how parse errors
266
- > are handled is well defined, so this works but isn’t pretty).
297
+ > 👉 ** Note** :
298
+ > intentionally creates parse errors in markup
299
+ > (how parse errors are handled is well defined,
300
+ > so this works but isn’t pretty).
267
301
268
302
###### ` tightCommaSeparatedLists `
269
303
270
- Join known comma-separated attribute values with just a comma (` , ` ), instead of
271
- padding them on the right as well (` ,␠ ` , where ` ␠ ` represents a space)
304
+ Join known comma-separated attribute values with just a comma (` , ` ),
305
+ instead of padding them on the right as well
306
+ (` ,␠ ` , where ` ␠ ` represents a space)
272
307
(` boolean ` , default: ` false ` ).
273
308
274
309
###### ` tightDoctype `
275
310
276
- Drop unneeded spaces in doctypes: ` <!doctypehtml> ` instead of ` <!doctype html> `
277
- to save bytes (` boolean ` , default: ` false ` ).
311
+ Drop unneeded spaces in doctypes:
312
+ ` <!doctypehtml> ` instead of ` <!doctype html> ` to save bytes
313
+ (` boolean ` , default: ` false ` ).
278
314
279
- > 👉 ** Note** : intentionally creates parse errors in markup (how parse errors
280
- > are handled is well defined, so this works but isn’t pretty).
315
+ > 👉 ** Note** :
316
+ > intentionally creates parse errors in markup
317
+ > (how parse errors are handled is well defined,
318
+ > so this works but isn’t pretty).
281
319
282
320
###### ` tightSelfClosing `
283
321
284
- Do not use an extra space when closing self-closing elements: ` <img/> ` instead
285
- of ` <img /> ` (` boolean ` , default: ` false ` ).
322
+ Do not use an extra space when closing self-closing elements:
323
+ ` <img/> ` instead of ` <img /> `
324
+ (` boolean ` , default: ` false ` ).
286
325
287
- > 👉 ** Note** : only used if ` closeSelfClosing: true ` or
288
- > ` closeEmptyElements: true ` .
326
+ > 👉 ** Note** :
327
+ > only used if ` closeSelfClosing: true ` or ` closeEmptyElements: true ` .
289
328
290
329
###### ` upperDoctype `
291
330
292
- Use a ` <!DOCTYPE… ` instead of ` <!doctype… ` (` boolean ` , default: ` false ` ).
331
+ Use a ` <!DOCTYPE… ` instead of ` <!doctype… `
332
+ (` boolean ` , default: ` false ` ).
293
333
294
334
Useless except for XHTML.
295
335
296
336
###### ` voids `
297
337
298
- Tag names of elements to serialize without closing tag (` Array<string> ` ,
338
+ Tag names of elements to serialize without closing tag
339
+ (` Array<string> ` ,
299
340
default: [ ` html-void-elements ` ] [ github-html-void-elements ] ).
300
341
301
342
Not used in the SVG space.
302
343
303
- > 👉 ** Note** : It’s highly unlikely that you want to pass this, because hast is
304
- > not for XML, and HTML will not add more void elements.
344
+ > 👉 ** Note** :
345
+ > it’s highly unlikely that you want to pass this,
346
+ > because hast is not for XML,
347
+ > and HTML will not add more void elements.
305
348
306
349
### ` Quote `
307
350
@@ -325,26 +368,28 @@ type Space = 'html' | 'svg'
325
368
326
369
## Syntax
327
370
328
- HTML is serialized according to WHATWG HTML (the living standard), which is also
329
- followed by browsers such as Chrome and Firefox.
371
+ HTML is serialized according to WHATWG HTML (the living standard),
372
+ which is also followed by browsers such as Chrome and Firefox.
330
373
331
374
## Types
332
375
333
376
This package is fully typed with [TypeScript][].
334
377
It exports the additional types
335
378
[ ` CharacterReferences ` ][api-character-references],
336
379
[ ` Options ` ][api-options],
337
- [ ` Quote ` ][api-quote], and
380
+ [ ` Quote ` ][api-quote],
381
+ and
338
382
[ ` Space ` ][api-space].
339
383
340
384
## Compatibility
341
385
342
386
Projects maintained by the unified collective are compatible with maintained
343
387
versions of Node.js.
344
388
345
- When we cut a new major release, we drop support for unmaintained versions of
346
- Node.
347
- This means we try to keep the current release line, ` hast -util -to -html @^9 ` ,
389
+ When we cut a new major release,
390
+ we drop support for unmaintained versions of Node.
391
+ This means we try to keep the current release line,
392
+ ` hast -util -to -html @9 ` ,
348
393
compatible with Node.js 16.
349
394
350
395
## Security
@@ -368,8 +413,9 @@ for ways to get started.
368
413
See [ ` support .md ` ][health-support] for ways to get help.
369
414
370
415
This project has a [code of conduct][health-coc].
371
- By interacting with this repository, organization, or community you agree to
372
- abide by its terms.
416
+ By interacting with this repository,
417
+ organization,
418
+ or community you agree to abide by its terms.
373
419
374
420
## License
375
421
0 commit comments