Skip to content

Commit aadc613

Browse files
committed
Add docs
1 parent 2812617 commit aadc613

File tree

1 file changed

+78
-7
lines changed

1 file changed

+78
-7
lines changed

readme.md

+78-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ console.log(
4848
// For other xast nodes, such as comments, instructions, doctypes, or cdata
4949
// can be created with unist-builder:
5050
console.log(
51-
u('root', [
51+
x(null, [
5252
u('instruction', {name: 'xml'}, 'version="1.0" encoding="UTF-8"'),
5353
x('album', [
5454
u('comment', 'Great album!'),
@@ -142,33 +142,94 @@ Yields:
142142

143143
## API
144144

145-
### `x(name[, attributes][, …children])`
145+
### `x(name?[, attributes][, …children])`
146146

147147
Create XML *[trees][tree]* in **[xast][]**.
148148

149+
##### Signatures
150+
151+
* `x(): root`
152+
* `x(null[, …children]): root`
153+
* `x(name[, attributes][, …children]): element`
154+
149155
##### Parameters
150156

151157
###### `name`
152158

153-
Qualified name (`string`).
159+
Qualified name (`string`, optional).
154160
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.
155163

156164
###### `attributes`
157165

158166
Map of attributes (`Object.<*>`, optional).
159167
Nullish (`null` or `undefined`) or `NaN` values are ignored, other values are
160168
turned to strings.
161169

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].
163173

164174
###### `children`
165175

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.
168180

169181
##### Returns
170182

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+
```
172233

173234
## Security
174235

@@ -249,10 +310,20 @@ abide by its terms.
249310

250311
[tree]: https://github.com/syntax-tree/unist#tree
251312

313+
[node]: https://github.com/syntax-tree/unist#node
314+
315+
[root]: https://github.com/syntax-tree/xast#root
316+
252317
[element]: https://github.com/syntax-tree/xast#element
253318

254319
[text]: https://github.com/syntax-tree/xast#text
255320

256321
[u]: https://github.com/syntax-tree/unist-builder
257322

258323
[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

Comments
 (0)