Adding a preprocessor to directives? #1354
-
I'm relatively new to the unified ecosystem (loving it) so this almost certainly has more to do with my lack of understanding, but I've scoured the docs and can't find an answer. Basically, my users don't like the directive syntax so I'm trying to come up with a more subtle syntax for them to use. Something like this A :nice[directive].
Some ((new:syntax)). # would become --> :new(value="syntax") The idea was that I could transform the second line into directive syntax by just find/replacing the text, and then the normal remark-directives library would do the rest of the parsing. I got this far... import remarkDirective from "remark-directive";
import remarkStringify from "remark-stringify";
import remarkParse from "remark-parse";
import { unified } from "unified";
import { findAndReplace } from "mdast-util-find-and-replace";
const content = `
A :nice[directive].
Some ((new:syntax)).
`;
const file = await unified()
.use(remarkParse)
.use(myRemarkPlugin)
//.use(remarkDirective). // <-- note that I have this commented out
.use(remarkStringify)
.process(content);
function myRemarkPlugin() {
return function (tree) {
findAndReplace(tree, [
[
/\(\(([a-zA-Z]+):([a-zA-Z]+)\)\)/g,
function (full, prop, val) {
return `:[${prop}]{ value="${val}"}`;
},
],
]);
};
}
console.log(String(file)); If i run that as-is, I get:
Exactly what I would expect, given that
The cause of this is that the colon in The part that is confusing me overall is that the I was expecting more like this behavior;
Thanks again for this awesome ecosystem/tooling and apologies if this is a stupid question / has been asked a million times. I did have a look and found a few people who looked like they were having similar problems but the threads all petered out. Similar-ish issues |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Welcome @mikeyhogarth! 👋 If you want to write a parser plugin, see
|
Beta Was this translation helpful? Give feedback.
Welcome @mikeyhogarth! 👋
There are two types of plugins, parser plugins and AST plugins.
parser plugins always run at the very beginning,
remark-gfm
andremark-directive
are examples of this.AST plugins run later and can work on the parsed content, your plugin is an example of one of these.
If you want to write a parser plugin, see