8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- ** [ hast] [ ] ** (HTML / SVG) utility to transform * [ trees] [ tree ] * to ** [ xast] [ ] **
12
- (XML).
11
+ [ hast] [ ] (HTML) utility to transform to [ xast] [ ] (XML).
13
12
14
- ## Install
13
+ ## Contents
14
+
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
+ * [ ` toXast(node[, space|options]) ` ] ( #toxastnode-spaceoptions )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Security] ( #security )
24
+ * [ Related] ( #related )
25
+ * [ Contribute] ( #contribute )
26
+ * [ License] ( #license )
27
+
28
+ ## What is this?
29
+
30
+ This package is a utility that takes a [ hast] [ ] (HTML) syntax tree as input and
31
+ turns it into a [ xast] [ ] (XML) syntax tree.
32
+ This package also supports embedded MDX nodes.
33
+
34
+ ## When should I use this?
35
+
36
+ This project is useful when you want to deal with ASTs, and for some reason,
37
+ * have* to deal with XML.
38
+ One example of this is for EPUB (digital books).
39
+
40
+ There is no inverse of this utility, because not all XML is HTML.
41
+
42
+ A similar package, [ ` hast-util-to-estree ` ] [ hast-util-to-estree ] , can turn
43
+ hast into estree (JavaScript) as JSX, which has some similarities to XML.
15
44
16
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
17
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
45
+ ## Install
18
46
19
- [ npm] [ ] :
47
+ This package is [ ESM only] [ esm ] .
48
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
20
49
21
50
``` sh
22
51
npm install hast-util-to-xast
23
52
```
24
53
54
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
55
+
56
+ ``` js
57
+ import {toXast } from " https://esm.sh/hast-util-to-xast@2"
58
+ ```
59
+
60
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
61
+
62
+ ``` html
63
+ <script type =" module" >
64
+ import {toXast } from " https://esm.sh/hast-util-to-xast@2?bundle"
65
+ </script >
66
+ ```
67
+
25
68
## Use
26
69
27
- Say we have an ` example.html ` file, that looks as follows :
70
+ Say our document ` example.html ` contains :
28
71
29
72
``` html
30
73
<!doctypehtml>
31
74
<title >Hello, World!</title >
32
75
<h1 >👋, 🌍</h1 >
33
76
```
34
77
35
- …and our script, ` example.js ` , looks as follows:
78
+ …and our module ` example.js ` looks as follows:
36
79
37
80
``` js
38
- import fs from ' node:fs'
81
+ import fs from ' node:fs/promises '
39
82
import {unified } from ' unified'
40
83
import rehypeParse from ' rehype-parse'
41
84
import {toXast } from ' hast-util-to-xast'
@@ -44,7 +87,7 @@ import {toXml} from 'xast-util-to-xml'
44
87
// Get the HTML syntax tree:
45
88
const hast = unified ()
46
89
.use (rehypeParse)
47
- .parse (fs .readFileSync (' example.html' ))
90
+ .parse (await fs .readFile (' example.html' ))
48
91
49
92
// Turn hast to xast:
50
93
const xast = toXast (hast)
@@ -53,7 +96,7 @@ const xast = toXast(hast)
53
96
console .log (toXml (xast))
54
97
```
55
98
56
- Yields :
99
+ …now running ` node example.js ` yields :
57
100
58
101
``` xml
59
102
<!DOCTYPE html ><html xmlns =" http://www.w3.org/1999/xhtml" ><head ><title >Hello, World!</title >
@@ -63,51 +106,59 @@ Yields:
63
106
64
107
## API
65
108
66
- This package exports the following identifiers: ` toXast ` .
109
+ This package exports the identifier ` toXast ` .
67
110
There is no default export.
68
111
69
112
### ` toXast(node[, space|options]) `
70
113
71
- Transform the given ** [ hast] [ ] ** * [ tree] [ ] * to ** [ xast] [ ] ** .
72
-
73
- ##### ` space `
74
-
75
- Treated as ` options.space ` .
114
+ [ hast] [ ] utility to transform to [ xast] [ ] .
76
115
77
116
##### ` options `
78
117
118
+ Configuration (optional).
119
+
79
120
###### ` options.space `
80
121
81
- Whether the [ * root * ] [ root ] of the [ * tree * ] [ tree ] is in the ` 'html '` or ` 'svg' `
82
- space (enum, ` 'svg' ` or ` 'html' ` , default: ` 'html' ` ).
122
+ Whether the given ` node ` is in the HTML or SVG space (enum, ` 'svg '` or ` 'html' ` ,
123
+ default: ` 'html' ` ).
83
124
84
125
If an ` svg ` element is found in the HTML space, ` toXast ` automatically switches
85
126
to the SVG space when entering the element, and switches back when exiting.
86
127
87
128
You can also switch explicitly with ` xmlns ` properties in hast, but note that
88
129
only HTML and SVG are supported.
89
130
131
+ ## Types
132
+
133
+ This package is fully typed with [ TypeScript] [ ] .
134
+ It exports the additional types ` Options ` and ` Space ` .
135
+
136
+ ## Compatibility
137
+
138
+ Projects maintained by the unified collective are compatible with all maintained
139
+ versions of Node.js.
140
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
141
+ Our projects sometimes work with older versions, but this is not guaranteed.
142
+
90
143
## Security
91
144
92
- Both HTML and XML can be a dangerous language : don’t trust user-provided data.
93
- Use [ ` hast-util-santize ` ] [ sanitize ] to make the hast tree safe before using this
94
- utility.
145
+ Both HTML and XML can be dangerous languages : don’t trust user-provided data.
146
+ Use [ ` hast-util-santize ` ] [ hast-util- sanitize] to make the hast tree safe before
147
+ using this utility.
95
148
96
149
## Related
97
150
98
- * [ ` unist-builder ` ] [ u ]
99
- — Create any unist tree
100
151
* [ ` hastscript ` ] [ h ]
101
- — Create a ** [ hast] [ ] ** (HTML or SVG) tree
152
+ — create ** [ hast] [ ] ** (HTML or SVG) trees
102
153
* [ ` xastscript ` ] [ x ]
103
- — Create a ** [ xast] [ ] ** (XML) tree
154
+ — create ** [ xast] [ ] ** (XML) trees
104
155
* [ ` xast-util-to-xml ` ] ( https://github.com/syntax-tree/xast-util-to-xml )
105
- — Serialize nodes to XML
156
+ — serialize as XML
106
157
107
158
## Contribute
108
159
109
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
110
- started.
160
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
161
+ ways to get started.
111
162
See [ ` support.md ` ] [ support ] for ways to get help.
112
163
113
164
This project has a [ code of conduct] [ coc ] .
@@ -148,28 +199,32 @@ abide by its terms.
148
199
149
200
[ npm ] : https://docs.npmjs.com/cli/install
150
201
202
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
203
+
204
+ [ esmsh ] : https://esm.sh
205
+
206
+ [ typescript ] : https://www.typescriptlang.org
207
+
151
208
[ license ] : license
152
209
153
210
[ author ] : https://wooorm.com
154
211
155
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
212
+ [ health ] : https://github.com/syntax-tree/.github
213
+
214
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
156
215
157
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
216
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
158
217
159
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD /code-of-conduct.md
218
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main /code-of-conduct.md
160
219
161
220
[ hast ] : https://github.com/syntax-tree/hast
162
221
163
222
[ xast ] : https://github.com/syntax-tree/xast
164
223
165
- [ tree ] : https://github.com/syntax-tree/unist#tree
166
-
167
- [ root ] : https://github.com/syntax-tree/unist#root
168
-
169
- [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
170
-
171
- [ u ] : https://github.com/syntax-tree/unist-builder
224
+ [ hast-util-sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
172
225
173
226
[ h ] : https://github.com/syntax-tree/hastscript
174
227
175
228
[ x ] : https://github.com/syntax-tree/xastscript
229
+
230
+ [ hast-util-to-estree ] : https://github.com/syntax-tree/hast-util-to-estree
0 commit comments