Skip to content

Commit

Permalink
Add support for passing a list of nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 8, 2020
1 parent 238dde2 commit 871df2d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
module.exports = toString

// Get the text content of a node.
// Prefer the node’s plain-text fields, otherwise serialize its children.
// Prefer the node’s plain-text fields, otherwise serialize its children,
// and if the given value is an array, serialize the nodes in it.
function toString(node) {
return (
(node &&
(node.value ||
node.alt ||
node.title ||
('children' in node && all(node.children)))) ||
('children' in node && all(node.children)) ||
('length' in node && all(node)))) ||
''
)
}
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Get the text content of a [node][].
The algorithm checks `value` of `node`, then `alt`, and finally `title`.
If no value is found, the algorithm checks the children of `node` and joins them
(without spaces or newlines).
If the given node is in fact a list of nodes, serializes them.

> This is not a markdown to plain-text library.
> Use [`strip-markdown`][strip-markdown] for that.
Expand Down
37 changes: 16 additions & 21 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,44 @@ test('mdast-util-to-string', function(t) {
t.equal(toString(), '', 'should not fail on a missing node')
t.equal(toString(null), '', 'should not fail on `null` missing node')

t.equal(
toString({value: 'foo'}),
'foo',
'should not fail on unrecognised nodes'
)
t.equal(toString({value: 'foo'}), 'foo', 'should not fail on nodes w/o type')

t.equal(
toString({
value: 'foo',
children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]
alt: 'bar',
title: 'baz',
children: [{value: 'qux'}]
}),
'foo',
'should prefer `value` over all others'
)

t.equal(
toString({value: 'foo', alt: 'bar', title: 'baz'}),
'foo',
'should prefer `value` over `alt` or `title`'
)

t.equal(
toString({alt: 'bar', title: 'baz'}),
toString({alt: 'bar', title: 'baz', children: [{value: 'qux'}]}),
'bar',
'should prefer `alt` over `title`'
'should prefer `alt` over all others'
)

t.equal(
toString({
title: 'baz',
children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]
}),
toString({title: 'baz', children: [{value: 'qux'}]}),
'baz',
'should use `title` over `children`'
'should prefer `title` over all others'
)

t.equal(
toString({children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]}),
'foobarbaz',
'should prefer `children`'
'should serialize children'
)

t.equal(
toString([{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]),
'foobarbaz',
'should serialize a list of nodes'
)

t.equal(toString({}), '', 'should fall back on an empty string')
t.equal(toString({}), '', 'should produce an empty string otherwise')

t.end()
})

0 comments on commit 871df2d

Please sign in to comment.