|
8 | 8 | [![Backers][backers-badge]][collective]
|
9 | 9 | [![Chat][chat-badge]][chat]
|
10 | 10 |
|
11 |
| -[**hast**][hast] utility to transform to a DOM tree. |
| 11 | +[hast][] utility to transform to a [DOM][] tree. |
12 | 12 |
|
13 |
| -## Install |
| 13 | +## Contents |
14 | 14 |
|
15 |
| -This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): |
16 |
| -Node 12+ is needed to use it and it must be `import`ed instead of `require`d. |
| 15 | +* [What is this?](#what-is-this) |
| 16 | +* [When should I use this?](#when-should-i-use-this) |
| 17 | +* [Install](#install) |
| 18 | +* [Use](#use) |
| 19 | +* [API](#api) |
| 20 | + * [`toDom(node[, options])`](#todomnode-options) |
| 21 | +* [Types](#types) |
| 22 | +* [Compatibility](#compatibility) |
| 23 | +* [Security](#security) |
| 24 | +* [Related](#related) |
| 25 | +* [Contribute](#contribute) |
| 26 | +* [License](#license) |
17 | 27 |
|
18 |
| -[npm][]: |
| 28 | +## What is this? |
19 | 29 |
|
20 |
| -```sh |
21 |
| -npm install hast-util-to-dom |
22 |
| -``` |
| 30 | +This package is a utility that creates a DOM tree (defaulting to the actual DOM |
| 31 | +but also supporting things like [`jsdom`][jsdom]) from a [hast][] (HTML) syntax |
| 32 | +tree. |
23 | 33 |
|
24 |
| -## Use |
| 34 | +## When should I use this? |
25 | 35 |
|
26 |
| -This utility is intended for browser use! |
| 36 | +You can use this project when you want to turn hast into a DOM in browsers, |
| 37 | +either to use it directly on a page, or to enable the use of DOM APIs (such as |
| 38 | +`querySelector` to find things or `innerHTML` to serialize stuff). |
27 | 39 |
|
28 |
| -```js |
29 |
| -import {toDom} from 'hast-util-to-dom'; |
| 40 | +The hast utility [`hast-util-from-dom`][hast-util-from-dom] does the inverse of |
| 41 | +this utility. |
| 42 | +It turns DOM trees into hast. |
30 | 43 |
|
31 |
| -const el = toDom({ |
32 |
| - type: 'element', |
33 |
| - tagName: 'h1', |
34 |
| - properties: {}, |
35 |
| - children: [{type: 'text', value: 'World!'}] |
36 |
| -}); |
| 44 | +The rehype plugin [`rehype-dom-stringify`][rehype-dom-stringify] wraps this |
| 45 | +utility to serialize as HTML with DOM APIs. |
37 | 46 |
|
38 |
| -console.log(el); |
39 |
| -``` |
| 47 | +## Install |
40 | 48 |
|
41 |
| -This will create a DOM node like this: |
| 49 | +This package is [ESM only][esm]. |
| 50 | +In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: |
42 | 51 |
|
43 |
| -```html |
44 |
| -<h1>World!</h1> |
| 52 | +```sh |
| 53 | +npm install hast-util-from-dom |
45 | 54 | ```
|
46 | 55 |
|
47 |
| -If you want a string of HTML, you have a few options: |
| 56 | +In Deno with [`esm.sh`][esmsh]: |
48 | 57 |
|
49 | 58 | ```js
|
50 |
| -// Outer HTML, eg. if you want an entire fragment |
51 |
| -console.log(el.outerHTML); |
52 |
| -// "<h1>World</h1>" |
| 59 | +import {toDom} from 'https://esm.sh/hast-util-to-dom@3' |
| 60 | +``` |
| 61 | + |
| 62 | +In browsers with [`esm.sh`][esmsh]: |
| 63 | + |
| 64 | +```html |
| 65 | +<script type="module"> |
| 66 | + import {toDom} from 'https://esm.sh/hast-util-to-dom@3?bundle' |
| 67 | +</script> |
| 68 | +``` |
| 69 | + |
| 70 | +## Use |
53 | 71 |
|
54 |
| -// Inner HTML, eg. if you have a wrapping element you don't need |
55 |
| -console.log(el.innerHTML); |
56 |
| -// "World" |
| 72 | +Say our page `example.html` looks as follows: |
57 | 73 |
|
58 |
| -// Full serialization, eg. when you want the whole document |
59 |
| -console.log(new XMLSerializer().serializeToString(el)); |
60 |
| -// "<div xmlns="http://www.w3.org/1999/xhtml">World</div>" |
| 74 | +```html |
| 75 | +<!doctype html> |
| 76 | +<title>Example</title> |
| 77 | +<body> |
| 78 | + <script type="module"> |
| 79 | + import {h} from 'https://esm.sh/hastscript?bundle' |
| 80 | + import {toDom} from 'https://esm.sh/hast-util-to-dom?bundle' |
| 81 | +
|
| 82 | + const tree = h('main', [ |
| 83 | + h('h1', 'Hi'), |
| 84 | + h('p', [h('em', 'Hello'), ', world!']) |
| 85 | + ]) |
| 86 | +
|
| 87 | + document.body.append(toDom(tree)) |
| 88 | + </script> |
61 | 89 | ```
|
62 | 90 |
|
63 |
| -Due to the nature of various browser implementations, you may notice |
64 |
| -cross-browser differences in the serialized output, especially with respect to |
65 |
| -whitespace or self-closing tags. |
66 |
| -Buddy, that’s the web! |
| 91 | +Now running `open example.html` shows the equivalent HTML on the page. |
67 | 92 |
|
68 | 93 | ## API
|
69 | 94 |
|
70 |
| -This package exports the following identifiers: `toDom`. |
| 95 | +This package exports the identifier `toDom`. |
71 | 96 | There is no default export.
|
72 | 97 |
|
73 | 98 | ### `toDom(node[, options])`
|
74 | 99 |
|
75 |
| -Transform a [**hast**][hast] [*tree*][tree] to a DOM tree. |
| 100 | +Turn a hast tree into a DOM tree. |
76 | 101 |
|
77 | 102 | ##### `options`
|
78 | 103 |
|
| 104 | +Configuration (optional). |
| 105 | + |
79 | 106 | ###### `options.fragment`
|
80 | 107 |
|
81 |
| -Whether a DOM fragment should be returned (default: `false`). |
| 108 | +Return a DOM fragment (`boolean`, default: `false`). |
| 109 | +Creates whole documents otherwise. |
82 | 110 |
|
83 | 111 | ###### `options.document`
|
84 | 112 |
|
85 |
| -Document interface to use (default: `globalThis.document`). |
| 113 | +Document interface to use (`Document`, default: `globalThis.document`). |
86 | 114 |
|
87 | 115 | ###### `options.namespace`
|
88 | 116 |
|
89 |
| -`namespace` to use to create [*elements*][element]. |
| 117 | +`namespace` to use to create elements (`string?`, optional). |
90 | 118 |
|
91 | 119 | ###### `options.afterTransform`
|
92 | 120 |
|
93 |
| -Function called after a hast node is transformed into a DOM node (`Function?`). |
94 |
| -Given the hast node that was handled as the first parameter and the |
95 |
| -corresponding DOM node as the second parameter. |
| 121 | +Called when a hast node was transformed into a DOM node |
| 122 | +(`(HastNode, Node) => void?`, optional). |
| 123 | + |
| 124 | +##### Returns |
| 125 | + |
| 126 | +[`Node`][dom]. |
| 127 | + |
| 128 | +## Types |
| 129 | + |
| 130 | +This package is fully typed with [TypeScript][]. |
| 131 | +It exports the additional type `Options`. |
| 132 | + |
| 133 | +## Compatibility |
| 134 | + |
| 135 | +Projects maintained by the unified collective are compatible with all maintained |
| 136 | +versions of Node.js. |
| 137 | +As of now, that is Node.js 12.20+, 14.14+, and 16.0+. |
| 138 | +Our projects sometimes work with older versions, but this is not guaranteed. |
96 | 139 |
|
97 | 140 | ## Security
|
98 | 141 |
|
99 | 142 | Use of `hast-util-to-dom` can open you up to a
|
100 | 143 | [cross-site scripting (XSS)][xss] attack if the hast tree is unsafe.
|
101 |
| -Use [`hast-util-santize`][sanitize] to make the hast tree safe. |
| 144 | +Use [`hast-util-santize`][hast-util-sanitize] to make the hast tree safe. |
102 | 145 |
|
103 | 146 | ## Related
|
104 | 147 |
|
105 | 148 | * [`hast-util-sanitize`](https://github.com/syntax-tree/hast-util-sanitize)
|
106 |
| - — Sanitize hast nodes |
| 149 | + — sanitize hast nodes |
107 | 150 | * [`hast-util-to-html`](https://github.com/syntax-tree/hast-util-to-html)
|
108 |
| - — Create an HTML string |
| 151 | + — serialize as HTML |
109 | 152 | * [`hast-util-from-dom`](https://github.com/syntax-tree/hast-util-from-dom)
|
110 |
| - — Create a hast tree from a DOM tree |
| 153 | + — create a hast tree from a DOM tree |
111 | 154 |
|
112 | 155 | ## Contribute
|
113 | 156 |
|
114 |
| -See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get |
115 |
| -started. |
| 157 | +See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for |
| 158 | +ways to get started. |
116 | 159 | See [`support.md`][support] for ways to get help.
|
117 | 160 |
|
118 | 161 | This project has a [code of conduct][coc].
|
119 |
| -By interacting with this repository, organization, or community you agree to |
| 162 | +By interacting with this repository, organisation, or community you agree to |
120 | 163 | abide by its terms.
|
121 | 164 |
|
122 | 165 | ## License
|
@@ -153,22 +196,34 @@ abide by its terms.
|
153 | 196 |
|
154 | 197 | [npm]: https://docs.npmjs.com/cli/install
|
155 | 198 |
|
| 199 | +[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c |
| 200 | + |
| 201 | +[esmsh]: https://esm.sh |
| 202 | + |
| 203 | +[typescript]: https://www.typescriptlang.org |
| 204 | + |
156 | 205 | [license]: license
|
157 | 206 |
|
158 | 207 | [author]: https://keith.mcknig.ht
|
159 | 208 |
|
160 |
| -[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md |
| 209 | +[health]: https://github.com/syntax-tree/.github |
161 | 210 |
|
162 |
| -[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md |
| 211 | +[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md |
163 | 212 |
|
164 |
| -[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md |
| 213 | +[support]: https://github.com/syntax-tree/.github/blob/main/support.md |
165 | 214 |
|
166 |
| -[hast]: https://github.com/syntax-tree/hast |
| 215 | +[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md |
167 | 216 |
|
168 |
| -[element]: https://github.com/syntax-tree/hast#element |
| 217 | +[hast]: https://github.com/syntax-tree/hast |
169 | 218 |
|
170 |
| -[tree]: https://github.com/syntax-tree/unist#tree |
| 219 | +[dom]: https://developer.mozilla.org/docs/Web/API/Document_Object_Model |
171 | 220 |
|
172 | 221 | [xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
|
173 | 222 |
|
174 |
| -[sanitize]: https://github.com/syntax-tree/hast-util-sanitize |
| 223 | +[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize |
| 224 | + |
| 225 | +[hast-util-from-dom]: https://github.com/syntax-tree/hast-util-from-dom |
| 226 | + |
| 227 | +[jsdom]: https://github.com/jsdom/jsdom |
| 228 | + |
| 229 | +[rehype-dom-stringify]: https://github.com/rehypejs/rehype-dom/tree/main/packages/rehype-dom-stringify |
0 commit comments