66[ ![ Size] [ badge-size-image ]] [ badge-size-url ]
77
88[ 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.
1011
1112## Contents
1213
2829
2930This package is a utility to parse a document again.
3031It 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.
3335
3436## When should I use this?
3537
3638This 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
3841` allowDangerousHtml: true ` to
3942[ ` 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.
4146We 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.
4551This utility can.
4652It can be used to take those strings of HTML and include them into the syntax
4753tree as actual nodes.
4854
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).
5259But there are two main cases where a proper syntax tree is preferred:
5360
5461* 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
5867
5968The plugin [ ` rehype-raw ` ] [ github-rehype-raw ] wraps this utility at a
6069higher-level (easier) abstraction.
6170
6271## Install
6372
6473This 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 ] :
6676
6777``` sh
6878npm install hast-util-raw
@@ -150,8 +160,9 @@ Configuration.
150160
151161### ` raw(tree, options) `
152162
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.
155166
156167###### Parameters
157168
@@ -177,9 +188,10 @@ The `Raw` node type is registered by and exposed from
177188Projects maintained by the unified collective are compatible with maintained
178189versions of Node.js.
179190
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 ` ,
183195compatible with Node.js 16.
184196
185197## Security
@@ -191,7 +203,7 @@ The following example shows how a raw node is used to inject a script that runs
191203when loaded in a browser.
192204
193205``` js
194- raw (u ( ' root' , [ u ( ' raw' , ' <script>alert(1)</script>' )]) )
206+ raw ({type : ' root' , children : [{type : ' raw' , value : ' <script>alert(1)</script>' }]} )
195207```
196208
197209Yields:
@@ -200,8 +212,8 @@ Yields:
200212<script >alert (1 ) </script >
201213```
202214
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 ] .
205217
206218## Related
207219
@@ -217,8 +229,9 @@ for ways to get started.
217229See [ ` support.md ` ] [ health-support ] for ways to get help.
218230
219231This 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.
222235
223236## License
224237
0 commit comments