UNPKG

@dhlx/vitepress-plugin-mindmap

Version:
96 lines (95 loc) 3.21 kB
import * as path from "path"; import { Transformer } from "markmap-lib"; import * as fs from "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 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.extname(src); const dir = path.dirname(env.path); if (ext === ".md" && type !== void 0 && type !== null) { const content = fs.readFileSync(path.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; }; export { withMindMap as default };