Skip to content

Commit 57e2db1

Browse files
committed
fix: plugin recursion
1 parent 228606d commit 57e2db1

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

plugin.mjs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
1-
import { visit, SKIP } from "unist-util-visit";
2-
3-
/**
4-
* Perform the redaction transform by wrapping text nodes inside delete nodes
5-
*
6-
* @param node - tree to transform
7-
*/
8-
function redactTransformImpl(node, index, parent) {
9-
console.log("Found redacted", node.type);
10-
visit(node, "text", (child, childIndex, childParent) => {
11-
const maybeParent = childParent || parent;
12-
const maybeIndex = childIndex || index;
13-
if (maybeParent) {
14-
maybeParent.children[maybeIndex] = {
15-
type: "delete",
16-
children: [child],
17-
};
18-
}
19-
return SKIP;
20-
});
21-
return SKIP;
22-
}
1+
import { visit, SKIP, EXIT } from "unist-util-visit";
232

243
/**
254
* Locate `redacted` nodes, and redact their text children (recursively)
@@ -33,9 +12,17 @@ function redactTransform(node) {
3312
child.class.split(/\s/).some((class_) => class_ === "redacted")
3413
);
3514
};
36-
visit(node, test, (child, index, parent) =>
37-
redactTransformImpl(child, index, parent),
38-
);
15+
visit(node, test, (child, index, parent) => {
16+
// Wrap any child text nodes
17+
visit(child, "text", (textChild, textIndex, textParent) => {
18+
(textParent ?? parent).children[textParent ? textIndex : index] = {
19+
type: "delete",
20+
children: [textChild],
21+
};
22+
return SKIP;
23+
});
24+
return SKIP;
25+
});
3926
}
4027

4128
const plugin = {

0 commit comments

Comments
 (0)