@@ -48,7 +48,7 @@ console.log(
48
48
// For other xast nodes, such as comments, instructions, doctypes, or cdata
49
49
// can be created with unist-builder:
50
50
console .log (
51
- u ( ' root ' , [
51
+ x ( null , [
52
52
u (' instruction' , {name: ' xml' }, ' version="1.0" encoding="UTF-8"' ),
53
53
x (' album' , [
54
54
u (' comment' , ' Great album!' ),
@@ -142,33 +142,94 @@ Yields:
142
142
143
143
## API
144
144
145
- ### ` x(name[, attributes][, …children]) `
145
+ ### ` x(name? [, attributes][, …children]) `
146
146
147
147
Create XML * [ trees] [ tree ] * in ** [ xast] [ ] ** .
148
148
149
+ ##### Signatures
150
+
151
+ * ` x(): root `
152
+ * ` x(null[, …children]): root `
153
+ * ` x(name[, attributes][, …children]): element `
154
+
149
155
##### Parameters
150
156
151
157
###### ` name `
152
158
153
- Qualified name (` string ` ).
159
+ Qualified name (` string ` , optional ).
154
160
Case sensitive and can contain a namespace prefix (such as ` rdf:RDF ` ).
161
+ When string, an [ ` Element ` ] [ element ] is built.
162
+ When nullish, a [ ` Root ` ] [ root ] is built instead.
155
163
156
164
###### ` attributes `
157
165
158
166
Map of attributes (` Object.<*> ` , optional).
159
167
Nullish (` null ` or ` undefined ` ) or ` NaN ` values are ignored, other values are
160
168
turned to strings.
161
169
162
- Cannot be omitted if ` children ` is a ` Node ` .
170
+ Cannot be given if building a [ ` Root ` ] [ root ] .
171
+ Cannot be omitted when building an [ ` Element ` ] [ element ] if the first child is a
172
+ [ ` Node ` ] [ node ] .
163
173
164
174
###### ` children `
165
175
166
- (Lists of) child nodes (` string ` , ` Node ` , ` Array.<children> ` , optional).
167
- When strings are encountered, they are mapped to [ ` text ` ] [ text ] nodes.
176
+ (Lists of) children (` string ` , ` number ` , ` Node ` , ` Array.<children> ` , optional).
177
+ When strings or numbers are encountered, they are mapped to [ ` Text ` ] [ text ]
178
+ nodes.
179
+ If a [ ` Root ` ] [ root ] node is given, its children are used instead.
168
180
169
181
##### Returns
170
182
171
- [ ` Element ` ] [ element ] .
183
+ [ ` Element ` ] [ element ] or [ ` Root ` ] [ root ] .
184
+
185
+ ## JSX
186
+
187
+ ` xastscript ` can be used as a pragma for JSX.
188
+ The example above (omitting the second) can then be written like so:
189
+
190
+ ``` jsx
191
+ var u = require (' unist-builder' )
192
+ var x = require (' xastscript' )
193
+
194
+ console .log (
195
+ < album id= {123 }>
196
+ < name> Born in the U .S .A .< / name>
197
+ < artist> Bruce Springsteen< / artist>
198
+ < releasedate> 1984 - 04 - 06 < / releasedate>
199
+ < / album>
200
+ )
201
+
202
+ console .log (
203
+ <>
204
+ {u (' instruction' , {name: ' xml' }, ' version="1.0" encoding="UTF-8"' )}
205
+ < album>
206
+ {u (' comment' , ' Great album!' )}
207
+ < name> Born in the U .S .A .< / name>
208
+ < description> {u (' cdata' , ' 3 < 5 & 8 > 13' )}< / description>
209
+ < / album>
210
+ < / >
211
+ )
212
+ ```
213
+
214
+ Note that you must still import ` xastscript ` yourself and configure your
215
+ JavaScript compiler to use the identifier you assign it to as a pragma (and
216
+ pass ` null ` for fragments).
217
+
218
+ For [ bublé] [ ] , this can be done by setting ` jsx: 'x' ` and ` jsxFragment: 'null' `
219
+ (note that ` jsxFragment ` is currently only available on the API, not the CLI).
220
+
221
+ For [ Babel] [ ] , use [ ` @babel/plugin-transform-react-jsx ` ] [ babel-jsx ] (in classic
222
+ mode), and pass ` pragma: 'x' ` and ` pragmaFrag: 'null' ` .
223
+
224
+ Babel also lets you configure this in a script:
225
+
226
+ ``` jsx
227
+ /** @jsx x */
228
+ /** @jsxFrag null */
229
+ var x = require (' xastscript' )
230
+
231
+ console .log (< music / > )
232
+ ```
172
233
173
234
## Security
174
235
@@ -249,10 +310,20 @@ abide by its terms.
249
310
250
311
[ tree ] : https://github.com/syntax-tree/unist#tree
251
312
313
+ [ node ] : https://github.com/syntax-tree/unist#node
314
+
315
+ [ root ] : https://github.com/syntax-tree/xast#root
316
+
252
317
[ element ] : https://github.com/syntax-tree/xast#element
253
318
254
319
[ text ] : https://github.com/syntax-tree/xast#text
255
320
256
321
[ u ] : https://github.com/syntax-tree/unist-builder
257
322
258
323
[ h ] : https://github.com/syntax-tree/hastscript
324
+
325
+ [ bublé ] : https://github.com/Rich-Harris/buble
326
+
327
+ [ babel ] : https://github.com/babel/babel
328
+
329
+ [ babel-jsx ] : https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-react-jsx
0 commit comments