Skip to content

Commit ad15a52

Browse files
committed
Refactor docs
1 parent 7cede5c commit ad15a52

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

lib/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
/**
2323
* @typedef Stitch
24-
* Custom comment-like value we pass through parse5, which contains a
25-
* replacement node that we’ll swap back in afterwards.
24+
* Custom comment-like value we pass through parse5,
25+
* which contains a replacement node that we’ll swap back in afterwards.
2626
* @property {'comment'} type
2727
* Node type.
2828
* @property {{stitch: Nodes}} value
@@ -56,8 +56,9 @@ const knownMdxNames = new Set([
5656
const parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
5757

5858
/**
59-
* Pass a hast tree through an HTML parser, which will fix nesting, and turn
60-
* raw nodes into actual nodes.
59+
* Pass a hast tree through an HTML parser,
60+
* which will fix nesting,
61+
* and turn raw nodes into actual nodes.
6162
*
6263
* @param {Nodes} tree
6364
* Original hast tree to transform.

readme.md

+38-25
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
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

@@ -28,41 +29,50 @@
2829

2930
This package is a utility to parse a document again.
3031
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.
3335

3436
## When should I use this?
3537

3638
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
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.
4146
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.
4551
This utility can.
4652
It can be used to take those strings of HTML and include them into the syntax
4753
tree 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).
5259
But 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

5968
The plugin [`rehype-raw`][github-rehype-raw] wraps this utility at a
6069
higher-level (easier) abstraction.
6170

6271
## Install
6372

6473
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]:
6676

6777
```sh
6878
npm 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
177188
Projects maintained by the unified collective are compatible with maintained
178189
versions 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`,
183195
compatible 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
191203
when 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

197209
Yields:
@@ -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.
217229
See [`support.md`][health-support] for ways to get help.
218230

219231
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.
222235

223236
## License
224237

0 commit comments

Comments
 (0)