Walk the tree only once

This commit is contained in:
Ben Edgington
2025-06-10 14:31:21 +01:00
parent 0dabc11908
commit f556af5e8f

View File

@@ -1,20 +1,20 @@
import { visit, SKIP } from 'unist-util-visit';
import { visit, CONTINUE, SKIP } from 'unist-util-visit';
// Clean up any weird HTML artefacts, especially those that fail validation
function cleanupHtml() {
return function (tree) {
// Remove `is:raw=""` that's on `code` elements, probably from Prism.
visit(tree, 'element', (node) => {
if (node.tagName == 'code') {
visit(tree, undefined, (node, index, parent) => {
// Remove `is:raw=""` that's on `code` elements, probably from Prism
if (node.type === 'element' && node.tagName === 'code') {
delete node.properties['is:raw'];
return CONTINUE;
}
// Remove all comments
if (node.type === 'comment') {
parent.children.splice(index, 1);
return SKIP;
}
});
// Remove any comments
visit(tree, 'comment', (node, index, parent) => {
parent.children.splice(index, 1);
return SKIP;
});
};
}