17
17
* [ Install] ( #install )
18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
- * [ ` toText(node, options?) ` ] ( #totextnode-options )
20
+ * [ ` toText(tree[, options]) ` ] ( #totexttree-options )
21
+ * [ ` Options ` ] ( #options )
22
+ * [ ` Whitespace ` ] ( #whitespace )
21
23
* [ Types] ( #types )
22
24
* [ Compatibility] ( #compatibility )
23
25
* [ Security] ( #security )
@@ -52,7 +54,7 @@ turning line endings into `<br>`s
52
54
## Install
53
55
54
56
This package is [ ESM only] [ esm ] .
55
- In Node.js (version 12.20+, 14.14+, 16.0+, 18 .0+), install with [ npm] [ ] :
57
+ In Node.js (version 14.14+ or 16 .0+), install with [ npm] [ ] :
56
58
57
59
``` sh
58
60
npm install hast-util-to-text
@@ -100,53 +102,77 @@ Delta echo foxtrot.
100
102
101
103
## API
102
104
103
- This package exports the identifier ` toText ` .
105
+ This package exports the identifier [ ` toText ` ] [ totext ] .
104
106
There is no default export.
105
107
106
- ### ` toText(node , options? ) `
108
+ ### ` toText(tree[ , options] ) `
107
109
108
110
Get the plain-text value of a node.
109
111
110
- * if ` node ` is a [ comment] [ ] , returns its ` value `
111
- * if ` node ` is a [ text] [ ] , applies normal white-space collapsing to its
112
- ` value ` , as defined by the [ CSS Text] [ css ] spec
113
- * if ` node ` is a [ root] [ ] or [ element] [ ] , applies an algorithm similar to the
114
- ` innerText ` getter as defined by [ HTML] [ ]
115
-
116
- ##### ` options `
112
+ ###### Parameters
117
113
118
- Configuration (optional).
114
+ * ` tree ` ([ ` Node ` ] [ node ] )
115
+ — tree to turn into text
116
+ * ` options ` ([ ` Options ` ] [ options ] , optional)
117
+ — configuration
119
118
120
- ###### ` options.whitespace `
119
+ ###### Returns
121
120
122
- Default whitespace setting to use ( ` 'normal' ` or ` 'pre' ` , default: ` 'normal' ` ).
121
+ Serialized ` tree ` ( ` string ` ).
123
122
124
- ###### Returns
123
+ ###### Algorithm
125
124
126
- Serialized ` node ` (` string ` ).
125
+ * if ` tree ` is a [ comment] [ ] , returns its ` value `
126
+ * if ` tree ` is a [ text] [ ] , applies normal whitespace collapsing to its
127
+ ` value ` , as defined by the [ CSS Text] [ css ] spec
128
+ * if ` tree ` is a [ root] [ ] or [ element] [ ] , applies an algorithm similar to the
129
+ ` innerText ` getter as defined by [ HTML] [ ]
127
130
128
131
###### Notes
129
132
130
- * if ` node ` is an element that is not displayed (such as a ` head ` ), we’ll
133
+ > 👉 ** Note** : the algorithm acts as if ` tree ` is being rendered, and as if
134
+ > we’re a CSS-supporting user agent, with scripting enabled.
135
+
136
+ * if ` tree ` is an element that is not displayed (such as a ` head ` ), we’ll
131
137
still use the ` innerText ` algorithm instead of switching to ` textContent `
132
- * if descendants of ` node ` are elements that are not displayed, they are
138
+ * if descendants of ` tree ` are elements that are not displayed, they are
133
139
ignored
134
140
* CSS is not considered, except for the default user agent style sheet
135
141
* a line feed is collapsed instead of ignored in cases where Fullwidth, Wide,
136
142
or Halfwidth East Asian Width characters are used, the same goes for a case
137
143
with Chinese, Japanese, or Yi writing systems
138
144
* replaced elements (such as ` audio ` ) are treated like non-replaced elements
139
145
146
+ ### ` Options `
147
+
148
+ Configuration (TypeScript type).
149
+
150
+ ##### Fields
151
+
152
+ * ` whitespace ` ([ ` Whitespace ` ] [ whitespace ] , default: ` 'normal' ` )
153
+ — default whitespace setting to use
154
+
155
+ ### ` Whitespace `
156
+
157
+ Valid and useful whitespace values (from [ CSS] [ ] ) (TypeScript type).
158
+
159
+ ##### Type
160
+
161
+ ``` ts
162
+ type Whitespace = ' normal' | ' pre' | ' nowrap' | ' pre-wrap'
163
+ ` ` `
164
+
140
165
## Types
141
166
142
167
This package is fully typed with [TypeScript][].
143
- It exports the additional type ` Options ` .
168
+ It exports the additional types [ ` Options ` ][options] and
169
+ [ ` Whitespace ` ][whitespace].
144
170
145
171
## Compatibility
146
172
147
173
Projects maintained by the unified collective are compatible with all maintained
148
174
versions of Node.js.
149
- As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18 .0+.
175
+ As of now, that is Node.js 14.14+ and 16 .0+.
150
176
Our projects sometimes work with older versions, but this is not guaranteed.
151
177
152
178
## Security
@@ -225,7 +251,7 @@ abide by its terms.
225
251
226
252
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
227
253
228
- [ html ] : https://html.spec.whatwg.org/#the-innertext-idl-attribute
254
+ [html]: https://html.spec.whatwg.org/multipage/dom.html #the-innertext-idl-attribute
229
255
230
256
[css]: https://drafts.csswg.org/css-text/#white-space-phase-1
231
257
@@ -235,6 +261,8 @@ abide by its terms.
235
261
236
262
[hast]: https://github.com/syntax-tree/hast
237
263
264
+ [node]: https://github.com/syntax-tree/hast#nodes
265
+
238
266
[root]: https://github.com/syntax-tree/hast#root
239
267
240
268
[comment]: https://github.com/syntax-tree/hast#comment
@@ -244,3 +272,9 @@ abide by its terms.
244
272
[element]: https://github.com/syntax-tree/hast#element
245
273
246
274
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
275
+
276
+ [totext]: #totexttree-options
277
+
278
+ [options]: #options
279
+
280
+ [whitespace]: #whitespace
0 commit comments