2021-09-11 17:43:07 +08:00
|
|
|
// Reference: https://github.com/mermaid-js/mermaid
|
|
|
|
|
|
|
|
import { PluginSimple } from "markdown-it/lib";
|
|
|
|
import { hash } from "@vuepress/utils";
|
|
|
|
|
|
|
|
const MermaidPlugin: PluginSimple = function (md) {
|
|
|
|
const fence = md.renderer.rules.fence;
|
|
|
|
md.renderer.rules.fence = (...args) => {
|
|
|
|
const [tokens, idx] = args;
|
|
|
|
const { info } = tokens[idx];
|
2021-09-11 18:37:02 +08:00
|
|
|
if (info.trim() === "mermaid") {
|
2021-09-11 17:43:07 +08:00
|
|
|
const token = tokens[idx];
|
|
|
|
const key = `mermaid_${hash(idx)}`;
|
|
|
|
let { content } = token;
|
2021-09-12 20:39:48 +08:00
|
|
|
return `<Mermaid identifier="${key}" graph="${encodeURI(
|
|
|
|
content
|
|
|
|
)}"></Mermaid>`;
|
2021-09-11 17:43:07 +08:00
|
|
|
}
|
|
|
|
const rawCode = fence(...args);
|
|
|
|
return `${rawCode}`;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export { MermaidPlugin };
|