Skip to content

Commit a08ae6e

Browse files
committed
Add support for options.whitespace
Related to remarkjs/remark-math#61. Related to remarkjs/remark-math#62.
1 parent c6f5df6 commit a08ae6e

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
* @property {Whitespace} whitespace
2121
* @property {BreakBefore} breakBefore
2222
* @property {BreakAfter} breakAfter
23+
*
24+
* @typedef Options
25+
* Configuration.
26+
* @property {Whitespace} [whitespace='normal']
27+
* Initial CSS whitespace setting to use.
2328
*/
2429

2530
import {convertElement} from 'hast-util-is-element'
@@ -103,15 +108,16 @@ const blockOrCaption = convertElement([
103108
* CSS-supporting user agent.
104109
*
105110
* @param {HastNode} node
111+
* @param {Options} [options={}]
106112
* @returns {string}
107113
*/
108-
export function toText(node) {
114+
export function toText(node, options = {}) {
109115
/** @type {Array.<HastChild>} */
110116
// @ts-ignore looks like a parent.
111117
const children = node.children || []
112118
const block = blockOrCaption(node)
113119
const whitespace = inferWhitespace(node, {
114-
whitespace: 'normal',
120+
whitespace: options.whitespace || 'normal',
115121
breakBefore: false,
116122
breakAfter: false
117123
})

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Delta echo foxtrot.
6060
This package exports the following identifiers: `toText`.
6161
There is no default export.
6262

63-
### `toText(node)`
63+
### `toText(node, options?)`
6464

6565
Utility to get the plain-text value of a [*node*][node].
6666

@@ -70,9 +70,9 @@ Utility to get the plain-text value of a [*node*][node].
7070
* If `node` is a [*root*][root] or [*element*][element], applies an algorithm
7171
similar to the `innerText` getter as defined by [HTML][]
7272

73-
###### Parameters
73+
###### `options.whitespace`
7474

75-
* `node` ([`Node`][node]) — Thing to stringify
75+
Default whitespace setting to use (`'normal'` or `'pre'`, default: `'normal'`).
7676

7777
###### Returns
7878

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ import {h} from 'hastscript'
44
import {toText} from './index.js'
55

66
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+
731
t.equal(
832
toText(u('doctype', {name: 'html'})),
933
'',

0 commit comments

Comments
 (0)