17
17
* [ Install] ( #install )
18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
- * [ ` fromHtml(file[, options]) ` ] ( #fromhtmlfile-options )
20
+ * [ ` fromHtml(value[, options]) ` ] ( #fromhtmlvalue-options )
21
+ * [ ` Options ` ] ( #options )
22
+ * [ ` OnError ` ] ( #onerror )
23
+ * [ ` ErrorCode ` ] ( #errorcode )
24
+ * [ ` ErrorSeverity ` ] ( #errorseverity )
21
25
* [ Examples] ( #examples )
22
26
* [ Example: fragment versus document] ( #example-fragment-versus-document )
23
27
* [ Example: whitespace around and inside ` <html> ` ] ( #example-whitespace-around-and-inside-html )
24
28
* [ Example: parse errors] ( #example-parse-errors )
25
29
* [ Syntax] ( #syntax )
26
- * [ Types] ( #types )
30
+ * [ Types] ( #types-2 )
27
31
* [ Compatibility] ( #compatibility )
28
32
* [ Security] ( #security )
29
33
* [ Related] ( #related )
@@ -55,7 +59,7 @@ It turns hast into HTML.
55
59
## Install
56
60
57
61
This package is [ ESM only] [ esm ] .
58
- In Node.js (version 14.14+, 16.0+, or 18 .0+), install with [ npm] [ ] :
62
+ In Node.js (version 14.14+ and 16 .0+), install with [ npm] [ ] :
59
63
60
64
``` sh
61
65
npm install hast-util-from-html
@@ -109,23 +113,31 @@ Yields:
109
113
110
114
## API
111
115
112
- This package exports the identifier ` fromHtml ` .
116
+ This package exports the identifier [ ` fromHtml ` ] [ fromhtml ] .
113
117
There is no default export.
114
118
115
- ### ` fromHtml(file [, options]) `
119
+ ### ` fromHtml(value [, options]) `
116
120
117
- Parse ` file ` ([ ` VFile ` ] [ vfile ] or anything that can be given to ` VFile ` ) as hast
118
- ([ ` Root ` ] [ root ] ).
121
+ Turn serialized HTML into a hast tree.
119
122
120
- ##### ` options `
123
+ ###### Parameters
121
124
122
- Configuration (optional).
125
+ <!-- To do: update link when `vfile` has new docs. -->
123
126
124
- ###### ` options.fragment `
127
+ * ` value ` ([ ` Compatible ` ] [ compatible ] )
128
+ — serialized HTML to parse
129
+ * ` options ` ([ ` Options ` ] [ options ] , optional)
130
+ — configuration
125
131
126
- Whether to parse as a fragment (` boolean ` , default: ` false ` ).
127
- The default is to expect a whole document.
128
- In document mode, unopened ` html ` , ` head ` , and ` body ` elements are opened.
132
+ ###### Returns
133
+
134
+ Tree ([ ` Root ` ] [ root ] ).
135
+
136
+ ### ` Options `
137
+
138
+ Configuration (TypeScript type).
139
+
140
+ ##### Fields
129
141
130
142
###### ` options.space `
131
143
@@ -146,19 +158,24 @@ exiting it.
146
158
###### ` options.verbose `
147
159
148
160
Add extra positional info about attributes, start tags, and end tags
149
- (` boolean ` , default: ` false ` )
161
+ (` boolean ` , default: ` false ` ).
162
+
163
+ ###### ` options.fragment `
164
+
165
+ Whether to parse as a fragment (` boolean ` , default: ` false ` ).
166
+ The default is to expect a whole document.
167
+ In document mode, unopened ` html ` , ` head ` , and ` body ` elements are opened.
150
168
151
169
###### ` options.onerror `
152
170
153
171
Function called when encountering [ HTML parse errors] [ parse-errors ]
154
- (` (error: VFileMessage) => void ` , optional).
155
- Called with a [ ` VFileMessage ` ] [ vfile-message ] .
172
+ ([ ` OnError ` ] [ onerror ] , optional).
156
173
157
- Specific rules can be turned off by setting their identifiers directly in
158
- ` options ` to ` false ` (or ` 0 ` ).
159
- The default, when ` onerror ` is passed, is ` true ` (or ` 1 ` ), and means that
160
- messages are warnings.
161
- Rules can also be configured with ` 2 ` , to turn them into fatal errors .
174
+ ###### ` options[key in ErrorCode] `
175
+
176
+ 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 .
162
179
163
180
The list of parse errors:
164
181
@@ -227,9 +244,50 @@ The list of parse errors:
227
244
228
245
<!-- parse-error end -->
229
246
230
- ##### Returns
247
+ ### ` OnError `
231
248
232
- Tree ([ ` Root ` ] [ root ] ).
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
+ ` ` `
233
291
234
292
## Examples
235
293
@@ -393,14 +451,14 @@ followed by browsers such as Chrome and Firefox.
393
451
## Types
394
452
395
453
This package is fully typed with [ TypeScript] [ ] .
396
- It exports the additional type ` Options ` , ` OnError ` , ` ErrorCode ` , and
397
- ` ErrorSeverity ` .
454
+ It exports the additional type [ ` Options ` ] [ options ] , [ ` OnError ` ] [ onerror ] ,
455
+ [ ` ErrorCode ` ] [ errorcode ] , and [ ` ErrorSeverity ` ] [ errorseverity ] .
398
456
399
457
## Compatibility
400
458
401
459
Projects maintained by the unified collective are compatible with all maintained
402
460
versions of Node.js.
403
- As of now, that is Node.js 14.14+, 16.0+, and 18 .0+.
461
+ As of now, that is Node.js 14.14+ and 16 .0+.
404
462
Our projects sometimes work with older versions, but this is not guaranteed.
405
463
406
464
## Security
@@ -494,14 +552,24 @@ abide by its terms.
494
552
495
553
[ xast-util-from-xml ] : https://github.com/syntax-tree/xast-util-from-xml
496
554
497
- [ vfile ] : https://github.com/vfile/vfile
498
-
499
- [ vfile-message ] : https://github.com/vfile/vfile-message
500
-
501
555
[ rehype-parse ] : https://github.com/rehypejs/rehype/tree/main/packages/rehype-parse#readme
502
556
503
557
[ rehype-format ] : https://github.com/rehypejs/rehype-format
504
558
505
559
[ parse5 ] : https://github.com/inikulin/parse5
506
560
507
561
[ parse-errors ] : https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
562
+
563
+ [ vfilemessage ] : https://github.com/vfile/vfile-message#vfilemessagereason-place-origin
564
+
565
+ [ fromhtml ] : #fromhtmlvalue-options
566
+
567
+ [ options ] : #options
568
+
569
+ [ onerror ] : #onerror
570
+
571
+ [ errorcode ] : #errorcode
572
+
573
+ [ errorseverity ] : #errorseverity
574
+
575
+ [ compatible ] : https://github.com/vfile/vfile/blob/03efac7/lib/index.js#L16
0 commit comments