Skip to content

Commit a142d48

Browse files
committed
Add improved docs
1 parent d2f2648 commit a142d48

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @typedef {import('./lib/index.js').Options} Options
3+
* @typedef {import('./lib/index.js').Quote} Quote
34
*/
45

56
export {toXml} from './lib/index.js'

lib/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,14 @@ import {one} from './one.js'
4545
/**
4646
* Serialize a xast tree to XML.
4747
*
48-
* @param {Node | Array<Node>} node
48+
* @param {Node | Array<Node>} tree
4949
* xast node(s) to serialize.
5050
* @param {Options | null | undefined} [options]
5151
* Configuration.
5252
* @returns {string}
5353
* Serialized XML.
5454
*/
55-
export function toXml(node, options) {
56-
/** @type {Node} */
57-
// @ts-expect-error Assume no `root` in `node`.
58-
const value = Array.isArray(node) ? {type: 'root', children: node} : node
59-
55+
export function toXml(tree, options) {
6056
/** @type {State} */
6157
const state = {options: options || {}}
6258

@@ -71,5 +67,9 @@ export function toXml(node, options) {
7167
)
7268
}
7369

74-
return one(value, state)
70+
/** @type {Node} */
71+
// @ts-expect-error Assume no `root` in `node`.
72+
const node = Array.isArray(tree) ? {type: 'root', children: tree} : tree
73+
74+
return one(node, state)
7575
}

readme.md

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* [Use](#use)
1919
* [API](#api)
2020
* [`toXml(tree[, options])`](#toxmltree-options)
21+
* [`Options`](#options)
22+
* [`Quote`](#quote-1)
2123
* [Types](#types)
2224
* [Compatibility](#compatibility)
2325
* [Security](#security)
@@ -50,7 +52,7 @@ utility but for HTML: it turns [hast][] into HTML.
5052
## Install
5153

5254
This package is [ESM only][esm].
53-
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:
55+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
5456

5557
```sh
5658
npm install xast-util-to-xml
@@ -113,48 +115,76 @@ Yields:
113115

114116
## API
115117

116-
This package exports the identifier `toXml`.
118+
This package exports the identifier [`toXml`][toxml].
117119
There is no default export.
118120

119121
### `toXml(tree[, options])`
120122

121-
Serialize the given [xast][] node (or list of nodes).
123+
Serialize a xast tree to XML.
122124

123-
##### `options`
125+
###### Parameters
124126

125-
Configuration (optional).
127+
* `tree` ([`Node`][node] or `Array<Node>`)
128+
— xast node(s) to serialize
129+
* `options` ([`Options`][options], optional)
130+
— configuration
126131

127-
###### `options.quote`
132+
###### Returns
128133

129-
Preferred quote to use (`'"'` or `'\''`, default: `'"'`).
134+
Serialized XML (`string`).
130135

131-
###### `options.quoteSmart`
136+
### `Options`
132137

133-
Use the other quote if that results in less bytes (`boolean`, default: `false`).
138+
Configuration (TypeScript type).
134139

135-
###### `options.closeEmptyElements`
140+
##### Fields
141+
142+
###### `allowDangerousXml`
143+
144+
Allow `raw` nodes and insert them as raw XML (`boolean`, default: `false`).
145+
146+
When `false`, `Raw` nodes are encoded.
147+
148+
> ⚠️ **Danger**: only set this if you completely trust the content.
149+
150+
###### `closeEmptyElements`
151+
152+
Close elements without any content with slash (`/`) on the opening tag instead
153+
of an end tag: `<circle />` instead of `<circle></circle>` (`boolean`, default:
154+
`false`).
136155

137-
Close elements without any content with slash (`/`) on the opening tag
138-
instead of an end tag: `<circle />` instead of `<circle></circle>` (`boolean`,
139-
default: `false`).
140156
See `tightClose` to control whether a space is used before the slash.
141157

142-
###### `options.tightClose`
158+
###### `quote`
159+
160+
Preferred quote to use ([`Quote`][quote], default: `'"'`).
161+
162+
###### `quoteSmart`
163+
164+
Use the other quote if that results in less bytes (`boolean`, default:
165+
`false`).
166+
167+
###### `tightClose`
143168

144169
Do not use an extra space when closing self-closing elements: `<circle/>`
145170
instead of `<circle />` (`boolean`, default: `false`).
146171

147-
###### `options.allowDangerousXml`
172+
> 👉 **Note**: only used if `closeEmptyElements: true`.
173+
174+
### `Quote`
148175

149-
Allow `raw` nodes and insert them as raw XML.
150-
When falsey, encodes `raw` nodes (`boolean`, default: `false`).
176+
XML quotes for attribute values (TypeScript type).
151177

152-
> ☢️ **Danger**: only set this if you completely trust the content.
178+
###### Type
179+
180+
```ts
181+
type Quote = '"' | "'"
182+
```
153183
154184
## Types
155185
156186
This package is fully typed with [TypeScript][].
157-
It exports the additional type `Options`.
187+
It exports the additional types [`Options`][options] and [`Quote`][quote].
158188
159189
## Compatibility
160190
@@ -240,8 +270,16 @@ abide by its terms.
240270
241271
[xast]: https://github.com/syntax-tree/xast
242272
273+
[node]: https://github.com/syntax-tree/xast#nodes
274+
243275
[hast]: https://github.com/syntax-tree/hast
244276
245277
[xast-util-from-xml]: https://github.com/syntax-tree/xast-util-from-xml
246278
247279
[hast-util-to-html]: https://github.com/syntax-tree/hast-util-to-html
280+
281+
[toxml]: #toxmltree-options
282+
283+
[options]: #options
284+
285+
[quote]: #quote-1

0 commit comments

Comments
 (0)