UNPKG

@dhlx/vitepress-plugin-mindmap

Version:
113 lines (112 loc) 3.83 kB
"use strict"; const path = require("path"); const markmapLib = require("markmap-lib"); const fs = require("fs"); function _interopNamespaceDefault(e) { const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } }); if (e) { for (const k in e) { if (k !== "default") { const d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: () => e[k] }); } } } n.default = e; return Object.freeze(n); } const path__namespace = /* @__PURE__ */ _interopNamespaceDefault(path); const fs__namespace = /* @__PURE__ */ _interopNamespaceDefault(fs); function drawioPlugin() { return { name: "drawio-plugin", enforce: "post", async transform(src, id) { if (!id.includes("vitepress/dist/client/app/index.js")) { return; } src = "\nimport MindMapView from '@dhlx/vitepress-plugin-mindmap/MindMapView.vue';\n" + src; const lines = src.split("\n"); const targetLineIndex = lines.findIndex( (line) => line.includes("app.component") ); lines.splice( targetLineIndex, 0, ' app.component("MindMapView", MindMapView);' ); src = lines.join("\n"); return { code: src, map: null // provide source map if available }; } }; } const transformer = new markmapLib.Transformer(); function escapeHtml(unsafe) { return unsafe.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"); } const withMindMap = (config) => { if (!config.markdown) config.markdown = {}; if (!config.vite) config.vite = {}; if (!config.markdown.config) config.markdown.config = () => { }; if (!config.vite.plugins) config.vite.plugins = []; config.vite.plugins.push(drawioPlugin()); const _config = config.markdown.config; config.markdown.config = function(md) { _config(md); const _image = md.renderer.rules.image; if (!_image) return; md.renderer.rules.image = (tokens, idx, options, env, self) => { if (!_image) return _image; const token = tokens[idx]; const result = _image(tokens, idx, options, env, self); const src = token.attrGet("src"); const type = token.attrGet("mindmap"); if (!src) return result; const ext = path__namespace.extname(src); const dir = path__namespace.dirname(env.path); if (ext === ".md" && type !== void 0 && type !== null) { const content = fs__namespace.readFileSync(path__namespace.join(dir, src), "utf-8").toString(); try { const { root } = transformer.transform(content); return ` <ClientOnly> <MindMapView data="${escapeHtml(JSON.stringify(root))}" type="file"></MindMapView> </ClientOnly> `; } catch (e) { return `<pre> ${e} </pre>`; } } return result; }; const _fence = md.renderer.rules.fence?.bind(md.renderer.rules.fence); if (!_fence) return; md.renderer.rules.fence = (tokens, idx, options, env, self) => { const token = tokens[idx]; if (token.info === "mindmap") { try { const content = token.content.trim(); const { root } = transformer.transform(content); return `<ClientOnly> <MindMapView data="${escapeHtml(JSON.stringify(root))}" type="fence"> </MindMapView> </ClientOnly> `; } catch (e) { return `<pre> ${e} </pre>`; } } return _fence(tokens, idx, options, env, self); }; }; return config; }; module.exports = withMindMap;