Skip to content

Commit e170d2c

Browse files
committed
Add improved docs
1 parent 0bcd5ce commit e170d2c

File tree

2 files changed

+77
-61
lines changed

2 files changed

+77
-61
lines changed

lib/index.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,9 @@
3939
* When string, an `Element` is built.
4040
* When nullish, a `Root` is built instead.
4141
* @param attributes
42-
* Map of attributes.
43-
*
44-
* Nullish (`null` or `undefined`) or `NaN` values are ignored, other values
45-
* are turned to strings.
46-
*
47-
* Cannot be given if building a `Root`.
48-
* Cannot be omitted when building an `Element` if the first child is a
49-
* `Node`.
42+
* Attributes of the element.
5043
* @param children
51-
* (Lists of) children.
52-
*
53-
* When strings or numbers are encountered, they are mapped to `Text` nodes.
54-
* If a `Root` node is encountered, its children are used instead.
44+
* Children of the node.
5545
* @returns
5646
* `Element` or `Root`.
5747
*/

readme.md

+75-49
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
* [Use](#use)
1919
* [API](#api)
2020
* [`x(name?[, attributes][, …children])`](#xname-attributes-children)
21+
* [`Attributes`](#attributes-1)
22+
* [`Child`](#child)
23+
* [`Result`](#result)
24+
* [Syntax tree](#syntax-tree)
2125
* [JSX](#jsx)
2226
* [Types](#types)
2327
* [Compatibility](#compatibility)
@@ -44,7 +48,7 @@ You can instead use [`unist-builder`][u] when creating any unist nodes and
4448
## Install
4549

4650
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][]:
4852

4953
```sh
5054
npm install xastscript
@@ -187,12 +191,12 @@ Yields:
187191

188192
## API
189193

190-
This package exports the identifier `x`.
194+
This package exports the identifier [`x`][x].
191195
There is no default export.
192196

193197
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.
196200

197201
### `x(name?[, attributes][, …children])`
198202

@@ -216,36 +220,81 @@ When nullish, a [`Root`][root] is built instead.
216220

217221
###### `attributes`
218222

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+
221239
Nullish (`null` or `undefined`) or `NaN` values are ignored, other values are
222240
turned to strings.
223241

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
227243

228-
###### `children`
244+
```ts
245+
type Attributes = Record<string, string | number | boolean | null | undefined>
246+
```
229247
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]
232253
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+
```
234268
235-
##### Returns
269+
### `Result`
236270
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][].
238282
239283
## JSX
240284
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`).
244292
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:
246294
247295
```jsx
248296
/** @jsxImportSource x */
297+
249298
import {u} from 'unist-builder'
250299

251300
console.log(
@@ -268,38 +317,17 @@ console.log(
268317
)
269318
```
270319

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-
293320
## Types
294321

295322
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].
297325

298326
## Compatibility
299327

300328
Projects maintained by the unified collective are compatible with all maintained
301329
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+.
303331
Our projects sometimes work with older versions, but this is not guaranteed.
304332

305333
## Security
@@ -383,8 +411,6 @@ abide by its terms.
383411

384412
[xast]: https://github.com/syntax-tree/xast
385413

386-
[node]: https://github.com/syntax-tree/unist#node
387-
388414
[root]: https://github.com/syntax-tree/xast#root
389415

390416
[element]: https://github.com/syntax-tree/xast#element
@@ -395,10 +421,10 @@ abide by its terms.
395421

396422
[h]: https://github.com/syntax-tree/hastscript
397423

398-
[babel]: https://github.com/babel/babel
424+
[x]: #xname-attributes-children
399425

400-
[babel-jsx]: https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-react-jsx
426+
[attributes]: #attributes-1
401427

402-
[typescript-jsx]: https://www.typescriptlang.org/docs/handbook/jsx.html
428+
[child]: #child
403429

404-
[estree-util-build-jsx]: https://github.com/syntax-tree/estree-util-build-jsx
430+
[result]: #result

0 commit comments

Comments
 (0)