11/**
2- * @typedef {import('unist').Node } Node
3- * @typedef {import('unist').Parent } Parent
4- * @typedef {import('mdast').Literal } Literal
5- * @typedef {import('mdast').Text } Text
2+ * @typedef {import('mdast').Root|import('mdast').Parent['children'][number] } MdastNode
63 * @typedef {import('./index.js').H } H
74 * @typedef {import('./index.js').Handler } Handler
85 * @typedef {import('./index.js').Content } Content
@@ -15,10 +12,19 @@ const own = {}.hasOwnProperty
1512/**
1613 * Transform an unknown node.
1714 * @type {Handler }
18- * @param {Node } node
15+ * @param {MdastNode } node
1916 */
2017function unknown ( h , node ) {
21- if ( text ( node ) ) {
18+ const data = node . data || { }
19+
20+ if (
21+ 'value' in node &&
22+ ! (
23+ own . call ( data , 'hName' ) ||
24+ own . call ( data , 'hProperties' ) ||
25+ own . call ( data , 'hChildren' )
26+ )
27+ ) {
2228 return h . augment ( node , u ( 'text' , node . value ) )
2329 }
2430
@@ -27,7 +33,7 @@ function unknown(h, node) {
2733
2834/**
2935 * @type {Handler }
30- * @param {Node } node
36+ * @param {MdastNode } node
3137 */
3238export function one ( h , node , parent ) {
3339 const type = node && node . type
@@ -50,69 +56,50 @@ export function one(h, node, parent) {
5056 return ( typeof fn === 'function' ? fn : unknown ) ( h , node , parent )
5157}
5258
53- /**
54- * Check if the node should be renderered as a text node.
55- * @param {Node } node
56- * @returns {node is Literal }
57- */
58- function text ( node ) {
59- const data = node . data || { }
60-
61- if (
62- own . call ( data , 'hName' ) ||
63- own . call ( data , 'hProperties' ) ||
64- own . call ( data , 'hChildren' )
65- ) {
66- return false
67- }
68-
69- return 'value' in node
70- }
71-
7259/**
7360 * @type {Handler }
74- * @param {unknown } node
61+ * @param {MdastNode } node
7562 */
7663function returnNode ( h , node ) {
77- // @ts -expect-error pass through any unknown node.
78- return node . children ? { ...node , children : all ( h , node ) } : node
64+ // @ts -expect-error: Pass through custom node.
65+ return ' children' in node ? { ...node , children : all ( h , node ) } : node
7966}
8067
8168/**
8269 * @param {H } h
83- * @param {Node } parent
70+ * @param {MdastNode } parent
8471 */
8572export function all ( h , parent ) {
86- /** @type {Array.<Node> } */
87- // @ts -expect-error looks like a parent.
88- const nodes = parent . children || [ ]
8973 /** @type {Array.<Content> } */
9074 const values = [ ]
91- let index = - 1
9275
93- while ( ++ index < nodes . length ) {
94- // @ts -expect-error looks like a parent.
95- const result = one ( h , nodes [ index ] , parent )
76+ if ( 'children' in parent ) {
77+ const nodes = parent . children
78+ let index = - 1
9679
97- if ( result ) {
98- if ( index && nodes [ index - 1 ] . type === 'break' ) {
99- if ( ! Array . isArray ( result ) && result . type === 'text' ) {
100- result . value = result . value . replace ( / ^ \s + / , '' )
101- }
80+ while ( ++ index < nodes . length ) {
81+ const result = one ( h , nodes [ index ] , parent )
10282
103- if ( ! Array . isArray ( result ) && result . type === 'element' ) {
104- const head = result . children [ 0 ]
83+ if ( result ) {
84+ if ( index && nodes [ index - 1 ] . type === 'break' ) {
85+ if ( ! Array . isArray ( result ) && result . type === 'text' ) {
86+ result . value = result . value . replace ( / ^ \s + / , '' )
87+ }
88+
89+ if ( ! Array . isArray ( result ) && result . type === 'element' ) {
90+ const head = result . children [ 0 ]
10591
106- if ( head && head . type === 'text' ) {
107- head . value = head . value . replace ( / ^ \s + / , '' )
92+ if ( head && head . type === 'text' ) {
93+ head . value = head . value . replace ( / ^ \s + / , '' )
94+ }
10895 }
10996 }
110- }
11197
112- if ( Array . isArray ( result ) ) {
113- values . push ( ...result )
114- } else {
115- values . push ( result )
98+ if ( Array . isArray ( result ) ) {
99+ values . push ( ...result )
100+ } else {
101+ values . push ( result )
102+ }
116103 }
117104 }
118105 }
0 commit comments