-
For my particular application, there are some situations where I would like to automatically replace text in markdown files based on rules defined by retext plugins. One simple example would be "smart quotes". To illustrate this, I've created a CodeSandbox demo to replace smart quotes with dumb quotes. Here's a summary with many of the details commented out: const sourceMarkdown = "They’re “smart” quotes!";
const remark_replace = () => {
// a transformer to apply fixes from certain messages
return (tree, file) => {
const fixes = [];
file.messages.forEach((message) => {
// push relevant message offsets and expected values to fixes
...
}
});
return unist_map(tree, (node) => {
if (unist_is(node, "text")) {
// extract node start and end offsets
...
// filter fixes within start and end offsets
const node_fixes = fixes.filter(...)
...
// set node value with relevant fixes applied
node.value = ...
}
return node;
});
};
}
const md_to_md_processor = unified()
.use(remark_parse)
.use(
remark_retext,
unified().use(retext_english).use(retext_quotes, { preferred: "straight" })
)
.use(remark_replace)
.use(remark_stringify);
const { contents } = md_to_md_processor().processSync(sourceMarkdown); I'm sorry if this question has already been raised or dismissed, but I've been searching Github for a few days and haven't found anyone who has been asking similar questions. I'd be happy to be pointed in that direction as well. In theory, such a plugin could allow custom handlers for each I'd be happy to start work on such a plugin, unless I'm missing any issues that would render this project a non-starter. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hi there! I believe that should somewhat be possible yes! Although, unified processors also change the AST and then serialize it. Although, I’m a fan of ASTs. And this is string mutations. String mutations is what ESLint also does, and they’re a bit unhappy with them (a small typo and it could accidentally create invalid code). Which is why I’d prefer CSTs instead. See also: remarkjs/remark-lint#82, retextjs/retext-repeated-words#3, retextjs/retext-readability#8, retextjs/retext-readability#8 |
Beta Was this translation helpful? Give feedback.
-
I believe I've done it! Here's the plugin on CodeSandbox. I'm going with a tentative name of TLDR: Unless anyone has anything to add at this stage, I'll make a repository for My plugin applies messages to all Here's an example showing all three This input markdown
produces this output markdown
Here's how a user would apply the const md_to_md_processor = unified()
.use(remark_parse)
.use(
remark_retext,
unified()
.use(retext_english)
.use(retext_spacing)
.use(retext_spell, dictionary)
.use(retext_repeated)
)
.use(remark_autofix)
.use(remark_stringify); The plugin as written currently modifies the syntax tree in the To better integrate with the |
Beta Was this translation helpful? Give feedback.
-
I've published a remark plugin to NPM that applies fixes to the MDAST during the |
Beta Was this translation helpful? Give feedback.
I've published a remark plugin to NPM that applies fixes to the MDAST during the
Transforms
stage. I'll be adding various tests and thinking of edge cases for a while, but I'll go ahead and mark this question as answered.