8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- ** [ xast] [ ] ** utility to create XML * [ trees] [ tree ] * (like [ ` hastscript ` ] [ h ] for
12
- ** [ hast] [ ] ** and [ ` unist-builder ` ] [ u ] for ** [ unist] [ ] ** ).
11
+ [ xast] [ ] utility to create trees with ease.
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
+ * [ ` x(name?[, attributes][, …children]) ` ] ( #xname-attributes-children )
21
+ * [ JSX] ( #jsx )
22
+ * [ Types] ( #types )
23
+ * [ Compatibility] ( #compatibility )
24
+ * [ Security] ( #security )
25
+ * [ Related] ( #related )
26
+ * [ Contribute] ( #contribute )
27
+ * [ License] ( #license )
28
+
29
+ ## What is this?
30
+
31
+ This package is a hyperscript interface (like ` createElement ` from React and
32
+ such) to help with creating xast trees.
33
+
34
+ ## When should I use this?
15
35
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.
36
+ You can use this utility in your project when you generate xast syntax trees
37
+ with code.
38
+ It helps because it replaces most of the repetition otherwise needed in a syntax
39
+ tree with function calls.
18
40
19
- [ npm] [ ] :
41
+ You can instead use [ ` unist-builder ` ] [ u ] when creating any unist nodes and
42
+ [ ` hastscript ` ] [ h ] when creating hast (HTML) nodes.
43
+
44
+ ## Install
45
+
46
+ This package is [ ESM only] [ esm ] .
47
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
20
48
21
49
``` sh
22
50
npm install xastscript
23
51
```
24
52
53
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
54
+
55
+ ``` js
56
+ import {x } from ' https://esm.sh/xastscript@3'
57
+ ```
58
+
59
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
60
+
61
+ ``` html
62
+ <script type =" module" >
63
+ import {x } from ' https://esm.sh/xastscript@3?bundle'
64
+ </script >
65
+ ```
66
+
25
67
## Use
26
68
27
69
``` js
@@ -145,9 +187,16 @@ Yields:
145
187
146
188
## API
147
189
190
+ This package exports the identifier ` x ` .
191
+ There is no default export.
192
+
193
+ The export map supports the automatic JSX runtime.
194
+ You can pass ` xastscript ` to your build tool (TypeScript, Babel, SWC) as with
195
+ an ` importSource ` option or similar.
196
+
148
197
### ` x(name?[, attributes][, …children]) `
149
198
150
- Create XML * [ trees ] [ tree ] * in ** [ xast] [ ] ** .
199
+ Create [ xast] [ ] trees .
151
200
152
201
##### Signatures
153
202
@@ -180,20 +229,23 @@ Cannot be omitted when building an [`Element`][element] if the first child is a
180
229
(Lists of) children (` string ` , ` number ` , ` Node ` , ` Array<children> ` , optional).
181
230
When strings or numbers are encountered, they are mapped to [ ` Text ` ] [ text ]
182
231
nodes.
183
- If a [ ` Root ` ] [ root ] node is given , its children are used instead.
232
+ If a [ ` Root ` ] [ root ] node is encountered , its children are used instead.
184
233
185
234
##### Returns
186
235
187
236
[ ` Element ` ] [ element ] or [ ` Root ` ] [ root ] .
188
237
189
238
## JSX
190
239
191
- ` xastscript ` can be used as a pragma for JSX.
240
+ ` xastscript ` can be used with JSX.
241
+ Either use the automatic runtime set to ` xastscript ` or import ` x ` yourself and
242
+ define it as the pragma (plus set the fragment to ` null ` ).
243
+
192
244
The example above (omitting the second) can then be written like so:
193
245
194
246
``` jsx
247
+ /** @jsxImportSource x */
195
248
import {u } from ' unist-builder'
196
- import {x } from ' xastscript'
197
249
198
250
console .log (
199
251
< album id= {123 }>
@@ -215,17 +267,14 @@ console.log(
215
267
)
216
268
```
217
269
218
- Note that you must still import ` xastscript ` yourself and configure your
219
- JavaScript compiler to use the identifier you assign it to as a pragma (and
220
- pass ` null ` for fragments).
221
-
222
- For [ bublé] [ ] , this can be done by setting ` jsx: 'x' ` and ` jsxFragment: 'null' `
223
- (note that ` jsxFragment ` is currently only available on the API, not the CLI).
270
+ You can use [ ` estree-util-build-jsx ` ] [ estree-util-build-jsx ] to compile JSX
271
+ away.
224
272
225
- For [ Babel] [ ] , use [ ` @babel/plugin-transform-react-jsx ` ] [ babel-jsx ] (in classic
226
- mode), and pass ` pragma: 'x' ` and ` pragmaFrag: 'null' ` .
227
-
228
- Babel also lets you configure this in a script:
273
+ For [ Babel] [ ] , use [ ` @babel/plugin-transform-react-jsx ` ] [ babel-jsx ] and either
274
+ pass ` pragma: 'x' ` and ` pragmaFrag: 'null' ` , or pass `importSource:
275
+ 'xastscript'`.
276
+ Alternatively, Babel also lets you configure this with a comment:
277
+ Babel also lets you configure this from code:
229
278
230
279
``` jsx
231
280
/** @jsx x @jsxFrag null */
@@ -237,16 +286,20 @@ console.log(<music />)
237
286
For [ TypeScript] [ ] , this can be done by setting ` "jsx": "react" ` ,
238
287
` "jsxFactory": "x" ` , and ` "jsxFragmentFactory": "null" ` in the compiler options.
239
288
For more details on configuring JSX for TypeScript, see the
240
- [ TypeScript JSX handbook page] [ ] .
289
+ [ TypeScript JSX handbook page] [ typescript-jsx ] .
290
+ TypeScript also lets you configure this from code as shown with Babel above.
241
291
242
- TypeScript also lets you configure this in a script:
292
+ ## Types
243
293
244
- ``` tsx
245
- /** @jsx x @jsxFrag null */
246
- import {x } from ' xastscript'
294
+ This package is fully typed with [ TypeScript] [ ] .
295
+ It exports the additional types ` Child ` and ` Attributes ` .
247
296
248
- console .log (<music />)
249
- ```
297
+ ## Compatibility
298
+
299
+ Projects maintained by the unified collective are compatible with all maintained
300
+ versions of Node.js.
301
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
302
+ Our projects sometimes work with older versions, but this is not guaranteed.
250
303
251
304
## Security
252
305
@@ -255,20 +308,20 @@ XML can be a dangerous language: don’t trust user-provided data.
255
308
## Related
256
309
257
310
* [ ` unist-builder ` ] [ u ]
258
- — Create any unist tree
311
+ — create any unist tree
259
312
* [ ` hastscript ` ] [ h ]
260
- — Create a ** [ hast] [ ] ** (HTML or SVG) unist tree
313
+ — create a hast tree
261
314
* [ ` xast-util-to-xml ` ] ( https://github.com/syntax-tree/xast-util-to-xml )
262
- — Serialize nodes to XML
315
+ — serialize xast as XML
263
316
* [ ` xast-util-from-xml ` ] ( https://github.com/syntax-tree/xast-util-from-xml )
264
- — Parse from XML
317
+ — parse xast from XML
265
318
* [ ` hast-util-to-xast ` ] ( https://github.com/syntax-tree/hast-util-to-xast )
266
- — Transform hast (html, svg) to xast (xml)
319
+ — transform hast to xast
267
320
268
321
## Contribute
269
322
270
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
271
- started.
323
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
324
+ ways to get started.
272
325
See [ ` support.md ` ] [ support ] for ways to get help.
273
326
274
327
This project has a [ code of conduct] [ coc ] .
@@ -309,24 +362,26 @@ abide by its terms.
309
362
310
363
[ npm ] : https://docs.npmjs.com/cli/install
311
364
365
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
366
+
367
+ [ esmsh ] : https://esm.sh
368
+
369
+ [ typescript ] : https://www.typescriptlang.org
370
+
312
371
[ license ] : license
313
372
314
373
[ author ] : https://wooorm.com
315
374
316
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
375
+ [ health ] : https://github.com/syntax-tree/.github
317
376
318
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
377
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
319
378
320
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct .md
379
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support .md
321
380
322
- [ unist ] : https://github.com/syntax-tree/unist
323
-
324
- [ hast ] : https://github.com/syntax-tree/hast
381
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
325
382
326
383
[ xast ] : https://github.com/syntax-tree/xast
327
384
328
- [ tree ] : https://github.com/syntax-tree/unist#tree
329
-
330
385
[ node ] : https://github.com/syntax-tree/unist#node
331
386
332
387
[ root ] : https://github.com/syntax-tree/xast#root
@@ -339,12 +394,10 @@ abide by its terms.
339
394
340
395
[ h ] : https://github.com/syntax-tree/hastscript
341
396
342
- [ bublé ] : https://github.com/Rich-Harris/buble
343
-
344
397
[ babel ] : https://github.com/babel/babel
345
398
346
399
[ babel-jsx ] : https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-react-jsx
347
400
348
- [ typescript ] : https://www.typescriptlang.org
401
+ [ typescript-jsx ] : https://www.typescriptlang.org/docs/handbook/jsx.html
349
402
350
- [ typescript jsx handbook page ] : https://www.typescriptlang.org/docs/handbook/ jsx.html
403
+ [ estree-util-build- jsx] : https://github.com/syntax-tree/estree-util-build- jsx
0 commit comments