From 871df2debecf5de6630040a53c79c93f80e82426 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sun, 8 Mar 2020 10:04:51 +0100 Subject: [PATCH] Add support for passing a list of nodes --- index.js | 6 ++++-- readme.md | 1 + test.js | 37 ++++++++++++++++--------------------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 8f7e003..2b9df1c 100644 --- a/index.js +++ b/index.js @@ -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)))) || '' ) } diff --git a/readme.md b/readme.md index f506c74..f816fdd 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/test.js b/test.js index dc58608..d7fdaa5 100644 --- a/test.js +++ b/test.js @@ -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() })