fix: parse ::directives correctly (#41186)

This commit is contained in:
Oliver Eyton-Williams
2021-02-22 04:44:12 +01:00
committed by GitHub
parent b12360d4a8
commit 04c2f4e620
15 changed files with 842 additions and 21 deletions

View File

@ -0,0 +1,32 @@
const visit = require('unist-util-visit');
const { matches } = require('unist-util-select');
const directive = require('mdast-util-directive');
var toMarkdown = require('mdast-util-to-markdown');
function plugin() {
return transformer;
function transformer(tree) {
visit(tree, visitor);
function visitor(node, id, parent) {
// currently `remark-directive` seems to be ignoring containerDirectives
// but, assuming that will get fixed, we test for it anyway.
const isDirective =
matches('leafDirective', node) ||
matches('textDirective', node) ||
matches('containerDirective', node);
if (isDirective) {
parent.children[id] = {
type: 'text',
value: toMarkdown(node, {
extensions: [directive.toMarkdown]
}).trim()
};
}
}
}
}
module.exports = plugin;