-
The current implementation of node removal is implemented as below. if (is(node, index, parent)) {
return null
}
if (children.length > 0) {
// Move all living children to the beginning of the children array.
while (++childIndex < children.length) {
// @ts-expect-error looks like a parent.
if (preorder(children[childIndex], childIndex, node)) {
children[position++] = children[childIndex]
}
}
// Cascade delete.
if (cascade && !position) {
return null
}
// Drop other nodes.
children.length = position
} it seems we need to filter out the selected node and repositioning other child nodes one by one recursively. it is another version out there from googling visit(tree, 'emphasis', function (node, index, parent) {
parent.children.splice(index, 1)
// Do not traverse `node`, continue at the node *now* at `index`.
return [SKIP, index]
}) It is much simpler and need not to reposition the child nodes. just remove the target node an skip transversal of node and continue visiting at the current index. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You’re comparing the internal code of I recommend using |
Beta Was this translation helpful? Give feedback.
You’re comparing the internal code of
remove
to the code you write withvisit
, which are not equal.The code you write for
remove
would be'emphasis'
, to remove all emphasis.The code in
visit
is pretty complex.I recommend using
unist-util-visit
because it’s very powerful and supports different cases: adding, removing, replacing, everything.Importantly, it allow you to do different things in one “walk of the tree”.
With
remove
, if you also want to do other things, you need to walk the tree multiple times.That’s typically the slowest thing: walking the tree many times.