8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
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.
12
12
13
- This is like the DOMs ` Node#innerText ` setter.
13
+ ## Contents
14
14
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 )
18
27
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.
20
45
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
23
47
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] [ ] :
25
50
26
51
``` sh
27
52
npm install hast-util-from-text
28
53
```
29
54
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
+
30
69
## Use
31
70
32
71
``` js
@@ -57,20 +96,32 @@ fromText(h('p'), 'Delta\nEcho')
57
96
58
97
## API
59
98
60
- This package exports the following identifiers: ` fromText ` .
99
+ This package exports the identifier ` fromText ` .
61
100
There is no default export.
62
101
63
102
### ` fromText(node[, value]) `
64
103
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
70
109
return, ` \r ` ; or a carriage return + line feed, ` \r\n ` ).
71
110
If no ` value ` is given (empty string ` '' ` , ` null ` , or ` undefined ` ), the
72
111
literal’s value is set to an empty string or the parent’s children are removed.
73
112
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
+
74
125
## Security
75
126
76
127
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
82
133
83
134
## Related
84
135
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 ` )
91
142
92
143
## Contribute
93
144
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.
96
147
See [ ` support.md ` ] [ support ] for ways to get help.
97
148
98
149
This project has a [ code of conduct] [ coc ] .
@@ -133,17 +184,25 @@ abide by its terms.
133
184
134
185
[ npm ] : https://docs.npmjs.com/cli/install
135
186
187
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
188
+
189
+ [ esmsh ] : https://esm.sh
190
+
191
+ [ typescript ] : https://www.typescriptlang.org
192
+
136
193
[ license ] : license
137
194
138
195
[ author ] : https://wooorm.com
139
196
140
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
197
+ [ health ] : https://github.com/syntax-tree/.github
141
198
142
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
199
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
143
200
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
145
202
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
147
206
148
207
[ literal ] : https://github.com/syntax-tree/unist#literal
149
208
@@ -153,12 +212,12 @@ abide by its terms.
153
212
154
213
[ hast ] : https://github.com/syntax-tree/hast
155
214
156
- [ node ] : https://github.com/syntax-tree/hast#nodes
157
-
158
215
[ text ] : https://github.com/syntax-tree/hast#text
159
216
160
217
[ element ] : https://github.com/syntax-tree/hast#element
161
218
162
219
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
163
220
164
221
[ 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