Skip to content

Commit 73f72b3

Browse files
committed
Add improved docs
1 parent cb909b6 commit 73f72b3

File tree

2 files changed

+103
-48
lines changed

2 files changed

+103
-48
lines changed

index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
2-
* @typedef {import('./lib/index.js').XChild}} Child Acceptable child value
3-
* @typedef {import('./lib/index.js').XAttributes}} Attributes Acceptable attributes value.
2+
* @typedef {import('./lib/index.js').XChild}} Child
3+
* Acceptable child value
4+
* @typedef {import('./lib/index.js').XAttributes}} Attributes
5+
* Acceptable attributes value.
46
*/
57

68
export {x} from './lib/index.js'

readme.md

+99-46
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,62 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

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

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?
1535

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

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][]:
2048

2149
```sh
2250
npm install xastscript
2351
```
2452

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+
2567
## Use
2668

2769
```js
@@ -145,9 +187,16 @@ Yields:
145187

146188
## API
147189

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+
148197
### `x(name?[, attributes][, …children])`
149198

150-
Create XML *[trees][tree]* in **[xast][]**.
199+
Create [xast][] trees.
151200

152201
##### Signatures
153202

@@ -180,20 +229,23 @@ Cannot be omitted when building an [`Element`][element] if the first child is a
180229
(Lists of) children (`string`, `number`, `Node`, `Array<children>`, optional).
181230
When strings or numbers are encountered, they are mapped to [`Text`][text]
182231
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.
184233

185234
##### Returns
186235

187236
[`Element`][element] or [`Root`][root].
188237

189238
## JSX
190239

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+
192244
The example above (omitting the second) can then be written like so:
193245

194246
```jsx
247+
/** @jsxImportSource x */
195248
import {u} from 'unist-builder'
196-
import {x} from 'xastscript'
197249

198250
console.log(
199251
<album id={123}>
@@ -215,17 +267,14 @@ console.log(
215267
)
216268
```
217269

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

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:
229278

230279
```jsx
231280
/** @jsx x @jsxFrag null */
@@ -237,16 +286,20 @@ console.log(<music />)
237286
For [TypeScript][], this can be done by setting `"jsx": "react"`,
238287
`"jsxFactory": "x"`, and `"jsxFragmentFactory": "null"` in the compiler options.
239288
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.
241291

242-
TypeScript also lets you configure this in a script:
292+
## Types
243293

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`.
247296

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

251304
## Security
252305

@@ -255,20 +308,20 @@ XML can be a dangerous language: don’t trust user-provided data.
255308
## Related
256309

257310
* [`unist-builder`][u]
258-
Create any unist tree
311+
create any unist tree
259312
* [`hastscript`][h]
260-
Create a **[hast][]** (HTML or SVG) unist tree
313+
create a hast tree
261314
* [`xast-util-to-xml`](https://github.com/syntax-tree/xast-util-to-xml)
262-
Serialize nodes to XML
315+
serialize xast as XML
263316
* [`xast-util-from-xml`](https://github.com/syntax-tree/xast-util-from-xml)
264-
Parse from XML
317+
parse xast from XML
265318
* [`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
267320

268321
## Contribute
269322

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.
272325
See [`support.md`][support] for ways to get help.
273326

274327
This project has a [code of conduct][coc].
@@ -309,24 +362,26 @@ abide by its terms.
309362

310363
[npm]: https://docs.npmjs.com/cli/install
311364

365+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
366+
367+
[esmsh]: https://esm.sh
368+
369+
[typescript]: https://www.typescriptlang.org
370+
312371
[license]: license
313372

314373
[author]: https://wooorm.com
315374

316-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
375+
[health]: https://github.com/syntax-tree/.github
317376

318-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
377+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
319378

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
321380

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
325382

326383
[xast]: https://github.com/syntax-tree/xast
327384

328-
[tree]: https://github.com/syntax-tree/unist#tree
329-
330385
[node]: https://github.com/syntax-tree/unist#node
331386

332387
[root]: https://github.com/syntax-tree/xast#root
@@ -339,12 +394,10 @@ abide by its terms.
339394

340395
[h]: https://github.com/syntax-tree/hastscript
341396

342-
[bublé]: https://github.com/Rich-Harris/buble
343-
344397
[babel]: https://github.com/babel/babel
345398

346399
[babel-jsx]: https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-react-jsx
347400

348-
[typescript]: https://www.typescriptlang.org
401+
[typescript-jsx]: https://www.typescriptlang.org/docs/handbook/jsx.html
349402

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

Comments
 (0)