@@ -82,24 +82,25 @@ import {createState} from './state.js'
8282 * mdast tree.
8383 * @param {Options | null | undefined } [options]
8484 * Configuration (optional).
85- * @returns {HastNodes | undefined }
85+ * @returns {HastNodes }
8686 * hast tree.
8787 */
88- // To do: next major: always return a single `root`.
8988export function toHast ( tree , options ) {
9089 const state = createState ( tree , options )
9190 const node = state . one ( tree , undefined )
9291 const foot = footer ( state )
92+ /** @type {HastNodes } */
93+ const result = Array . isArray ( node )
94+ ? { type : 'root' , children : node }
95+ : node || { type : 'root' , children : [ ] }
9396
9497 if ( foot ) {
9598 // If there’s a footer, there were definitions, meaning block
9699 // content.
97- // So `node` is a parent node.
98- assert ( node )
99- assert ( 'children' in node )
100- node . children . push ( { type : 'text' , value : '\n' } , foot )
100+ // So `result` is a parent node.
101+ assert ( 'children' in result )
102+ result . children . push ( { type : 'text' , value : '\n' } , foot )
101103 }
102104
103- // To do: next major: always return root?
104- return Array . isArray ( node ) ? { type : 'root' , children : node } : node
105+ return result
105106}
0 commit comments