@@ -26,12 +26,18 @@ import {createState} from './state.js'
2626 * mdast tree.
2727 */
2828export function toMdast ( tree , options ) {
29+ // We have to clone, cause we’ll use `rehype-minify-whitespace` on the tree,
30+ // which modifies
31+ /** @type {Node } */
32+ const cleanTree = JSON . parse ( JSON . stringify ( tree ) )
2933 const options_ = options || { }
3034 const state = createState ( options_ )
3135 /** @type {MdastNode | MdastRoot } */
3236 let mdast
3337
34- visit ( tree , function ( node ) {
38+ // @ts -expect-error: does return a transformer, that does accept any node.
39+ rehypeMinifyWhitespace ( { newlines : options_ . newlines === true } ) ( cleanTree )
40+ visit ( cleanTree , function ( node ) {
3541 if ( node && node . type === 'element' && node . properties ) {
3642 const id = String ( node . properties . id || '' ) || undefined
3743
@@ -41,11 +47,7 @@ export function toMdast(tree, options) {
4147 }
4248 } )
4349
44- // To do: clone the tree.
45- // @ts -expect-error: does return a transformer, that does accept any node.
46- rehypeMinifyWhitespace ( { newlines : options_ . newlines === true } ) ( tree )
47-
48- const result = state . one ( tree , undefined )
50+ const result = state . one ( cleanTree , undefined )
4951
5052 if ( ! result ) {
5153 mdast = { type : 'root' , children : [ ] }
0 commit comments