Skip to content

Commit 8d29193

Browse files
committed
Fix changing of input tree
1 parent 9640771 commit 8d29193

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

lib/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ import {createState} from './state.js'
2626
* mdast tree.
2727
*/
2828
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))
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: []}

lib/util/resolve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {import('../state.js').State} State
2+
* @typedef {import('../types.js').State} State
33
*/
44

55
/**

0 commit comments

Comments
 (0)