@@ -33,15 +33,13 @@ import {all} from './all.js'
3333 */
3434export function toMdast ( tree , options ) {
3535 const options_ = options || { }
36- /** @type {Record<string, Element> } */
37- const byId = Object . create ( null )
3836 /** @type {State } */
3937 const state = {
4038 patch,
4139 all : allBound ,
4240 one : oneBound ,
4341 options : options_ ,
44- nodeById : byId ,
42+ elementById : new Map ( ) ,
4543 handlers : options_ . handlers
4644 ? { ...handlers , ...options_ . handlers }
4745 : handlers ,
@@ -53,14 +51,13 @@ export function toMdast(tree, options) {
5351 /** @type {MdastNode | MdastRoot } */
5452 let mdast
5553
56- visit ( tree , 'element' , ( node ) => {
57- const id =
58- node . properties &&
59- 'id' in node . properties &&
60- String ( node . properties . id ) . toUpperCase ( )
54+ visit ( tree , function ( node ) {
55+ if ( node && node . type === 'element' && node . properties ) {
56+ const id = String ( node . properties . id || '' ) || undefined
6157
62- if ( id && ! ( id in byId ) ) {
63- byId [ id ] = node
58+ if ( id && ! state . elementById . has ( id ) ) {
59+ state . elementById . set ( id , node )
60+ }
6461 }
6562 } )
6663
@@ -79,63 +76,55 @@ export function toMdast(tree, options) {
7976 mdast = result
8077 }
8178
82- visit ( mdast , 'text' , ontext )
83-
84- return mdast
85-
86- /**
87- * Collapse text nodes, and fix whitespace.
88- * Most of this is taken care of by `rehype-minify-whitespace`, but
89- * we’re generating some whitespace too, and some nodes are in the end
90- * ignored.
91- * So clean up.
92- *
93- * @type {import('unist-util-visit/complex-types.js').BuildVisitor<MdastRoot, 'text'> }
94- */
95- function ontext ( node , index , parent ) {
96- /* c8 ignore next 3 */
97- if ( index === null || ! parent ) {
98- return
99- }
100-
101- const previous = parent . children [ index - 1 ]
102-
103- if ( previous && previous . type === node . type ) {
104- previous . value += node . value
105- parent . children . splice ( index , 1 )
106-
107- if ( previous . position && node . position ) {
108- previous . position . end = node . position . end
79+ // Collapse text nodes, and fix whitespace.
80+ //
81+ // Most of this is taken care of by `rehype-minify-whitespace`, but
82+ // we’re generating some whitespace too, and some nodes are in the end
83+ // ignored.
84+ // So clean up.
85+ visit ( mdast , function ( node , index , parent ) {
86+ if ( node . type === 'text' && index !== null && parent ) {
87+ const previous = parent . children [ index - 1 ]
88+
89+ if ( previous && previous . type === node . type ) {
90+ previous . value += node . value
91+ parent . children . splice ( index , 1 )
92+
93+ if ( previous . position && node . position ) {
94+ previous . position . end = node . position . end
95+ }
96+
97+ // Iterate over the previous node again, to handle its total value.
98+ return index - 1
10999 }
110100
111- // Iterate over the previous node again, to handle its total value.
112- return index - 1
113- }
114-
115- node . value = node . value . replace ( / [ \t ] * ( \r ? \n | \r ) [ \t ] * / , '$1' )
116-
117- // We don’t care about other phrasing nodes in between (e.g., `[ asd ]()`),
118- // as there the whitespace matters.
119- if (
120- parent &&
121- ( parent . type === 'heading' ||
122- parent . type === 'paragraph' ||
123- parent . type === 'root' )
124- ) {
125- if ( ! index ) {
126- node . value = node . value . replace ( / ^ [ \t ] + / , '' )
101+ node . value = node . value . replace ( / [ \t ] * ( \r ? \n | \r ) [ \t ] * / , '$1' )
102+
103+ // We don’t care about other phrasing nodes in between (e.g., `[ asd ]()`),
104+ // as there the whitespace matters.
105+ if (
106+ parent &&
107+ ( parent . type === 'heading' ||
108+ parent . type === 'paragraph' ||
109+ parent . type === 'root' )
110+ ) {
111+ if ( ! index ) {
112+ node . value = node . value . replace ( / ^ [ \t ] + / , '' )
113+ }
114+
115+ if ( index === parent . children . length - 1 ) {
116+ node . value = node . value . replace ( / [ \t ] + $ / , '' )
117+ }
127118 }
128119
129- if ( index === parent . children . length - 1 ) {
130- node . value = node . value . replace ( / [ \t ] + $ / , '' )
120+ if ( ! node . value ) {
121+ parent . children . splice ( index , 1 )
122+ return index
131123 }
132124 }
125+ } )
133126
134- if ( ! node . value ) {
135- parent . children . splice ( index , 1 )
136- return index
137- }
138- }
127+ return mdast
139128}
140129
141130export { handlers as defaultHandlers } from './handlers/index.js'
0 commit comments