@@ -26,12 +26,18 @@ import {createState} from './state.js'
26
26
* mdast tree.
27
27
*/
28
28
export 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 ) )
29
33
const options_ = options || { }
30
34
const state = createState ( options_ )
31
35
/** @type {MdastNode | MdastRoot } */
32
36
let mdast
33
37
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 ) {
35
41
if ( node && node . type === 'element' && node . properties ) {
36
42
const id = String ( node . properties . id || '' ) || undefined
37
43
@@ -41,11 +47,7 @@ export function toMdast(tree, options) {
41
47
}
42
48
} )
43
49
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 )
49
51
50
52
if ( ! result ) {
51
53
mdast = { type : 'root' , children : [ ] }
0 commit comments