Having troubles creating a plugin to manipulate a markdown syntax tree #162
Replies
|
@kvietcong sorry you ran into a spot of trouble. source: https://github.com/unifiedjs/unified#description
const remarkFixMermaid = options => {
return ast => {
visit(ast, "code", node => {
if (node.lang === "mermaid") {
node.type = "mermaid";
}
return node;
});
}
}
const ast = unified()
.use(remarkParse)
.parse(testInput);
const transformedAst = unified()
.use(remarkFixMermaid, {test: "test"})
.run(ast);
console.log(transformedAst);or, an equivalent solution like: const remarkFixMermaid = options => {
return ast => {
visit(ast, "code", node => {
if (node.lang === "mermaid") {
node.type = "mermaid";
}
return node;
});
}
}
const pipeline = unified()
.use(remarkParse)
.use(remarkFixMermaid, {test: "test"})
const ast = pipeline.parse(testInput);
const transformedAst = pipeline.run(ast);
console.log(transformedAst); |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Hi I'm working with Next.JS right now and as apart of the static generation, I wanted to parse out some markdown. However, I wanted to do something special with the mermaid code blocks and so I'm trying to manipulate them. However, it seems that when I try running this code, the tree is not impacted and it seems the calling stops at the first level. Only
"Is this even called?"and theoptionsare logged. It seems like the transformer isn't even run. I have also tried using regular functions but that didn't help. Thanks for any help!Current Code
Example input of test
Example output of test
{ "type": "root", "children": [ { "type": "heading", "depth": 1, "children": [ { "type": "text", "value": "Hi there", "position": { "start": { "line": 2, "column": 3, "offset": 3 }, "end": { "line": 2, "column": 11, "offset": 11 } } } ], "position": { "start": { "line": 2, "column": 1, "offset": 1 }, "end": { "line": 2, "column": 11, "offset": 11 } } }, { "type": "paragraph", "children": [ { "type": "text", "value": "This is a test", "position": { "start": { "line": 3, "column": 1, "offset": 12 }, "end": { "line": 3, "column": 15, "offset": 26 } } } ], "position": { "start": { "line": 3, "column": 1, "offset": 12 }, "end": { "line": 3, "column": 15, "offset": 26 } } }, { "type": "code", "lang": "mermaid", "meta": null, "value": " graph TD;\n A ==> B", "position": { "start": { "line": 5, "column": 1, "offset": 28 }, "end": { "line": 8, "column": 4, "offset": 72 } } } ], "position": { "start": { "line": 1, "column": 1, "offset": 0 }, "end": { "line": 9, "column": 1, "offset": 73 } } }@kvietcong sorry you ran into a spot of trouble.
Some context on the different ways to call unified:
source: https://github.com/unifiedjs/unified#description
unified.parsewill generate a syntax tree, but do…