@@ -46,7 +46,7 @@ of does the inverse: it takes a node and gets its text.
46
46
## Install
47
47
48
48
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] [ ] :
50
50
51
51
``` sh
52
52
npm install hast-util-from-text
@@ -96,19 +96,35 @@ fromText(h('p'), 'Delta\nEcho')
96
96
97
97
## API
98
98
99
- This package exports the identifier ` fromText ` .
99
+ This package exports the identifier [ ` fromText ` ] [ fromtext ] .
100
100
There is no default export.
101
101
102
102
### ` fromText(node[, value]) `
103
103
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.
112
128
113
129
## Types
114
130
@@ -119,7 +135,7 @@ It exports no additional types.
119
135
120
136
Projects maintained by the unified collective are compatible with all maintained
121
137
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+.
123
139
Our projects sometimes work with older versions, but this is not guaranteed.
124
140
125
141
## Security
@@ -204,20 +220,14 @@ abide by its terms.
204
220
205
221
[ hast-util-from-string ] : https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-from-string
206
222
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
-
213
223
[ hast ] : https://github.com/syntax-tree/hast
214
224
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
218
226
219
227
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
220
228
221
229
[ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
222
230
223
231
[ hast-util-to-text ] : https://github.com/syntax-tree/hast-util-to-text
232
+
233
+ [ fromtext ] : #fromtextnode-value
0 commit comments