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 get the plain-text value of a node.
12
12
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
16
14
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 )
20
27
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?
22
40
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
25
48
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] [ ] :
27
51
28
52
``` sh
29
53
npm install hast-util-to-text
30
54
```
31
55
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
+
32
70
## Use
33
71
34
72
``` js
@@ -57,40 +95,54 @@ Delta echo foxtrot.
57
95
58
96
## API
59
97
60
- This package exports the following identifiers: ` toText ` .
98
+ This package exports the identifier ` toText ` .
61
99
There is no default export.
62
100
63
101
### ` toText(node, options?) `
64
102
65
- Utility to get the plain-text value of a [ * node* ] [ node ] .
103
+ Get the plain-text value of a node.
66
104
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
69
107
` 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).
72
114
73
115
###### ` options.whitespace `
74
116
75
117
Default whitespace setting to use (` 'normal' ` or ` 'pre' ` , default: ` 'normal' ` ).
76
118
77
119
###### Returns
78
120
79
- ` string ` — Stringified ` node ` .
121
+ Serialized ` node ` ( ` string ` ) .
80
122
81
123
###### Notes
82
124
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
88
129
* 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,
90
131
or Halfwidth East Asian Width characters are used, the same goes for a case
91
132
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.
94
146
95
147
## Security
96
148
@@ -99,17 +151,17 @@ openings for [cross-site scripting (XSS)][xss] attacks.
99
151
100
152
## Related
101
153
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 )
103
155
— Get the plain-text value (` textContent ` )
104
156
* [ ` hast-util-from-text ` ] ( https://github.com/syntax-tree/hast-util-from-text )
105
157
— 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 )
107
159
— Set the plain-text value (` textContent ` )
108
160
109
161
## Contribute
110
162
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.
113
165
See [ ` support.md ` ] [ support ] for ways to get help.
114
166
115
167
This project has a [ code of conduct] [ coc ] .
@@ -150,28 +202,32 @@ abide by its terms.
150
202
151
203
[ npm ] : https://docs.npmjs.com/cli/install
152
204
205
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
206
+
207
+ [ esmsh ] : https://esm.sh
208
+
209
+ [ typescript ] : https://www.typescriptlang.org
210
+
153
211
[ license ] : license
154
212
155
213
[ author ] : https://wooorm.com
156
214
157
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
215
+ [ health ] : https://github.com/syntax-tree/.github
158
216
159
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
217
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
160
218
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
162
222
163
223
[ html ] : https://html.spec.whatwg.org/#the-innertext-idl-attribute
164
224
165
225
[ css ] : https://drafts.csswg.org/css-text/#white-space-phase-1
166
226
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
170
228
171
229
[ hast ] : https://github.com/syntax-tree/hast
172
230
173
- [ node ] : https://github.com/syntax-tree/hast#nodes
174
-
175
231
[ root ] : https://github.com/syntax-tree/hast#root
176
232
177
233
[ comment ] : https://github.com/syntax-tree/hast#comment
0 commit comments