1+ /**
2+ * @typedef {import('./lib/types.js').Node } Node
3+ * @typedef {import('./lib/types.js').Element } Element
4+ * @typedef {import('./lib/types.js').Text } Text
5+ * @typedef {import('./lib/types.js').Options } Options
6+ * @typedef {import('./lib/types.js').Context } Context
7+ * @typedef {import('./lib/types.js').Properties } Properties
8+ * @typedef {import('./lib/types.js').H } H
9+ * @typedef {import('./lib/types.js').HWithoutProps } HWithoutProps
10+ * @typedef {import('./lib/types.js').HWithProps } HWithProps
11+ * @typedef {import('./lib/types.js').MdastNode } MdastNode
12+ * @typedef {import('./lib/types.js').MdastRoot } MdastRoot
13+ */
14+
115import { hasProperty } from 'hast-util-has-property'
2- import minify from 'rehype-minify-whitespace'
16+ import minifyWhitespace from 'rehype-minify-whitespace'
317import { convert } from 'unist-util-is'
418import { visit } from 'unist-util-visit'
519import { one } from './lib/one.js'
@@ -8,70 +22,97 @@ import {own} from './lib/util/own.js'
822
923var block = convert ( [ 'heading' , 'paragraph' , 'root' ] )
1024
11- export function toMdast ( tree , options ) {
12- var settings = options || { }
25+ /**
26+ * @param {Node } tree
27+ * @param {Options } [options]
28+ */
29+ export function toMdast ( tree , options = { } ) {
30+ /** @type {Object.<string, Element> } */
1331 var byId = { }
32+ /** @type {MdastNode|Array.<MdastNode>|void } */
33+ var result
34+ /** @type {MdastNode|MdastRoot } */
1435 var mdast
1536
16- h . nodeById = byId
17- h . baseFound = false
18- h . frozenBaseUrl = null
19- h . wrapText = true
20- h . qNesting = 0
21-
22- h . handlers = settings . handlers
23- ? { ...handlers , ...settings . handlers }
24- : handlers
25- h . augment = augment
26-
27- h . document = settings . document
28- h . checked = settings . checked || '[x]'
29- h . unchecked = settings . unchecked || '[ ]'
30- h . quotes = settings . quotes || [ '"' ]
37+ /**
38+ * @type {H }
39+ */
40+ var h = Object . assign (
41+ /**
42+ * @type {HWithProps & HWithoutProps }
43+ */
44+ (
45+ /**
46+ * @param {Node } node
47+ * @param {string } type
48+ * @param {Properties|string|Array.<Node> } [props]
49+ * @param {string|Array.<Node> } [children]
50+ */
51+ function ( node , type , props , children ) {
52+ /** @type {Node } */
53+ var result
54+ /** @type {Properties } */
55+ var properties
56+
57+ if ( typeof props === 'string' || Array . isArray ( props ) ) {
58+ children = props
59+ properties = { }
60+ } else {
61+ properties = props
62+ }
63+
64+ // @ts -ignore Assume valid `type` and `children`/`value`.
65+ result = { type, ...properties }
66+
67+ if ( typeof children === 'string' ) {
68+ result . value = children
69+ } else if ( children ) {
70+ result . children = children
71+ }
72+
73+ if ( node . position ) {
74+ result . position = node . position
75+ }
76+
77+ return result
78+ }
79+ ) ,
80+ {
81+ nodeById : byId ,
82+ baseFound : false ,
83+ wrapText : true ,
84+ /** @type {string|null } */
85+ frozenBaseUrl : null ,
86+ qNesting : 0 ,
87+ handlers : options . handlers
88+ ? { ...handlers , ...options . handlers }
89+ : handlers ,
90+ document : options . document ,
91+ checked : options . checked || '[x]' ,
92+ unchecked : options . unchecked || '[ ]' ,
93+ quotes : options . quotes || [ '"' ]
94+ }
95+ )
3196
3297 visit ( tree , 'element' , onelement )
3398
34- minify ( { newlines : settings . newlines === true } ) ( tree )
35-
36- mdast = one ( h , tree , null )
37-
38- visit ( mdast , 'text' , ontext )
39-
40- return mdast
99+ minifyWhitespace ( { newlines : options . newlines === true } ) ( tree )
41100
42- function h ( node , type , props , children ) {
43- var result
101+ result = one ( h , tree , null )
44102
45- if (
46- ! children &&
47- ( typeof props === 'string' ||
48- ( typeof props === 'object' && 'length' in props ) )
49- ) {
50- children = props
51- props = { }
52- }
53-
54- result = { type, ...props }
55-
56- if ( typeof children === 'string' ) {
57- result . value = children
58- } else if ( children ) {
59- result . children = children
60- }
61-
62- return augment ( node , result )
103+ if ( ! result ) {
104+ mdast = { type : 'root' , children : [ ] }
105+ } else if ( Array . isArray ( result ) ) {
106+ mdast = { type : 'root' , children : result }
107+ } else {
108+ mdast = result
63109 }
64110
65- // To do: inline in a future major.
66- // `right` is the finalized mdast node, created from `left`, a hast node.
67- function augment ( left , right ) {
68- if ( left . position ) {
69- right . position = left . position
70- }
111+ visit ( mdast , 'text' , ontext )
71112
72- return right
73- }
113+ return mdast
74114
115+ /** @type {import('unist-util-visit').Visitor<Element> } */
75116 function onelement ( node ) {
76117 var id = hasProperty ( node , 'id' ) && String ( node . properties . id ) . toUpperCase ( )
77118
@@ -80,11 +121,15 @@ export function toMdast(tree, options) {
80121 }
81122 }
82123
83- // Collapse text nodes, and fix whitespace.
84- // Most of this is taken care of by `rehype-minify-whitespace`, but
85- // we’re generating some whitespace too, and some nodes are in the end
86- // ignored.
87- // So clean up:
124+ /**
125+ * Collapse text nodes, and fix whitespace.
126+ * Most of this is taken care of by `rehype-minify-whitespace`, but
127+ * we’re generating some whitespace too, and some nodes are in the end
128+ * ignored.
129+ * So clean up.
130+ *
131+ * @type {import('unist-util-visit').Visitor<Text> }
132+ */
88133 function ontext ( node , index , parent ) {
89134 var previous = parent . children [ index - 1 ]
90135
0 commit comments