Skip to content

Commit e32404d

Browse files
committed
Add improved docs
1 parent 4b527a0 commit e32404d

File tree

1 file changed

+94
-38
lines changed

1 file changed

+94
-38
lines changed

readme.md

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

11-
[**hast**][hast] utility to get the plain-text value of a [*node*][node].
11+
[hast][] utility to get the plain-text value of a node.
1212

13-
This is like the DOMs `Node#innerText` getter but there are some deviations from
14-
the spec.
15-
The resulting text is returned.
13+
## Contents
1614

17-
You’d typically want to use [`hast-util-to-string`][to-string]
18-
(`textContent`), but `hast-util-to-text` (`innerText`) adds for example line
19-
breaks where `<br>` elements are used.
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+
* [`toText(node, options?)`](#totextnode-options)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Security](#security)
24+
* [Related](#related)
25+
* [Contribute](#contribute)
26+
* [License](#license)
2027

21-
## Install
28+
## What is this?
29+
30+
This package is a utility that takes a [hast][] node takes its plain-text value.
31+
It is like the DOMs `Node#innerText`, which is a bitter nicer than
32+
`Node#textContent`, because this turns `<br>` elements into line breaks and
33+
uses `'\t'` (tabs) between table cells.
34+
35+
There are some small deviations from the spec, because the DOM has knowledge of
36+
associated CSS, and can take into account that elements have `display: none` or
37+
`text-transform` association with them, and this utility can’t do that.
38+
39+
## When should I use this?
2240

23-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
24-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
41+
This is a small utility that is useful when you want a plain-text version of a
42+
node that is close to how it’s “visible” to users.
43+
44+
This utility is similar to [`hast-util-to-string`][hast-util-to-string], which
45+
is simpler, and more like the `Node#textContent` algorithm discussed above.
46+
47+
## Install
2548

26-
[npm][]:
49+
This package is [ESM only][esm].
50+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
2751

2852
```sh
2953
npm install hast-util-to-text
3054
```
3155

56+
In Deno with [`esm.sh`][esmsh]:
57+
58+
```js
59+
import {toText} from "https://esm.sh/hast-util-to-text@3"
60+
```
61+
62+
In browsers with [`esm.sh`][esmsh]:
63+
64+
```html
65+
<script type="module">
66+
import {toText} from "https://esm.sh/hast-util-to-text@3?bundle"
67+
</script>
68+
```
69+
3270
## Use
3371

3472
```js
@@ -57,40 +95,54 @@ Delta echo foxtrot.
5795

5896
## API
5997

60-
This package exports the following identifiers: `toText`.
98+
This package exports the identifier `toText`.
6199
There is no default export.
62100

63101
### `toText(node, options?)`
64102

65-
Utility to get the plain-text value of a [*node*][node].
103+
Get the plain-text value of a node.
66104

67-
* If `node` is a [*comment*][comment], returns its `value`
68-
* If `node` is a [*text*][text], applies normal white-space collapsing to its
105+
* if `node` is a [comment][], returns its `value`
106+
* if `node` is a [text][], applies normal white-space collapsing to its
69107
`value`, as defined by the [CSS Text][css] spec
70-
* If `node` is a [*root*][root] or [*element*][element], applies an algorithm
71-
similar to the `innerText` getter as defined by [HTML][]
108+
* if `node` is a [root][] or [element][], applies an algorithm similar to the
109+
`innerText` getter as defined by [HTML][]
110+
111+
##### `options`
112+
113+
Configuration (optional).
72114

73115
###### `options.whitespace`
74116

75117
Default whitespace setting to use (`'normal'` or `'pre'`, default: `'normal'`).
76118

77119
###### Returns
78120

79-
`string` — Stringified `node`.
121+
Serialized `node` (`string`).
80122

81123
###### Notes
82124

83-
* If `node` is an [*element*][element] that is not displayed (such as a
84-
`head`), we’ll still use the `innerText` algorithm instead of switching to
85-
`textContent`
86-
* If [*descendants*][descendant] of `node` are [*elements*][element] that are
87-
not displayed, they are ignored
125+
* if `node` is an element that is not displayed (such as a `head`), we’ll
126+
still use the `innerText` algorithm instead of switching to `textContent`
127+
* if descendants of `node` are elements that are not displayed, they are
128+
ignored
88129
* CSS is not considered, except for the default user agent style sheet
89-
* A line feed is collapsed instead of ignored in cases where Fullwidth, Wide,
130+
* a line feed is collapsed instead of ignored in cases where Fullwidth, Wide,
90131
or Halfwidth East Asian Width characters are used, the same goes for a case
91132
with Chinese, Japanese, or Yi writing systems
92-
* Replaced [*elements*][element] (such as `audio`) are treated like
93-
non-replaced *elements*
133+
* replaced elements (such as `audio`) are treated like non-replaced elements
134+
135+
## Types
136+
137+
This package is fully typed with [TypeScript][].
138+
It exports the additional type `Options`.
139+
140+
## Compatibility
141+
142+
Projects maintained by the unified collective are compatible with all maintained
143+
versions of Node.js.
144+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
145+
Our projects sometimes work with older versions, but this is not guaranteed.
94146

95147
## Security
96148

@@ -99,17 +151,17 @@ openings for [cross-site scripting (XSS)][xss] attacks.
99151

100152
## Related
101153

102-
* [`hast-util-to-string`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-to-string)
154+
* [`hast-util-to-string`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-to-string)
103155
— Get the plain-text value (`textContent`)
104156
* [`hast-util-from-text`](https://github.com/syntax-tree/hast-util-from-text)
105157
— Set the plain-text value (`innerText`)
106-
* [`hast-util-from-string`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-from-string)
158+
* [`hast-util-from-string`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-from-string)
107159
— Set the plain-text value (`textContent`)
108160

109161
## Contribute
110162

111-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
112-
started.
163+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
164+
ways to get started.
113165
See [`support.md`][support] for ways to get help.
114166

115167
This project has a [code of conduct][coc].
@@ -150,28 +202,32 @@ abide by its terms.
150202

151203
[npm]: https://docs.npmjs.com/cli/install
152204

205+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
206+
207+
[esmsh]: https://esm.sh
208+
209+
[typescript]: https://www.typescriptlang.org
210+
153211
[license]: license
154212

155213
[author]: https://wooorm.com
156214

157-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
215+
[health]: https://github.com/syntax-tree/.github
158216

159-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
217+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
160218

161-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
219+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
220+
221+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
162222

163223
[html]: https://html.spec.whatwg.org/#the-innertext-idl-attribute
164224

165225
[css]: https://drafts.csswg.org/css-text/#white-space-phase-1
166226

167-
[to-string]: https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-to-string
168-
169-
[descendant]: https://github.com/syntax-tree/unist#descendant
227+
[hast-util-to-string]: https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-to-string
170228

171229
[hast]: https://github.com/syntax-tree/hast
172230

173-
[node]: https://github.com/syntax-tree/hast#nodes
174-
175231
[root]: https://github.com/syntax-tree/hast#root
176232

177233
[comment]: https://github.com/syntax-tree/hast#comment

0 commit comments

Comments
 (0)