18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
20
* [ ` fromHtml(value[, options]) ` ] ( #fromhtmlvalue-options )
21
- * [ ` Options ` ] ( #options )
22
- * [ ` OnError ` ] ( #onerror )
23
21
* [ ` ErrorCode ` ] ( #errorcode )
24
22
* [ ` ErrorSeverity ` ] ( #errorseverity )
23
+ * [ ` OnError ` ] ( #onerror )
24
+ * [ ` Options ` ] ( #options )
25
25
* [ Examples] ( #examples )
26
26
* [ Example: fragment versus document] ( #example-fragment-versus-document )
27
27
* [ Example: whitespace around and inside ` <html> ` ] ( #example-whitespace-around-and-inside-html )
@@ -42,24 +42,29 @@ tree.
42
42
## When should I use this?
43
43
44
44
If you want to handle syntax trees manually, use this.
45
+
45
46
Use [ ` parse5 ` ] [ parse5 ] instead when you just want to parse HTML and don’t care
46
47
about [ hast] [ ] .
47
-
48
48
You can also use [ ` hast-util-from-parse5 ` ] [ hast-util-from-parse5 ] and
49
- [ ` parse5 ` ] [ parse5 ] yourself manually , or use the rehype plugin
49
+ [ ` parse5 ` ] [ parse5 ] yourself, or use the rehype plugin
50
50
[ ` rehype-parse ` ] [ rehype-parse ] , which wraps this utility to also parse HTML at
51
51
a higher-level (easier) abstraction.
52
52
[ ` xast-util-from-xml ` ] [ xast-util-from-xml ] can be used if you are dealing with
53
53
XML instead of HTML.
54
54
55
+ If you might run in a browser and prefer a ligher alternative, while not caring
56
+ about positional info, parse errors, and consistency across browsers, use
57
+ [ ` hast-util-from-html-isomorphic ` ] [ hast-util-from-html-isomorphic ] , which
58
+ wraps this in Node and uses browser APIs otherwise.
59
+
55
60
Finally you can use the utility [ ` hast-util-to-html ` ] [ hast-util-to-html ] to do
56
61
the inverse of this utility.
57
62
It turns hast into HTML.
58
63
59
64
## Install
60
65
61
66
This package is [ ESM only] [ esm ] .
62
- In Node.js (version 14.14+ and 16.0 +), install with [ npm] [ ] :
67
+ In Node.js (version 16 +), install with [ npm] [ ] :
63
68
64
69
``` sh
65
70
npm install hast-util-from-html
@@ -68,14 +73,14 @@ npm install hast-util-from-html
68
73
In Deno with [ ` esm.sh ` ] [ esmsh ] :
69
74
70
75
``` js
71
- import {fromHtml } from " https://esm.sh/hast-util-from-html@1"
76
+ import {fromHtml } from ' https://esm.sh/hast-util-from-html@1'
72
77
```
73
78
74
79
In browsers with [ ` esm.sh ` ] [ esmsh ] :
75
80
76
81
``` html
77
82
<script type =" module" >
78
- import {fromHtml } from " https://esm.sh/hast-util-from-html@1?bundle"
83
+ import {fromHtml } from ' https://esm.sh/hast-util-from-html@1?bundle'
79
84
</script >
80
85
```
81
86
@@ -113,7 +118,7 @@ Yields:
113
118
114
119
## API
115
120
116
- This package exports the identifier [ ` fromHtml ` ] [ fromhtml ] .
121
+ This package exports the identifier [ ` fromHtml ` ] [ api-from-html ] .
117
122
There is no default export.
118
123
119
124
### ` fromHtml(value[, options]) `
@@ -126,13 +131,58 @@ Turn serialized HTML into a hast tree.
126
131
127
132
* ` value ` ([ ` Compatible ` ] [ compatible ] )
128
133
— serialized HTML to parse
129
- * ` options ` ([ ` Options ` ] [ options ] , optional)
134
+ * ` options ` ([ ` Options ` ] [ api- options] , optional)
130
135
— configuration
131
136
132
137
###### Returns
133
138
134
139
Tree ([ ` Root ` ] [ root ] ).
135
140
141
+ ### ` ErrorCode `
142
+
143
+ Known names of parse errors (TypeScript type).
144
+
145
+ ###### Types
146
+
147
+ ``` ts
148
+ type ErrorCode =
149
+ | ' abandonedHeadElementChild'
150
+ | ' abruptClosingOfEmptyComment'
151
+ | ' abruptDoctypePublicIdentifier'
152
+ // … see readme on `options[key in ErrorCode]` above.
153
+ ` ` `
154
+
155
+ ### ` ErrorSeverity `
156
+
157
+ Error severity (TypeScript type).
158
+
159
+ ###### Types
160
+
161
+ ` ` ` ts
162
+ export type ErrorSeverity =
163
+ // Turn the parse error off:
164
+ | 0
165
+ | false
166
+ // Turn the parse error into a warning:
167
+ | 1
168
+ | true
169
+ // Turn the parse error into an actual error: processing stops.
170
+ | 2
171
+ ` ` `
172
+
173
+ ### ` OnError `
174
+
175
+ Function called when encountering [HTML parse errors][parse-errors].
176
+
177
+ ###### Parameters
178
+
179
+ * ` error ` ([ ` VFileMessage ` ][vfile-message])
180
+ — message
181
+
182
+ ###### Returns
183
+
184
+ Nothing ( ` void ` ).
185
+
136
186
### ` Options `
137
187
138
188
Configuration (TypeScript type).
@@ -141,7 +191,7 @@ Configuration (TypeScript type).
141
191
142
192
###### ` options .space `
143
193
144
- Which space the document is in (` 'svg ' ` or ` 'html ' ` , default: ` 'html' ` ).
194
+ Which space the document is in ( ` ' html ' ` or ` ' svg ' ` , default: ` ' html' ` ).
145
195
146
196
When an ` <svg >` element is found in the HTML space, ` hast -util -from -html `
147
197
already automatically switches to and from the SVG space when entering and
@@ -169,13 +219,13 @@ In document mode, unopened `html`, `head`, and `body` elements are opened.
169
219
###### ` options .onerror `
170
220
171
221
Function called when encountering [HTML parse errors][parse-errors]
172
- ([ ` OnError ` ] [ onerror ] , optional).
222
+ ([ ` OnError ` ][api-on-error ], optional).
173
223
174
224
###### ` options [key in ErrorCode ]`
175
225
176
226
Specific parse errors can be configured by setting their identifiers (see
177
- [ ` ErrorCode ` ] [ errorcode ] ) as keys directly in ` options ` to an
178
- [ ` ErrorSeverity ` ] [ errorseverity ] as value.
227
+ [ ` ErrorCode ` ][api-error-code ]) as keys directly in ` options ` to an
228
+ [ ` ErrorSeverity ` ][api-error-severity ] as value.
179
229
180
230
The list of parse errors:
181
231
@@ -244,51 +294,6 @@ The list of parse errors:
244
294
245
295
<!-- parse-error end -->
246
296
247
- ### ` OnError `
248
-
249
- Function called when encountering [ HTML parse errors] [ parse-errors ] .
250
-
251
- ###### Parameters
252
-
253
- * ` error ` ([ ` VFileMessage ` ] [ vfilemessage ] )
254
- — message
255
-
256
- ###### Returns
257
-
258
- Nothing (` void ` ).
259
-
260
- ### ` ErrorCode `
261
-
262
- Known names of parse errors (TypeScript type).
263
-
264
- ###### Types
265
-
266
- ``` ts
267
- type ErrorCode =
268
- | ' abandonedHeadElementChild'
269
- | ' abruptClosingOfEmptyComment'
270
- | ' abruptDoctypePublicIdentifier'
271
- // … see readme on `options[key in ErrorCode]` above.
272
- ` ` `
273
-
274
- ### ` ErrorSeverity `
275
-
276
- Error severity (TypeScript type).
277
-
278
- ###### Types
279
-
280
- ` ` ` ts
281
- export type ErrorSeverity =
282
- // Turn the parse error off:
283
- | 0
284
- | false
285
- // Turn the parse error into a warning:
286
- | 1
287
- | true
288
- // Turn the parse error into an actual error: processing stops.
289
- | 2
290
- ` ` `
291
-
292
297
## Examples
293
298
294
299
### Example: fragment versus document
@@ -368,7 +373,7 @@ root[2] (1:1-9:8, 0-119)
368
373
│ │ └─0 text "Hi!" (4:12-4:15, 51-54)
369
374
│ └─2 text "\n " (4:23-5:3, 62-65)
370
375
├─1 text "\n " (5:10-6:3, 72-75)
371
- └─2 element<body>[3] (6:3-9:8 , 75-119 )
376
+ └─2 element<body>[3] (6:3-8:10 , 75-111 )
372
377
│ properties: {}
373
378
├─0 text "\n " (6:9-7:5, 81-86)
374
379
├─1 element<h1>[1] (7:5-7:20, 86-101)
@@ -410,24 +415,34 @@ fromHtml(doc, {
410
415
411
416
``` txt
412
417
[1:10-1:10: Missing whitespace before doctype name] {
413
- reason: 'Missing whitespace before doctype name' ,
414
- line: 1 ,
418
+ ancestors: undefined ,
419
+ cause: undefined ,
415
420
column: 10,
416
- source: 'parse-error',
417
- ruleId: 'missing-whitespace-before-doctype-name',
418
- position: [Object],
419
421
fatal: true,
422
+ line: 1,
423
+ place: {
424
+ start: { line: 1, column: 10, offset: 9 },
425
+ end: { line: 1, column: 10, offset: 9 }
426
+ },
427
+ reason: 'Missing whitespace before doctype name',
428
+ ruleId: 'missing-whitespace-before-doctype-name',
429
+ source: 'parse-error',
420
430
note: 'Unexpected `h`. Expected ASCII whitespace instead',
421
431
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-before-doctype-name'
422
432
}
423
433
[2:23-2:23: Unexpected duplicate attribute] {
424
- reason: 'Unexpected duplicate attribute' ,
425
- line: 2 ,
434
+ ancestors: undefined ,
435
+ cause: undefined ,
426
436
column: 23,
427
- source: 'parse-error',
428
- ruleId: 'duplicate-attribute',
429
- position: [Object],
430
437
fatal: false,
438
+ line: 2,
439
+ place: {
440
+ start: { line: 2, column: 23, offset: 37 },
441
+ end: { line: 2, column: 23, offset: 37 }
442
+ },
443
+ reason: 'Unexpected duplicate attribute',
444
+ ruleId: 'duplicate-attribute',
445
+ source: 'parse-error',
431
446
note: 'Unexpectedly double attribute. Expected attributes to occur only once',
432
447
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-duplicate-attribute'
433
448
}
@@ -451,15 +466,19 @@ followed by browsers such as Chrome and Firefox.
451
466
## Types
452
467
453
468
This package is fully typed with [ TypeScript] [ ] .
454
- It exports the additional type [ ` Options ` ] [ options ] , [ ` OnError ` ] [ onerror ] ,
455
- [ ` ErrorCode ` ] [ errorcode ] , and [ ` ErrorSeverity ` ] [ errorseverity ] .
469
+ It exports the additional types
470
+ [ ` ErrorCode ` ] [ api-error-code ] , [ ` ErrorSeverity ` ] [ api-error-severity ] ,
471
+ [ ` OnError ` ] [ api-on-error ] , and [ ` Options ` ] [ api-options ] .
456
472
457
473
## Compatibility
458
474
459
- Projects maintained by the unified collective are compatible with all maintained
475
+ Projects maintained by the unified collective are compatible with maintained
460
476
versions of Node.js.
461
- As of now, that is Node.js 14.14+ and 16.0+.
462
- Our projects sometimes work with older versions, but this is not guaranteed.
477
+
478
+ When we cut a new major release, we drop support for unmaintained versions of
479
+ Node.
480
+ This means we try to keep the current release line, ` hast-util-from-html@^1 ` ,
481
+ compatible with Node.js 12.
463
482
464
483
## Security
465
484
@@ -504,9 +523,9 @@ abide by its terms.
504
523
505
524
[ downloads ] : https://www.npmjs.com/package/hast-util-from-html
506
525
507
- [ size-badge ] : https://img.shields.io/bundlephobia/minzip/ hast-util-from-html.svg
526
+ [ size-badge ] : https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q= hast-util-from-html
508
527
509
- [ size ] : https://bundlephobia .com/result?p =hast-util-from-html
528
+ [ size ] : https://bundlejs .com/?q =hast-util-from-html
510
529
511
530
[ sponsors-badge ] : https://opencollective.com/unified/sponsors/badge.svg
512
531
@@ -560,16 +579,18 @@ abide by its terms.
560
579
561
580
[ parse-errors ] : https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
562
581
563
- [ vfilemessage ] : https://github.com/vfile/vfile-message#vfilemessagereason-place-origin
582
+ [ vfile-message ] : https://github.com/vfile/vfile-message#vfilemessagereason-place-origin
564
583
565
- [ fromhtml ] : #fromhtmlvalue-options
584
+ [ hast-util-from-html-isomorphic ] : https://github.com/syntax-tree/hast-util-from-html-isomorphic
566
585
567
- [ options ] : #options
586
+ [ compatible ] : https://github.com/vfile/vfile/blob/03efac7/lib/index.js#L16
568
587
569
- [ onerror ] : #onerror
588
+ [ api-from-html ] : #fromhtmlvalue-options
570
589
571
- [ errorcode ] : #errorcode
590
+ [ api-error-code ] : #errorcode
572
591
573
- [ errorseverity ] : #errorseverity
592
+ [ api-error-severity ] : #errorseverity
574
593
575
- [ compatible ] : https://github.com/vfile/vfile/blob/03efac7/lib/index.js#L16
594
+ [ api-on-error ] : #onerror
595
+
596
+ [ api-options ] : #options
0 commit comments