Skip to content

Commit 4de6078

Browse files
committed
Add improved docs
1 parent b70a803 commit 4de6078

File tree

3 files changed

+80
-26
lines changed

3 files changed

+80
-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').Whitespace} Whitespace
34
*/
45

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

lib/index.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @typedef {Extract<Node, import('unist').Parent>} Parent
1414
* Any parent.
1515
* @typedef {'normal' | 'pre' | 'nowrap' | 'pre-wrap'} Whitespace
16-
* Whitespace values (from CSS).
16+
* Valid and useful whitespace values (from CSS).
1717
* @typedef {boolean} BreakValue
1818
* Whether there was a break.
1919
* @typedef {1 | 2} BreakNumber
@@ -115,11 +115,30 @@ const blockOrCaption = convertElement([
115115
])
116116

117117
/**
118-
* Implementation of the `innerText` getter.
118+
* Get the plain-text value of a node.
119119
*
120-
* See: <https://html.spec.whatwg.org/#the-innertext-idl-attribute>.
121-
* Note that we act as if `node` is being rendered, and as if we’re a
122-
* CSS-supporting user agent, with scripting enabled.
120+
* ###### Algorithm
121+
*
122+
* * if `tree` is a comment, returns its `value`
123+
* * if `tree` is a text, applies normal whitespace collapsing to its
124+
* `value`, as defined by the CSS Text spec
125+
* * if `tree` is a root or element, applies an algorithm similar to the
126+
* `innerText` getter as defined by HTML
127+
*
128+
* ###### Notes
129+
*
130+
* > 👉 **Note**: the algorithm acts as if `tree` is being rendered, and as if
131+
* > we’re a CSS-supporting user agent, with scripting enabled.
132+
*
133+
* * if `tree` is an element that is not displayed (such as a `head`), we’ll
134+
* still use the `innerText` algorithm instead of switching to `textContent`
135+
* * if descendants of `tree` are elements that are not displayed, they are
136+
* ignored
137+
* * CSS is not considered, except for the default user agent style sheet
138+
* * a line feed is collapsed instead of ignored in cases where Fullwidth, Wide,
139+
* or Halfwidth East Asian Width characters are used, the same goes for a case
140+
* with Chinese, Japanese, or Yi writing systems
141+
* * replaced elements (such as `audio`) are treated like non-replaced elements
123142
*
124143
* @param {Node} tree
125144
* Tree to turn into text.

readme.md

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`toText(node, options?)`](#totextnode-options)
20+
* [`toText(tree[, options])`](#totexttree-options)
21+
* [`Options`](#options)
22+
* [`Whitespace`](#whitespace)
2123
* [Types](#types)
2224
* [Compatibility](#compatibility)
2325
* [Security](#security)
@@ -52,7 +54,7 @@ turning line endings into `<br>`s
5254
## Install
5355

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

5759
```sh
5860
npm install hast-util-to-text
@@ -100,53 +102,77 @@ Delta echo foxtrot.
100102

101103
## API
102104

103-
This package exports the identifier `toText`.
105+
This package exports the identifier [`toText`][totext].
104106
There is no default export.
105107

106-
### `toText(node, options?)`
108+
### `toText(tree[, options])`
107109

108110
Get the plain-text value of a node.
109111

110-
* if `node` is a [comment][], returns its `value`
111-
* if `node` is a [text][], applies normal white-space collapsing to its
112-
`value`, as defined by the [CSS Text][css] spec
113-
* if `node` is a [root][] or [element][], applies an algorithm similar to the
114-
`innerText` getter as defined by [HTML][]
115-
116-
##### `options`
112+
###### Parameters
117113

118-
Configuration (optional).
114+
* `tree` ([`Node`][node])
115+
— tree to turn into text
116+
* `options` ([`Options`][options], optional)
117+
— configuration
119118

120-
###### `options.whitespace`
119+
###### Returns
121120

122-
Default whitespace setting to use (`'normal'` or `'pre'`, default: `'normal'`).
121+
Serialized `tree` (`string`).
123122

124-
###### Returns
123+
###### Algorithm
125124

126-
Serialized `node` (`string`).
125+
* if `tree` is a [comment][], returns its `value`
126+
* if `tree` is a [text][], applies normal whitespace collapsing to its
127+
`value`, as defined by the [CSS Text][css] spec
128+
* if `tree` is a [root][] or [element][], applies an algorithm similar to the
129+
`innerText` getter as defined by [HTML][]
127130

128131
###### Notes
129132

130-
* if `node` is an element that is not displayed (such as a `head`), we’ll
133+
> 👉 **Note**: the algorithm acts as if `tree` is being rendered, and as if
134+
> we’re a CSS-supporting user agent, with scripting enabled.
135+
136+
* if `tree` is an element that is not displayed (such as a `head`), we’ll
131137
still use the `innerText` algorithm instead of switching to `textContent`
132-
* if descendants of `node` are elements that are not displayed, they are
138+
* if descendants of `tree` are elements that are not displayed, they are
133139
ignored
134140
* CSS is not considered, except for the default user agent style sheet
135141
* a line feed is collapsed instead of ignored in cases where Fullwidth, Wide,
136142
or Halfwidth East Asian Width characters are used, the same goes for a case
137143
with Chinese, Japanese, or Yi writing systems
138144
* replaced elements (such as `audio`) are treated like non-replaced elements
139145

146+
### `Options`
147+
148+
Configuration (TypeScript type).
149+
150+
##### Fields
151+
152+
* `whitespace` ([`Whitespace`][whitespace], default: `'normal'`)
153+
— default whitespace setting to use
154+
155+
### `Whitespace`
156+
157+
Valid and useful whitespace values (from [CSS][]) (TypeScript type).
158+
159+
##### Type
160+
161+
```ts
162+
type Whitespace = 'normal' | 'pre' | 'nowrap' | 'pre-wrap'
163+
```
164+
140165
## Types
141166
142167
This package is fully typed with [TypeScript][].
143-
It exports the additional type `Options`.
168+
It exports the additional types [`Options`][options] and
169+
[`Whitespace`][whitespace].
144170
145171
## Compatibility
146172
147173
Projects maintained by the unified collective are compatible with all maintained
148174
versions of Node.js.
149-
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
175+
As of now, that is Node.js 14.14+ and 16.0+.
150176
Our projects sometimes work with older versions, but this is not guaranteed.
151177
152178
## Security
@@ -225,7 +251,7 @@ abide by its terms.
225251
226252
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
227253
228-
[html]: https://html.spec.whatwg.org/#the-innertext-idl-attribute
254+
[html]: https://html.spec.whatwg.org/multipage/dom.html#the-innertext-idl-attribute
229255
230256
[css]: https://drafts.csswg.org/css-text/#white-space-phase-1
231257
@@ -235,6 +261,8 @@ abide by its terms.
235261
236262
[hast]: https://github.com/syntax-tree/hast
237263
264+
[node]: https://github.com/syntax-tree/hast#nodes
265+
238266
[root]: https://github.com/syntax-tree/hast#root
239267
240268
[comment]: https://github.com/syntax-tree/hast#comment
@@ -244,3 +272,9 @@ abide by its terms.
244272
[element]: https://github.com/syntax-tree/hast#element
245273
246274
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
275+
276+
[totext]: #totexttree-options
277+
278+
[options]: #options
279+
280+
[whitespace]: #whitespace

0 commit comments

Comments
 (0)