Skip to content

Commit c95da95

Browse files
committed
Add improved docs
1 parent 6f7161f commit c95da95

File tree

1 file changed

+86
-21
lines changed

1 file changed

+86
-21
lines changed

readme.md

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,65 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
**[xast][]** utility to parse from XML.
11+
[xast][] utility to parse from XML.
1212

13-
## 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+
* [`fromXml(value)`](#fromxmlvalue)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Security](#security)
24+
* [Related](#related)
25+
* [Contribute](#contribute)
26+
* [License](#license)
27+
28+
## What is this?
29+
30+
This package is a utility that takes XML input and turns it into a [xast][]
31+
syntax tree.
32+
It uses [`sax`][sax], which turns XML into events, while it turns those events
33+
into nodes.
1434

15-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
35+
## When should I use this?
1736

18-
[npm][]:
37+
If you want to handle syntax trees, use this.
38+
Use [`sax`][sax] itself instead when you want to do other things.
39+
40+
The utility [`xast-util-to-xml`][xast-util-to-xml] does the inverse of this
41+
utility.
42+
It turns xast into XML.
43+
44+
## Install
45+
46+
This package is [ESM only][esm].
47+
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:
1948

2049
```sh
2150
npm install xast-util-from-xml
2251
```
2352

53+
In Deno with [`esm.sh`][esmsh]:
54+
55+
```js
56+
import {fromXml} from 'https://esm.sh/xast-util-from-xml@2'
57+
```
58+
59+
In browsers with [`esm.sh`][esmsh]:
60+
61+
```html
62+
<script type="module">
63+
import {fromXml} from 'https://esm.sh/xast-util-from-xml@2?bundle'
64+
</script>
65+
```
66+
2467
## Use
2568

26-
Say we have the following XML file, `example.xml`:
69+
Say our document `example.xml` contains:
2770

2871
```xml
2972
<album id="123">
@@ -33,20 +76,18 @@ Say we have the following XML file, `example.xml`:
3376
</album>
3477
```
3578

36-
And our script, `example.js`, looks as follows:
79+
…and our module `example.js` looks as follows:
3780

3881
```js
39-
import fs from 'node:fs'
82+
import fs from 'node:fs/promises'
4083
import {fromXml} from 'xast-util-from-xml'
4184

42-
const doc = fs.readFileSync('example.xml')
43-
44-
const tree = fromXml(doc)
85+
const tree = fromXml(await fs.readFile('example.xml'))
4586

4687
console.log(tree)
4788
```
4889

49-
Now, running `node example` yields (positional info removed for brevity):
90+
…now running `node example.js` yields (positional info removed for brevity):
5091

5192
```js
5293
{
@@ -88,23 +129,35 @@ Now, running `node example` yields (positional info removed for brevity):
88129

89130
## API
90131

91-
This package exports the following identifiers: `fromXml`.
132+
This package exports the identifier `fromXml`.
92133
There is no default export.
93134

94-
### `fromXml(doc)`
135+
### `fromXml(value)`
95136

96-
Parse XML to a **[xast][]** tree.
137+
Turn XML into a syntax tree.
97138

98139
##### Parameters
99140

100-
###### `doc`
141+
###### `value`
101142

102143
Value to parse (`string` or `Buffer` in UTF-8).
103144

104145
##### Returns
105146

106147
[`Root`][root].
107148

149+
## Types
150+
151+
This package is fully typed with [TypeScript][].
152+
It exports no additional types.
153+
154+
## Compatibility
155+
156+
Projects maintained by the unified collective are compatible with all maintained
157+
versions of Node.js.
158+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
159+
Our projects sometimes work with older versions, but this is not guaranteed.
160+
108161
## Security
109162

110163
XML can be a dangerous language: don’t trust user-provided data.
@@ -120,8 +173,8 @@ XML can be a dangerous language: don’t trust user-provided data.
120173

121174
## Contribute
122175

123-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
124-
started.
176+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
177+
ways to get started.
125178
See [`support.md`][support] for ways to get help.
126179

127180
This project has a [code of conduct][coc].
@@ -162,16 +215,28 @@ abide by its terms.
162215

163216
[npm]: https://docs.npmjs.com/cli/install
164217

218+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
219+
220+
[esmsh]: https://esm.sh
221+
222+
[typescript]: https://www.typescriptlang.org
223+
165224
[license]: license
166225

167226
[author]: https://wooorm.com
168227

169-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
228+
[health]: https://github.com/syntax-tree/.github
170229

171-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
230+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
172231

173-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
232+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
233+
234+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
174235

175236
[xast]: https://github.com/syntax-tree/xast
176237

177238
[root]: https://github.com/syntax-tree/xast#root
239+
240+
[sax]: https://github.com/isaacs/sax-js
241+
242+
[xast-util-to-xml]: https://github.com/syntax-tree/xast-util-to-xml

0 commit comments

Comments
 (0)