Skip to content

Commit 9d85fa7

Browse files
committed
Add support for passing a list of nodes
1 parent b77f47c commit 9d85fa7

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

Diff for: lib/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ function toHtml(node, options) {
1818
var quote = settings.quote || quotationMark
1919
var alternative = quote === quotationMark ? apostrophe : quotationMark
2020
var smart = settings.quoteSmart
21+
var value =
22+
node && typeof node === 'object' && 'length' in node
23+
? {type: 'root', children: node}
24+
: node
2125

2226
if (quote !== quotationMark && quote !== apostrophe) {
2327
throw new Error(
@@ -62,6 +66,6 @@ function toHtml(node, options) {
6266
close: settings.closeSelfClosing,
6367
closeEmpty: settings.closeEmptyElements
6468
},
65-
node
69+
value
6670
)
6771
}

Diff for: readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Yields:
4444

4545
### `toHtml(tree[, options])`
4646

47-
Serialize the given [**hast**][hast] [*tree*][tree].
47+
Serialize the given [**hast**][hast] [*tree*][tree] (or list of nodes).
4848

4949
###### `options.space`
5050

Diff for: test/core.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var test = require('tape')
44
var u = require('unist-builder')
5+
var h = require('hastscript')
56
var to = require('..')
67

78
test('toHtml()', function(t) {
@@ -21,5 +22,8 @@ test('toHtml()', function(t) {
2122
'should throw on unknown nodes'
2223
)
2324

25+
t.equal(to(h()), '<div></div>', 'should support a node')
26+
t.equal(to([h('b'), h('i')]), '<b></b><i></i>', 'should support an array')
27+
2428
t.end()
2529
})

Diff for: types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ declare namespace hastUtilToHtml {
175175
* @param options configuration for stringifier
176176
*/
177177
declare function hastUtilToHtml(
178-
tree: Node,
178+
tree: Node | Node[],
179179
options?: Partial<hastUtilToHtml.HastUtilToHtmlOptions>
180180
): string
181181

0 commit comments

Comments
 (0)