18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
20
* [ ` x(name?[, attributes][, …children]) ` ] ( #xname-attributes-children )
21
+ * [ ` Attributes ` ] ( #attributes-1 )
22
+ * [ ` Child ` ] ( #child )
23
+ * [ ` Result ` ] ( #result )
24
+ * [ Syntax tree] ( #syntax-tree )
21
25
* [ JSX] ( #jsx )
22
26
* [ Types] ( #types )
23
27
* [ Compatibility] ( #compatibility )
@@ -44,7 +48,7 @@ You can instead use [`unist-builder`][u] when creating any unist nodes and
44
48
## Install
45
49
46
50
This package is [ ESM only] [ esm ] .
47
- In Node.js (version 12.20+, 14.14+, 16.0+, 18 .0+), install with [ npm] [ ] :
51
+ In Node.js (version 14.14+ and 16 .0+), install with [ npm] [ ] :
48
52
49
53
``` sh
50
54
npm install xastscript
@@ -187,12 +191,12 @@ Yields:
187
191
188
192
## API
189
193
190
- This package exports the identifier ` x ` .
194
+ This package exports the identifier [ ` x ` ] [ x ] .
191
195
There is no default export.
192
196
193
197
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.
198
+ You can pass ` xastscript ` to your build tool (TypeScript, Babel, SWC) with an
199
+ ` importSource ` option or similar.
196
200
197
201
### ` x(name?[, attributes][, …children]) `
198
202
@@ -216,36 +220,81 @@ When nullish, a [`Root`][root] is built instead.
216
220
217
221
###### ` attributes `
218
222
219
- Map of attributes (` Record<string, string | number | boolean | null | undefined> ` ,
220
- optional).
223
+ Attributes of the element ([ ` Attributes ` ] [ attributes ] , optional).
224
+
225
+ ###### ` children `
226
+
227
+ Children of the node ([ ` Child ` ] [ child ] or ` Array<Child> ` , optional).
228
+
229
+ ##### Returns
230
+
231
+ Created tree ([ ` Result ` ] [ result ] ).
232
+
233
+ [ ` Element ` ] [ element ] when a ` name ` is passed, otherwise [ ` Root ` ] [ root ] .
234
+
235
+ ### ` Attributes `
236
+
237
+ Map of attributes (TypeScript type).
238
+
221
239
Nullish (` null ` or ` undefined ` ) or ` NaN ` values are ignored, other values are
222
240
turned to strings.
223
241
224
- Cannot be given if building a [ ` Root ` ] [ root ] .
225
- Cannot be omitted when building an [ ` Element ` ] [ element ] if the first child is a
226
- [ ` Node ` ] [ node ] .
242
+ ###### Type
227
243
228
- ###### ` children `
244
+ ``` ts
245
+ type Attributes = Record <string , string | number | boolean | null | undefined >
246
+ ` ` `
229
247
230
- (Lists of) children (` string ` , ` number ` , ` Node ` , ` Array<children> ` , optional).
231
- When strings or numbers are encountered, they are mapped to [ ` Text ` ] [ text ]
248
+ ### ` Child `
249
+
250
+ (Lists of) children (TypeScript type).
251
+
252
+ When strings or numbers are encountered, they are turned into [ ` Text ` ][text]
232
253
nodes.
233
- If a [ ` Root ` ] [ root ] node is encountered, its children are used instead.
254
+ [ ` Root ` ][root] nodes are treated as “fragments”, meaning that their children
255
+ are used instead.
256
+
257
+ ###### Type
258
+
259
+ ` ` ` ts
260
+ type Child =
261
+ | string
262
+ | number
263
+ | null
264
+ | undefined
265
+ | Node
266
+ | Array <string | number | null | undefined | Node >
267
+ ` ` `
234
268
235
- ##### Returns
269
+ ### ` Result `
236
270
237
- [ ` Element ` ] [ element ] or [ ` Root ` ] [ root ] .
271
+ Result from a ` x ` call (TypeScript type).
272
+
273
+ ###### Type
274
+
275
+ ` ` ` ts
276
+ type Result = Root | Element
277
+ ` ` `
278
+
279
+ ## Syntax tree
280
+
281
+ The syntax tree is [xast][].
238
282
239
283
## JSX
240
284
241
- ` xastscript ` can be used with JSX.
242
- Either use the automatic runtime set to ` xastscript ` or import ` x ` yourself and
243
- define it as the pragma (plus set the fragment to ` null ` ).
285
+ This package can be used with JSX.
286
+ You should use the automatic JSX runtime set to ` xastscript ` .
287
+
288
+ > 🪦 **Legacy**: you can also use the classic JSX runtime, but this is not
289
+ > recommended.
290
+ > To do so, import ` x ` yourself and define it as the pragma (plus set the
291
+ > fragment to ` null ` ).
244
292
245
- The example above (omitting the second) can then be written like so:
293
+ The Use example above (omitting the second) can then be written like so:
246
294
247
295
` ` ` jsx
248
296
/** @jsxImportSource x */
297
+
249
298
import {u } from ' unist-builder'
250
299
251
300
console .log (
@@ -268,38 +317,17 @@ console.log(
268
317
)
269
318
```
270
319
271
- You can use [ ` estree-util-build-jsx ` ] [ estree-util-build-jsx ] to compile JSX
272
- away.
273
-
274
- For [ Babel] [ ] , use [ ` @babel/plugin-transform-react-jsx ` ] [ babel-jsx ] and either
275
- pass ` pragma: 'x' ` and ` pragmaFrag: 'null' ` , or pass `importSource:
276
- 'xastscript'`.
277
- Alternatively, Babel also lets you configure this with a comment:
278
- Babel also lets you configure this from code:
279
-
280
- ``` jsx
281
- /** @jsx x @jsxFrag null */
282
- import {x } from ' xastscript'
283
-
284
- console .log (< music / > )
285
- ```
286
-
287
- For [ TypeScript] [ ] , this can be done by setting ` "jsx": "react" ` ,
288
- ` "jsxFactory": "x" ` , and ` "jsxFragmentFactory": "null" ` in the compiler options.
289
- For more details on configuring JSX for TypeScript, see the
290
- [ TypeScript JSX handbook page] [ typescript-jsx ] .
291
- TypeScript also lets you configure this from code as shown with Babel above.
292
-
293
320
## Types
294
321
295
322
This package is fully typed with [ TypeScript] [ ] .
296
- It exports the additional types ` Child ` and ` Attributes ` .
323
+ It exports the additional types [ ` Attributes ` ] [ attributes ] , [ ` Child ` ] [ child ] ,
324
+ and [ ` Result ` ] [ result ] .
297
325
298
326
## Compatibility
299
327
300
328
Projects maintained by the unified collective are compatible with all maintained
301
329
versions of Node.js.
302
- As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18 .0+.
330
+ As of now, that is Node.js 14.14+ and 16 .0+.
303
331
Our projects sometimes work with older versions, but this is not guaranteed.
304
332
305
333
## Security
@@ -383,8 +411,6 @@ abide by its terms.
383
411
384
412
[ xast ] : https://github.com/syntax-tree/xast
385
413
386
- [ node ] : https://github.com/syntax-tree/unist#node
387
-
388
414
[ root ] : https://github.com/syntax-tree/xast#root
389
415
390
416
[ element ] : https://github.com/syntax-tree/xast#element
@@ -395,10 +421,10 @@ abide by its terms.
395
421
396
422
[ h ] : https://github.com/syntax-tree/hastscript
397
423
398
- [ babel ] : https://github.com/babel/babel
424
+ [ x ] : #xname-attributes-children
399
425
400
- [ babel-jsx ] : https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-react-jsx
426
+ [ attributes ] : #attributes-1
401
427
402
- [ typescript-jsx ] : https://www.typescriptlang.org/docs/handbook/jsx.html
428
+ [ child ] : #child
403
429
404
- [ estree-util-build-jsx ] : https://github.com/syntax-tree/estree-util-build-jsx
430
+ [ result ] : #result
0 commit comments