Skip to content

Commit 871df2d

Browse files
committed
Add support for passing a list of nodes
1 parent 238dde2 commit 871df2d

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
module.exports = toString
44

55
// Get the text content of a node.
6-
// Prefer the node’s plain-text fields, otherwise serialize its children.
6+
// Prefer the node’s plain-text fields, otherwise serialize its children,
7+
// and if the given value is an array, serialize the nodes in it.
78
function toString(node) {
89
return (
910
(node &&
1011
(node.value ||
1112
node.alt ||
1213
node.title ||
13-
('children' in node && all(node.children)))) ||
14+
('children' in node && all(node.children)) ||
15+
('length' in node && all(node)))) ||
1416
''
1517
)
1618
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Get the text content of a [node][].
4141
The algorithm checks `value` of `node`, then `alt`, and finally `title`.
4242
If no value is found, the algorithm checks the children of `node` and joins them
4343
(without spaces or newlines).
44+
If the given node is in fact a list of nodes, serializes them.
4445

4546
> This is not a markdown to plain-text library.
4647
> Use [`strip-markdown`][strip-markdown] for that.

test.js

+16-21
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,44 @@ test('mdast-util-to-string', function(t) {
77
t.equal(toString(), '', 'should not fail on a missing node')
88
t.equal(toString(null), '', 'should not fail on `null` missing node')
99

10-
t.equal(
11-
toString({value: 'foo'}),
12-
'foo',
13-
'should not fail on unrecognised nodes'
14-
)
10+
t.equal(toString({value: 'foo'}), 'foo', 'should not fail on nodes w/o type')
1511

1612
t.equal(
1713
toString({
1814
value: 'foo',
19-
children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]
15+
alt: 'bar',
16+
title: 'baz',
17+
children: [{value: 'qux'}]
2018
}),
2119
'foo',
2220
'should prefer `value` over all others'
2321
)
2422

2523
t.equal(
26-
toString({value: 'foo', alt: 'bar', title: 'baz'}),
27-
'foo',
28-
'should prefer `value` over `alt` or `title`'
29-
)
30-
31-
t.equal(
32-
toString({alt: 'bar', title: 'baz'}),
24+
toString({alt: 'bar', title: 'baz', children: [{value: 'qux'}]}),
3325
'bar',
34-
'should prefer `alt` over `title`'
26+
'should prefer `alt` over all others'
3527
)
3628

3729
t.equal(
38-
toString({
39-
title: 'baz',
40-
children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]
41-
}),
30+
toString({title: 'baz', children: [{value: 'qux'}]}),
4231
'baz',
43-
'should use `title` over `children`'
32+
'should prefer `title` over all others'
4433
)
4534

4635
t.equal(
4736
toString({children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]}),
4837
'foobarbaz',
49-
'should prefer `children`'
38+
'should serialize children'
39+
)
40+
41+
t.equal(
42+
toString([{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]),
43+
'foobarbaz',
44+
'should serialize a list of nodes'
5045
)
5146

52-
t.equal(toString({}), '', 'should fall back on an empty string')
47+
t.equal(toString({}), '', 'should produce an empty string otherwise')
5348

5449
t.end()
5550
})

0 commit comments

Comments
 (0)