UNPKG

mind-elixir

Version:

Mind elixir is a free open source mind map core.

219 lines (218 loc) • 7.11 kB
//#region src/utils/mindElixirToPlaintext.ts var e = class { node; children = []; slots; constructor(e) { this.node = e; let t = e.children?.length ?? 0; this.slots = Array.from({ length: t + 1 }, () => []); } }, t = (e) => e.replace(/\\/g, "\\\\").replace(/\r?\n/g, "\\n").replace(/^(\s*)([>}])/, "$1\\$2"); function n(n) { let { nodeData: r, arrows: i = [], summaries: a = [] } = n, o = /* @__PURE__ */ new Map(); function s(t) { let n = new e(t); return o.set(t.id, n), t.children && (n.children = t.children.map(s)), n; } let c = s(r), l = [], u = /* @__PURE__ */ new Set(); for (let e of i) { u.add(e.from), u.add(e.to); let t = e.metadata, n = t?.parentId, r = t?.index ?? Infinity; if (!n) { l.push(e); continue; } let i = o.get(n); if (i) { let t = Math.min(r, i.children.length); i.slots[t].push({ type: "arrow", arrow: e }); } else l.push(e); } for (let e of a) { let t = o.get(e.parent); if (t) { let n = Math.min(e.end + 1, t.children.length); t.slots[n].push({ type: "summary", summary: e }); } } function d(e) { return o.get(e)?.node.metadata?.refId ?? e; } let f = []; function p(e, n) { let r = " ".repeat(n), i = " ".repeat(n + 1), a = e.node.metadata, o = [t(e.node.topic)], s = a?.refId ?? (u.has(e.node.id) ? e.node.id : void 0); s && o.push(`[^${s}]`), e.node.style && Object.keys(e.node.style).length > 0 && o.push(JSON.stringify(e.node.style)), f.push(`${r}- ${o.join(" ")}`); for (let t = 0; t <= e.children.length; t++) { for (let n of e.slots[t]) if (n.type === "arrow") { let e = n.arrow, t = e.bidirectional ? `<-${e.label ?? ""}->` : `>-${e.label ?? ""}->`, r = e.delta1 ? ` (${e.delta1.x},${e.delta1.y})` : "", a = e.delta2 ? ` (${e.delta2.x},${e.delta2.y})` : ""; f.push(`${i}- > [^${d(e.from)}]${r} ${t}${a} [^${d(e.to)}]`); } else if (n.type === "summary") { let t = n.summary, r = t.end - t.start + 1, a = r === e.children.length ? "" : `:${r} `; f.push(`${i}- }${a}${t.label}`); } t < e.children.length && p(e.children[t], n + 1); } } p(c, 0); for (let e of l) { let t = e.bidirectional ? `<-${e.label ?? ""}->` : `>-${e.label ?? ""}->`, n = e.delta1 ? ` (${e.delta1.x},${e.delta1.y})` : "", r = e.delta2 ? ` (${e.delta2.x},${e.delta2.y})` : ""; f.push(`- > [^${d(e.from)}]${n} ${t}${r} [^${d(e.to)}]`); } return f.join("\n") + "\n"; } //#endregion //#region src/utils/index.ts function r() { return Date.now().toString(36) + Math.random().toString(36).substring(2, 8); } //#endregion //#region src/utils/plaintextToMindElixir.ts var i = "- Root Node\n - Child Node 1\n - Child Node 1-1 {\"color\": \"#e87a90\", \"fontSize\": \"18px\"}\n - Child Node 1-2\n - Child Node 1-3\n - }:2 Summary of first two nodes\n - Child Node 2\n - Child Node 2-1 [^node-2-1]\n - Child Node 2-2 [^id2]\n - Child Node 2-3\n - > [^node-2-1] <-Bidirectional Link-> [^id2]\n - Child Node 3\n - Child Node 3-1 [^id3]\n - Child Node 3-2 [^id4]\n - Child Node 3-3 [^id5] {\"fontFamily\": \"Arial\", \"fontWeight\": \"bold\"}\n - > [^id3] >-Unidirectional Link-> [^id4]\n - Child Node 4\n - Child Node 4-1 [^id6]\n - Child Node 4-2 [^id7]\n - Child Node 4-3 [^id8]\n - } Summary of all previous nodes\n - Child Node 4-4\n- > [^node-2-1] <-Link position is not restricted, as long as the id can be found during rendering-> [^id8]\n\n"; function a(e, t = "Root") { let n = e.split("\n").filter((e) => e.trim()); if (n.length === 0) throw Error("Failed to parse plaintext: no root node found"); let i = { arrowLines: [], nodeIdMap: /* @__PURE__ */ new Map() }, a = [], s = [], c = []; for (let e of n) { let t = o(e), n = l(e); for (; s.length > 0 && s[s.length - 1].indent >= t;) s.pop(); let u = s.length > 0 ? s[s.length - 1].node : null, f = u ? u.children ??= [] : c; if (n.type === "arrow") { i.arrowLines.push({ arrow: n.arrow, parentId: u?.id ?? null, index: f.length }); continue; } if (n.type === "summary") { let e = d(n.content, f, u?.id ?? ""); e && a.push(e); continue; } let p = r(), m = { topic: n.topic, id: p }; n.style && (m.style = n.style), n.refId && (i.nodeIdMap.set(n.refId, p), m.metadata = { refId: n.refId }), f.push(m), s.push({ indent: t, node: m }); } if (c.length === 0) throw Error("Failed to parse plaintext: no root node found"); let u; u = c.length === 1 ? c[0] : { topic: t, id: r(), children: c }; let f = i.arrowLines.map(({ arrow: e, parentId: t, index: n }) => (e.from = i.nodeIdMap.get(e.from) ?? e.from, e.to = i.nodeIdMap.get(e.to) ?? e.to, e.metadata = { parentId: t, index: n }, e)); return { nodeData: u, arrows: f.length > 0 ? f : void 0, summaries: a.length > 0 ? a : void 0 }; } function o(e) { let t = e.match(/^(\s*)/); return t ? t[1].length : 0; } function s(e) { try { return JSON.parse(e); } catch { return console.warn(`[plaintext] Invalid node style (must be valid JSON), ignored: ${e}`), null; } } var c = (e) => e.replace(/\\(n|\\|>|\})/g, (e, t) => t === "n" ? "\n" : t); function l(e) { let t = e.trim().replace(/^-\s*/, ""); if (t.startsWith(">")) { let e = u(t.substring(1).trim()); if (e) return { type: "arrow", arrow: e }; } else if (t.startsWith("}")) return { type: "summary", content: t.substring(1).trim() }; let n = t, r, i, a = n.match(/\s*(\{[^}]+\})\s*$/); if (a) { let e = s(a[1]); e && (i = e, n = n.substring(0, n.length - a[0].length).trim()); } let o = n.match(/\s*\[\^([\w-]+)\]\s*$/); return o && (r = o[1], n = n.substring(0, n.length - o[0].length).trim()), { type: "node", topic: c(n), refId: r, style: i }; } function u(e) { let t = e.match(/^\[\^([\w-]+)\](?:\s*\(([\d.-]+),([\d.-]+)\))?\s*<-([\s\S]*)->(?:\s*\(([\d.-]+),([\d.-]+)\))?\s*\[\^([\w-]+)\]$/); if (t) { let e = t[1], n = t[2], i = t[3], a = t[4].trim(), o = t[5], s = t[6], c = t[7]; return { id: r(), label: a, from: e, to: c, delta1: n && i ? { x: Number(n), y: Number(i) } : void 0, delta2: o && s ? { x: Number(o), y: Number(s) } : void 0, bidirectional: !0 }; } let n = e.match(/^\[\^([\w-]+)\](?:\s*\(([\d.-]+),([\d.-]+)\))?\s*>-([\s\S]*)->(?:\s*\(([\d.-]+),([\d.-]+)\))?\s*\[\^([\w-]+)\]$/); if (n) { let e = n[1], t = n[2], i = n[3], a = n[4].trim(), o = n[5], s = n[6], c = n[7]; return { id: r(), label: a, from: e, to: c, delta1: t && i ? { x: Number(t), y: Number(i) } : void 0, delta2: o && s ? { x: Number(o), y: Number(s) } : void 0 }; } return null; } function d(e, t, n) { let i = e.match(/^:(\d+)\s+(.*)/), a, o; if (i ? (a = parseInt(i[1], 10), o = i[2]) : (a = t.length, o = e.trim()), t.length === 0 || a === 0) return null; let s = t.slice(-a), c = t.indexOf(s[0]), l = t.indexOf(s[s.length - 1]); return { id: r(), label: o, parent: n, start: c, end: l }; } //#endregion export { t as escapeTopic, n as mindElixirToPlaintext, i as plaintextExample, a as plaintextToMindElixir };