UNPKG

vue-renderer-markdown

Version:

Vue 3 Markdown renderer optimized for large docs: progressive Mermaid, streaming diff code blocks, and fast real-time preview.

118 lines (117 loc) 4.08 kB
var __getOwnPropNames = Object.getOwnPropertyNames; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; import mermaid from "mermaid"; var require_mermaidParser_worker = __commonJS({ "workers/mermaidParser.worker.js"(exports) { mermaid.initialize({ startOnLoad: false, securityLevel: "loose" }); function applyThemeTo(code, theme) { const themeValue = theme === "dark" ? "dark" : "default"; const themeConfig = `%%{init: {"theme": "${themeValue}"}}%% `; const trimmed = code.trimStart(); if (trimmed.startsWith("%%{")) return code; return themeConfig + code; } function findHeaderIndex(lines) { const headerRe = /^(?:graph|flowchart|flowchart\s+tb|flowchart\s+lr|sequenceDiagram|gantt|classDiagram|stateDiagram(?:-v2)?|erDiagram|journey|pie|quadrantChart|timeline|xychart(?:-beta)?)\b/; for (let i = 0; i < lines.length; i++) { const l = lines[i].trim(); if (!l) continue; if (l.startsWith("%%")) continue; if (headerRe.test(l)) return i; } return -1; } function canParse(code, theme) { return __async(this, null, function* () { var _a; const themed = applyThemeTo(code, theme); const anyMermaid = mermaid; if (typeof anyMermaid.parse === "function") { yield (_a = anyMermaid.parse) == null ? void 0 : _a.call(anyMermaid, themed); return true; } throw new Error("mermaid.parse not available in worker"); }); } function findLastRenderablePrefix(baseCode, theme) { return __async(this, null, function* () { const lines = baseCode.split("\n"); const headerIdx = findHeaderIndex(lines); if (headerIdx === -1) return null; const head = lines.slice(0, headerIdx + 1); yield canParse(head.join("\n"), theme); let low = headerIdx + 1; let high = lines.length; let lastGood = headerIdx + 1; let tries = 0; const MAX_TRIES = 12; while (low <= high && tries < MAX_TRIES) { const mid = Math.floor((low + high) / 2); const candidate = [...head, ...lines.slice(headerIdx + 1, mid)].join("\n"); tries++; try { yield canParse(candidate, theme); lastGood = mid; low = mid + 1; } catch (e) { high = mid - 1; } } return [...head, ...lines.slice(headerIdx + 1, lastGood)].join("\n"); }); } self.onmessage = (ev) => __async(null, null, function* () { var _a; const msg = ev.data; const send = (res) => self.postMessage(res); const id = msg.id; try { if (msg.action === "canParse") { const ok = yield canParse(msg.payload.code, msg.payload.theme); send({ id, ok: true, result: ok }); return; } if (msg.action === "findPrefix") { const res = yield findLastRenderablePrefix( msg.payload.code, msg.payload.theme ); send({ id, ok: true, result: res }); return; } send({ id, ok: false, error: "Unknown action" }); } catch (e) { send({ id, ok: false, error: (_a = e == null ? void 0 : e.message) != null ? _a : String(e) }); } }); } }); export default require_mermaidParser_worker();