6
6
[ ![ Size] [ badge-size-image ]] [ badge-size-url ]
7
7
8
8
[ hast] [ github-hast ] utility to parse the tree and semistandard ` raw ` nodes
9
- (strings of HTML) again, keeping positional info okay.
9
+ (strings of HTML) again,
10
+ keeping positional info okay.
10
11
11
12
## Contents
12
13
28
29
29
30
This package is a utility to parse a document again.
30
31
It passes each node and embedded raw HTML through an HTML parser
31
- ([ ` parse5 ` ] [ github-parse5 ] ), to recreate a tree exactly as how a browser would
32
- parse it, while keeping the original data and positional info intact.
32
+ ([ ` parse5 ` ] [ github-parse5 ] ),
33
+ to recreate a tree exactly as how a browser would parse it,
34
+ while keeping the original data and positional info intact.
33
35
34
36
## When should I use this?
35
37
36
38
This utility is particularly useful when coming from markdown and wanting to
37
- support HTML embedded inside that markdown (which requires passing
39
+ support HTML embedded inside that markdown
40
+ (which requires passing
38
41
` allowDangerousHtml: true ` to
39
42
[ ` mdast-util-to-hast ` ] [ github-mdast-util-to-hast ] ).
40
- Markdown dictates how, say, a list item or emphasis can be parsed.
43
+ Markdown dictates how,
44
+ say,
45
+ a list item or emphasis can be parsed.
41
46
We can use that to turn the markdown syntax tree into an HTML syntax tree.
42
- But markdown also dictates that things that look like HTML, are passed through
43
- untouched, even when it just looks like XML but doesn’t really make sense, so we
44
- can’t normally use these strings of “HTML” to create an HTML syntax tree.
47
+ But markdown also dictates that things that look like HTML,
48
+ are passed through untouched,
49
+ even when it just looks like XML but doesn’t really make sense,
50
+ so we can’t normally use these strings of “HTML” to create an HTML syntax tree.
45
51
This utility can.
46
52
It can be used to take those strings of HTML and include them into the syntax
47
53
tree as actual nodes.
48
54
49
- If your final result is HTML and you trust content, then “strings” are fine
50
- (you can pass ` allowDangerousHtml: true ` to ` hast-util-to-html ` , which passes
51
- HTML through untouched).
55
+ If your final result is HTML and you trust content,
56
+ then “strings” are fine
57
+ (you can pass ` allowDangerousHtml: true ` to ` hast-util-to-html ` ,
58
+ which passes HTML through untouched).
52
59
But there are two main cases where a proper syntax tree is preferred:
53
60
54
61
* hast utilities need a proper syntax tree as they operate on actual nodes to
55
- inspect or transform things, they can’t operate on strings of HTML
56
- * other output formats (React, MDX, etc) need actual nodes and can’t handle
57
- strings of HTML
62
+ inspect or transform things,
63
+ they can’t operate on strings of HTML
64
+ * other output formats
65
+ (React, MDX, etc)
66
+ need actual nodes and can’t handle strings of HTML
58
67
59
68
The plugin [ ` rehype-raw ` ] [ github-rehype-raw ] wraps this utility at a
60
69
higher-level (easier) abstraction.
61
70
62
71
## Install
63
72
64
73
This package is [ ESM only] [ github-gist-esm ] .
65
- In Node.js (version 16+), install with [ npm] [ npmjs-install ] :
74
+ In Node.js (version 16+),
75
+ install with [ npm] [ npmjs-install ] :
66
76
67
77
``` sh
68
78
npm install hast-util-raw
@@ -150,8 +160,9 @@ Configuration.
150
160
151
161
### ` raw(tree, options) `
152
162
153
- Pass a hast tree through an HTML parser, which will fix nesting, and turn
154
- raw nodes into actual nodes.
163
+ Pass a hast tree through an HTML parser,
164
+ which will fix nesting,
165
+ and turn raw nodes into actual nodes.
155
166
156
167
###### Parameters
157
168
@@ -177,9 +188,10 @@ The `Raw` node type is registered by and exposed from
177
188
Projects maintained by the unified collective are compatible with maintained
178
189
versions of Node.js.
179
190
180
- When we cut a new major release, we drop support for unmaintained versions of
181
- Node.
182
- This means we try to keep the current release line, ` hast-util-raw@^9 ` ,
191
+ When we cut a new major release,
192
+ we drop support for unmaintained versions of Node.
193
+ This means we try to keep the current release line,
194
+ ` hast-util-raw@9 ` ,
183
195
compatible with Node.js 16.
184
196
185
197
## Security
@@ -191,7 +203,7 @@ The following example shows how a raw node is used to inject a script that runs
191
203
when loaded in a browser.
192
204
193
205
``` js
194
- raw (u ( ' root' , [ u ( ' raw' , ' <script>alert(1)</script>' )]) )
206
+ raw ({type : ' root' , children : [{type : ' raw' , value : ' <script>alert(1)</script>' }]} )
195
207
```
196
208
197
209
Yields:
@@ -200,8 +212,8 @@ Yields:
200
212
<script >alert (1 ) </script >
201
213
```
202
214
203
- Either do not use this utility in combination with user input, or use
204
- [ ` hast-util-santize ` ] [ github-hast-util-sanitize ] .
215
+ Either do not use this utility in combination with user input,
216
+ or use [ ` hast-util-santize ` ] [ github-hast-util-sanitize ] .
205
217
206
218
## Related
207
219
@@ -217,8 +229,9 @@ for ways to get started.
217
229
See [ ` support.md ` ] [ health-support ] for ways to get help.
218
230
219
231
This project has a [ code of conduct] [ health-coc ] .
220
- By interacting with this repository, organization, or community you agree to
221
- abide by its terms.
232
+ By interacting with this repository,
233
+ organization,
234
+ or community you agree to abide by its terms.
222
235
223
236
## License
224
237
0 commit comments