Passing information between transformers? #100
-
I've got the following pipeline for parsing Markdown: unified()
.use(remarkParse)
.use(remarkRehype, {
allowDangerousHtml: true
})
.use(rehypeRaw) Essentially passing through Rehype again to extract any embedded HTML in the Markdown. However, on larger Markdown documents, this is expensive and often unnecessary (a lot of Markdown does not have embedded HTML). I want to modify remarkRehype so that, when it encounters an html node, it sets a "flag" on the processor which says "I've seen an HTML node, we need to run rehypeRaw". If the flag is not set, I skip rehypeRaw. Using the current interface, we can imagine something like this: unified()
.use(remarkParse)
.use(remarkRehype, {
allowDangerousHtml: true,
handlers: {
html(h, node) {
// set some sort of flag here
return defaultHtmlHandler(h, node);
},
}
})
.use((opts) => {
return function (tree) {
if (flag) {
return rehypeRaw(opts)(tree);
}
return tree;
};
}) What's the best way of passing this flag between the processors? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
What are you trying to achieve here? This feels like an XY problem.
Unified uses the Or it can be done on individual AST nodes https://github.com/syntax-tree/unist#node
I'm not sure what you mean by this. do you mean a transformer? or are there multiple instances of |
Beta Was this translation helpful? Give feedback.
What are you trying to achieve here? This feels like an XY problem.
rehypeRaw
scans forraw
nodes, if it doesn't find any, then it doesn't do anything.My guess is you are looking for a performance boost? If so, this likely wouldn't achieve your goal, skipping a scan here would have a negligible different on performance.
Unified uses the
data
attribute to pass around metadata.This can be done at a file level https://github.com/vfile/vf…