| import { h } from "hastscript";
|
| import { visit } from "unist-util-visit";
|
|
|
| const ADMONITION_TYPES = [
|
| "note",
|
| "tip",
|
| "important",
|
| "warning",
|
| "caution",
|
| "abstract",
|
| "summary",
|
| "tldr",
|
| "info",
|
| "todo",
|
| "success",
|
| "check",
|
| "done",
|
| "question",
|
| "help",
|
| "faq",
|
| "attention",
|
| "failure",
|
| "missing",
|
| "fail",
|
| "danger",
|
| "error",
|
| "bug",
|
| "example",
|
| "quote",
|
| "cite",
|
| ];
|
|
|
| export function parseDirectiveNode() {
|
| return (tree) => {
|
| visit(tree, (node) => {
|
| if (
|
| node.type === "containerDirective" ||
|
| node.type === "leafDirective" ||
|
| node.type === "textDirective"
|
| ) {
|
| const name = node.name ? node.name.toLowerCase() : "";
|
|
|
|
|
|
|
| if (
|
| node.type === "containerDirective" &&
|
| ADMONITION_TYPES.includes(name)
|
| ) {
|
| const type = name.toUpperCase();
|
|
|
|
|
| const firstChild = node.children[0];
|
| if (firstChild?.data?.directiveLabel) {
|
|
|
| if (
|
| firstChild.children.length > 0 &&
|
| firstChild.children[0].type === "text"
|
| ) {
|
| firstChild.children[0].value = `[!${type}] ${firstChild.children[0].value}`;
|
| } else {
|
| firstChild.children.unshift({
|
| type: "text",
|
| value: `[!${type}] `,
|
| });
|
| }
|
| } else {
|
|
|
| node.children.unshift({
|
| type: "paragraph",
|
| children: [{ type: "text", value: `[!${type}]` }],
|
| });
|
| }
|
|
|
|
|
| node.type = "blockquote";
|
| node.data = node.data || {};
|
| node.data.hName = "blockquote";
|
|
|
| delete node.data.hProperties;
|
| } else {
|
|
|
| const data = node.data || {};
|
| node.data = data;
|
| node.attributes = node.attributes || {};
|
|
|
|
|
| if (
|
| node.children.length > 0 &&
|
| node.children[0].data &&
|
| node.children[0].data.directiveLabel
|
| ) {
|
| node.attributes["has-directive-label"] = true;
|
| }
|
|
|
| const hast = h(node.name, node.attributes);
|
| data.hName = hast.tagName;
|
| data.hProperties = hast.properties;
|
| }
|
| }
|
| });
|
| };
|
| }
|
|
|