Skip to content

Commit 273d127

Browse files
committed
Add improved docs
1 parent 8b0e1bb commit 273d127

File tree

1 file changed

+88
-29
lines changed

1 file changed

+88
-29
lines changed

readme.md

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,64 @@
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 set the plain-text value of a node.
1212

13-
This is like the DOMs `Node#innerText` setter.
13+
## Contents
1414

15-
You’d typically want to use [`hast-util-from-string`][from-string]
16-
(`textContent`), but `hast-util-from-text` (`innerText`) adds `<br>` elements
17-
instead of line breaks.
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+
* [`fromText(node[, value])`](#fromtextnode-value)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Security](#security)
24+
* [Related](#related)
25+
* [Contribute](#contribute)
26+
* [License](#license)
1827

19-
## Install
28+
## What is this?
29+
30+
This package is a utility that takes a [hast][] node and a string and sets that
31+
value as its text.
32+
It is like the DOMs `Node#innerText` setter, which can be a bit nicer than
33+
`Node#textContent`, because this turns line endings into `<br>` elements.
34+
35+
## When should I use this?
36+
37+
This is a small utility that is useful when you want to set a string that is
38+
close to how it’s “visible” to users.
39+
40+
This utility is similar to [`hast-util-from-string`][hast-util-from-string],
41+
which is simpler, and like the `Node#textContent` algorithm discussed above.
42+
43+
There is also a package [`hast-util-to-text`][hast-util-to-text], which sort
44+
of does the inverse: it takes a node and gets its text.
2045

21-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
22-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
46+
## Install
2347

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

2651
```sh
2752
npm install hast-util-from-text
2853
```
2954

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

3271
```js
@@ -57,20 +96,32 @@ fromText(h('p'), 'Delta\nEcho')
5796

5897
## API
5998

60-
This package exports the following identifiers: `fromText`.
99+
This package exports the identifier `fromText`.
61100
There is no default export.
62101

63102
### `fromText(node[, value])`
64103

65-
If the given `node` is a [*literal*][literal], set that to the given `value` or
66-
an empty string.
67-
If the given `node` is a [*parent*][parent], its [*children*][child] are
68-
replaced with new children: [*texts*][text] for every run of text and `<br>`
69-
[*elements*][element] for every line break (a line feed, `\n`; a carriage
104+
If the given `node` is a *[literal][]*, set that to the given `value` or an
105+
empty string.
106+
If the given `node` is a *[parent][]*, its [children][child] are replaced with
107+
new children: *[texts][text]* for every run of text and `<br>`
108+
*[elements][element]* for every line break (a line feed, `\n`; a carriage
70109
return, `\r`; or a carriage return + line feed, `\r\n`).
71110
If no `value` is given (empty string `''`, `null`, or `undefined`), the
72111
literal’s value is set to an empty string or the parent’s children are removed.
73112

113+
## Types
114+
115+
This package is fully typed with [TypeScript][].
116+
It exports no additional types.
117+
118+
## Compatibility
119+
120+
Projects maintained by the unified collective are compatible with all maintained
121+
versions of Node.js.
122+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
123+
Our projects sometimes work with older versions, but this is not guaranteed.
124+
74125
## Security
75126

76127
Improper use can open you up to a [cross-site scripting (XSS)][xss] attack as
@@ -82,17 +133,17 @@ Do not use user input in `value` when operating on `script` elements or use
82133

83134
## Related
84135

85-
* [`hast-util-to-text`](https://github.com/syntax-tree/hast-util-to-text)
86-
Get the plain-text value (`innerText`)
87-
* [`hast-util-to-string`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-to-string)
88-
Get the plain-text value (`textContent`)
89-
* [`hast-util-from-string`][from-string]
90-
Set the plain-text value (`textContent`)
136+
* [`hast-util-to-text`][hast-util-to-text]
137+
get the plain-text value (`innerText`)
138+
* [`hast-util-to-string`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-to-string)
139+
get the plain-text value (`textContent`)
140+
* [`hast-util-from-string`][hast-util-from-string]
141+
set the plain-text value (`textContent`)
91142

92143
## Contribute
93144

94-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
95-
started.
145+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
146+
ways to get started.
96147
See [`support.md`][support] for ways to get help.
97148

98149
This project has a [code of conduct][coc].
@@ -133,17 +184,25 @@ abide by its terms.
133184

134185
[npm]: https://docs.npmjs.com/cli/install
135186

187+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
188+
189+
[esmsh]: https://esm.sh
190+
191+
[typescript]: https://www.typescriptlang.org
192+
136193
[license]: license
137194

138195
[author]: https://wooorm.com
139196

140-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
197+
[health]: https://github.com/syntax-tree/.github
141198

142-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
199+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
143200

144-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
201+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
145202

146-
[from-string]: https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-from-string
203+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
204+
205+
[hast-util-from-string]: https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-from-string
147206

148207
[literal]: https://github.com/syntax-tree/unist#literal
149208

@@ -153,12 +212,12 @@ abide by its terms.
153212

154213
[hast]: https://github.com/syntax-tree/hast
155214

156-
[node]: https://github.com/syntax-tree/hast#nodes
157-
158215
[text]: https://github.com/syntax-tree/hast#text
159216

160217
[element]: https://github.com/syntax-tree/hast#element
161218

162219
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
163220

164221
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
222+
223+
[hast-util-to-text]: https://github.com/syntax-tree/hast-util-to-text

0 commit comments

Comments
 (0)