Skip to content

Commit 340d8cc

Browse files
committed
Add improved docs
1 parent fc84f87 commit 340d8cc

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

lib/index.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@ const search = /\r?\n|\r/g
1616
/**
1717
* Set the plain-text value of a node.
1818
*
19-
* Implementation of the `innerText` setter:
20-
* <https://html.spec.whatwg.org/#the-innertext-idl-attribute>
19+
* ###### Algorithm
2120
*
22-
* > 👉 **Note**: `innerText` only exists on elements.
23-
* > In this utility, we accept all parent nodes and handle them as elements,
24-
* > and for all literals we set the `value` of the given node the the given
25-
* > value.
21+
* * if `tree` is a `comment` or `text`, sets its `value`
22+
* * if `tree` is a `root` or `element`, replaces its children with a `br`
23+
* element for every line ending and a `text` for everything else
24+
*
25+
* ###### Notes
26+
*
27+
* `innerText` only exists on elements.
28+
* In this utility, we accept all parent nodes and handle them as elements, and
29+
* for all literals we set the `value` of the given node the given value.
2630
*
2731
* @template {Node} T
2832
* Node type.
29-
* @param {T} node
33+
* @param {T} tree
3034
* Tree to change.
3135
* @param {string | null | undefined} [text]
3236
* Value to set.
3337
* @returns {T}
3438
* Given, modified, tree.
3539
*/
36-
export function fromText(node, text) {
40+
export function fromText(tree, text) {
3741
const value = text === undefined || text === null ? '' : String(text)
3842

39-
if ('children' in node) {
43+
if ('children' in tree) {
4044
/** @type {Array<BreakElement | Text>} */
4145
const nodes = []
4246
let start = 0
@@ -63,10 +67,10 @@ export function fromText(node, text) {
6367
}
6468
}
6569

66-
node.children = nodes
67-
} else if (node.type !== 'doctype') {
68-
node.value = value
70+
tree.children = nodes
71+
} else if (tree.type !== 'doctype') {
72+
tree.value = value
6973
}
7074

71-
return node
75+
return tree
7276
}

readme.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ of does the inverse: it takes a node and gets its text.
4646
## Install
4747

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

5151
```sh
5252
npm install hast-util-from-text
@@ -96,19 +96,35 @@ fromText(h('p'), 'Delta\nEcho')
9696

9797
## API
9898

99-
This package exports the identifier `fromText`.
99+
This package exports the identifier [`fromText`][fromtext].
100100
There is no default export.
101101

102102
### `fromText(node[, value])`
103103

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
109-
return, `\r`; or a carriage return + line feed, `\r\n`).
110-
If no `value` is given (empty string `''`, `null`, or `undefined`), the
111-
literal’s value is set to an empty string or the parent’s children are removed.
104+
Set the plain-text value of a node.
105+
106+
###### Parameters
107+
108+
* `tree` ([`Node`][node])
109+
— tree to change
110+
* `value` (`string`, optional)
111+
— value to set
112+
113+
###### Returns
114+
115+
Given, modified, tree ([`Node`][node]).
116+
117+
###### Algorithm
118+
119+
* if `tree` is a `comment` or `text`, sets its `value`
120+
* if `tree` is a `root` or `element`, replaces its children with a `br`
121+
element for every line ending and a `text` for everything else
122+
123+
###### Notes
124+
125+
`innerText` only exists on elements.
126+
In this utility, we accept all parent nodes and handle them as elements, and
127+
for all literals we set the `value` of the given node the given value.
112128

113129
## Types
114130

@@ -119,7 +135,7 @@ It exports no additional types.
119135

120136
Projects maintained by the unified collective are compatible with all maintained
121137
versions of Node.js.
122-
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
138+
As of now, that is Node.js 14.14+ and 16.0+.
123139
Our projects sometimes work with older versions, but this is not guaranteed.
124140

125141
## Security
@@ -204,20 +220,14 @@ abide by its terms.
204220

205221
[hast-util-from-string]: https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-from-string
206222

207-
[literal]: https://github.com/syntax-tree/unist#literal
208-
209-
[parent]: https://github.com/syntax-tree/unist#parent
210-
211-
[child]: https://github.com/syntax-tree/unist#child
212-
213223
[hast]: https://github.com/syntax-tree/hast
214224

215-
[text]: https://github.com/syntax-tree/hast#text
216-
217-
[element]: https://github.com/syntax-tree/hast#element
225+
[node]: https://github.com/syntax-tree/hast#nodes
218226

219227
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
220228

221229
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
222230

223231
[hast-util-to-text]: https://github.com/syntax-tree/hast-util-to-text
232+
233+
[fromtext]: #fromtextnode-value

0 commit comments

Comments
 (0)