remark-directive-rehype
Version:
Remark plugin to enable Markdown directives to be parsed as HTML.
56 lines • 1.8 kB
JavaScript
import { h } from 'hastscript';
import { map } from 'unist-util-map';
/**
* Type guard that checks whether a node is a directive node
* (`textDirective`, `leafDirective`, or `containerDirective`).
*/
const isDirectiveNode = (node) => {
const { type } = node;
return type === 'textDirective' || type === 'leafDirective' || type === 'containerDirective';
};
/**
* Maps a directive node to include hast data (`hName` and `hProperties`),
* so that `remark-rehype` can convert it to the corresponding HTML element.
* Non-directive nodes are returned unchanged.
*/
const mapDirectiveNode = (node) => {
if (isDirectiveNode(node)) {
const { properties, tagName } = h(node.name, node.attributes ?? {});
return {
...node,
data: {
...node.data,
hName: tagName,
hProperties: properties
}
};
}
return node;
};
const transformNodeTree = (nodeTree) => map(nodeTree, mapDirectiveNode);
/**
* Remark plugin that enables directives (parsed by `remark-directive`) to be
* rendered as HTML elements when using `remark-rehype`.
*
* @remarks
* This plugin must be used after `remark-directive` and before `remark-rehype`
* in the unified pipeline.
*
* @example
* ```ts
* import remarkDirective from 'remark-directive'
* import remarkDirectiveRehype from 'remark-directive-rehype'
* import remarkRehype from 'remark-rehype'
*
* const file = await unified()
* .use(remarkParse)
* .use(remarkDirective)
* .use(remarkDirectiveRehype)
* .use(remarkRehype)
* .use(rehypeStringify)
* .process('::my-component{title="Hello"}')
* ```
*/
const remarkDirectiveRehype = () => transformNodeTree;
export default remarkDirectiveRehype;
//# sourceMappingURL=index.js.map