File tree Expand file tree Collapse file tree 3 files changed +35
-5
lines changed Expand file tree Collapse file tree 3 files changed +35
-5
lines changed Original file line number Diff line number Diff line change 20
20
* @property {Whitespace } whitespace
21
21
* @property {BreakBefore } breakBefore
22
22
* @property {BreakAfter } breakAfter
23
+ *
24
+ * @typedef Options
25
+ * Configuration.
26
+ * @property {Whitespace } [whitespace='normal']
27
+ * Initial CSS whitespace setting to use.
23
28
*/
24
29
25
30
import { convertElement } from 'hast-util-is-element'
@@ -103,15 +108,16 @@ const blockOrCaption = convertElement([
103
108
* CSS-supporting user agent.
104
109
*
105
110
* @param {HastNode } node
111
+ * @param {Options } [options={}]
106
112
* @returns {string }
107
113
*/
108
- export function toText ( node ) {
114
+ export function toText ( node , options = { } ) {
109
115
/** @type {Array.<HastChild> } */
110
116
// @ts -ignore looks like a parent.
111
117
const children = node . children || [ ]
112
118
const block = blockOrCaption ( node )
113
119
const whitespace = inferWhitespace ( node , {
114
- whitespace : 'normal' ,
120
+ whitespace : options . whitespace || 'normal' ,
115
121
breakBefore : false ,
116
122
breakAfter : false
117
123
} )
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ Delta echo foxtrot.
60
60
This package exports the following identifiers: ` toText ` .
61
61
There is no default export.
62
62
63
- ### ` toText(node) `
63
+ ### ` toText(node, options? ) `
64
64
65
65
Utility to get the plain-text value of a [ * node* ] [ node ] .
66
66
@@ -70,9 +70,9 @@ Utility to get the plain-text value of a [*node*][node].
70
70
* If ` node ` is a [ * root* ] [ root ] or [ * element* ] [ element ] , applies an algorithm
71
71
similar to the ` innerText ` getter as defined by [ HTML] [ ]
72
72
73
- ###### Parameters
73
+ ###### ` options.whitespace `
74
74
75
- * ` node ` ( [ ` Node ` ] [ node ] ) — Thing to stringify
75
+ Default whitespace setting to use ( ` 'normal' ` or ` 'pre' ` , default: ` 'normal' ` ).
76
76
77
77
###### Returns
78
78
Original file line number Diff line number Diff line change @@ -4,6 +4,30 @@ import {h} from 'hastscript'
4
4
import { toText } from './index.js'
5
5
6
6
test ( 'hast-util-to-text' , ( t ) => {
7
+ t . equal (
8
+ toText ( h ( 'div' , 'a\n b\t\nc' ) ) ,
9
+ 'a b c' ,
10
+ 'should default to `whitespace: normal`'
11
+ )
12
+
13
+ t . equal (
14
+ toText ( h ( 'div' , 'a\n b\t\nc' ) , { whitespace : 'pre' } ) ,
15
+ 'a\n b\t\nc' ,
16
+ 'should support `whitespace: pre`'
17
+ )
18
+
19
+ t . equal (
20
+ toText ( h ( 'div' , 'a\n b\t\nc' ) , { whitespace : 'pre-wrap' } ) ,
21
+ 'a\n b\t\nc' ,
22
+ 'should support `whitespace: pre-wrap`'
23
+ )
24
+
25
+ t . equal (
26
+ toText ( h ( 'div' , 'a\n b\t\nc' ) , { whitespace : 'nowrap' } ) ,
27
+ 'a\n b\t\nc' ,
28
+ 'should support `whitespace: nowrap`'
29
+ )
30
+
7
31
t . equal (
8
32
toText ( u ( 'doctype' , { name : 'html' } ) ) ,
9
33
'' ,
You can’t perform that action at this time.
0 commit comments