mind-elixir
Version:
Mind elixir is a free open source mind map core.
235 lines (233 loc) • 7.28 kB
JavaScript
class S {
node;
children = [];
// slots[i] represents elements that appear *before* children[i]
// slots[children.length] represents elements that appear *after* all children
slots;
constructor(o) {
this.node = o;
const d = o.children?.length ?? 0;
this.slots = Array.from({ length: d + 1 }, () => []);
}
}
function j(c) {
const { nodeData: o, arrows: d = [], summaries: e = [] } = c, r = /* @__PURE__ */ new Map();
function n(t) {
const s = new S(t);
return r.set(t.id, s), t.children && (s.children = t.children.map(n)), s;
}
const i = n(o), l = [], m = /* @__PURE__ */ new Set();
for (const t of d) {
m.add(t.from), m.add(t.to);
const s = t.metadata, p = s?.parentId, y = s?.index ?? 1 / 0;
if (!p) {
l.push(t);
continue;
}
const g = r.get(p);
if (g) {
const w = Math.min(y, g.children.length);
g.slots[w].push({ type: "arrow", arrow: t });
} else
l.push(t);
}
for (const t of e) {
const s = r.get(t.parent);
if (s) {
const p = Math.min(t.end + 1, s.children.length);
s.slots[p].push({ type: "summary", summary: t });
}
}
function a(t) {
return r.get(t)?.node.metadata?.refId ?? t;
}
const h = [];
function u(t, s) {
const p = " ".repeat(s), y = " ".repeat(s + 1), g = t.node.metadata, w = [t.node.topic], C = g?.refId ?? (m.has(t.node.id) ? t.node.id : void 0);
C && w.push(`[^${C}]`), t.node.style && Object.keys(t.node.style).length > 0 && w.push(JSON.stringify(t.node.style)), h.push(`${p}- ${w.join(" ")}`);
for (let I = 0; I <= t.children.length; I++) {
for (const N of t.slots[I])
if (N.type === "arrow") {
const f = N.arrow, x = f.bidirectional ? `<-${f.label ?? ""}->` : `>-${f.label ?? ""}->`, b = f.delta1 ? ` (${f.delta1.x},${f.delta1.y})` : "", M = f.delta2 ? ` (${f.delta2.x},${f.delta2.y})` : "";
h.push(`${y}- > [^${a(f.from)}]${b} ${x}${M} [^${a(f.to)}]`);
} else if (N.type === "summary") {
const f = N.summary, x = f.end - f.start + 1, b = x === t.children.length ? "" : `:${x} `;
h.push(`${y}- }${b}${f.label}`);
}
I < t.children.length && u(t.children[I], s + 1);
}
}
u(i, 0);
for (const t of l) {
const s = t.bidirectional ? `<-${t.label ?? ""}->` : `>-${t.label ?? ""}->`, p = t.delta1 ? ` (${t.delta1.x},${t.delta1.y})` : "", y = t.delta2 ? ` (${t.delta2.x},${t.delta2.y})` : "";
h.push(`- > [^${a(t.from)}]${p} ${s}${y} [^${a(t.to)}]`);
}
return h.join(`
`) + `
`;
}
function $() {
return ((/* @__PURE__ */ new Date()).getTime().toString(16) + Math.random().toString(16).substring(2)).substring(2, 18);
}
const k = `- Root Node
- Child Node 1
- Child Node 1-1 {"color": "#e87a90", "fontSize": "18px"}
- Child Node 1-2
- Child Node 1-3
- }:2 Summary of first two nodes
- Child Node 2
- Child Node 2-1 [^node-2-1]
- Child Node 2-2 [^id2]
- Child Node 2-3
- > [^node-2-1] <-Bidirectional Link-> [^id2]
- Child Node 3
- Child Node 3-1 [^id3]
- Child Node 3-2 [^id4]
- Child Node 3-3 [^id5] {"fontFamily": "Arial", "fontWeight": "bold"}
- > [^id3] >-Unidirectional Link-> [^id4]
- Child Node 4
- Child Node 4-1 [^id6]
- Child Node 4-2 [^id7]
- Child Node 4-3 [^id8]
- } Summary of all previous nodes
- Child Node 4-4
- > [^node-2-1] <-Link position is not restricted, as long as the id can be found during rendering-> [^id8]
`;
function A(c, o = "Root") {
const d = c.split(`
`).filter((a) => a.trim());
if (d.length === 0)
throw new Error("Failed to parse plaintext: no root node found");
const e = {
arrowLines: [],
nodeIdMap: /* @__PURE__ */ new Map()
}, r = [], n = [], i = [];
for (const a of d) {
const h = v(a), u = T(a);
for (; n.length > 0 && n[n.length - 1].indent >= h; )
n.pop();
const t = n.length > 0 ? n[n.length - 1].node : null, s = t ? t.children ??= [] : i;
if (u.type === "arrow") {
e.arrowLines.push({
content: u.content,
parentId: t?.id ?? null,
index: s.length
});
continue;
}
if (u.type === "summary") {
const g = O(u.content, s, t?.id ?? "");
g && r.push(g);
continue;
}
const p = $(), y = {
topic: u.topic,
id: p
};
u.style && (y.style = u.style), u.refId && (e.nodeIdMap.set(u.refId, p), y.metadata = { refId: u.refId }), s.push(y), n.push({ indent: h, node: y });
}
if (i.length === 0)
throw new Error("Failed to parse plaintext: no root node found");
let l;
i.length === 1 ? l = i[0] : l = {
topic: o,
id: $(),
children: i
};
const m = e.arrowLines.map(({ content: a, parentId: h, index: u }) => {
const t = R(a, e);
return t && (t.metadata = { parentId: h, index: u }), t;
}).filter((a) => a !== null);
return {
nodeData: l,
arrows: m.length > 0 ? m : void 0,
summaries: r.length > 0 ? r : void 0
};
}
function v(c) {
const o = c.match(/^(\s*)/);
return o ? o[1].length : 0;
}
function L(c) {
try {
const o = c.trim().startsWith("{") ? c : `{${c}}`;
return JSON.parse(o);
} catch {
return {};
}
}
function T(c) {
const d = c.trim().replace(/^-\s*/, "");
if (d.startsWith(">"))
return {
type: "arrow",
topic: "",
content: d.substring(1).trim()
};
if (d.startsWith("}"))
return {
type: "summary",
topic: "",
content: d.substring(1).trim()
};
let e = d, r, n;
const i = e.match(/\[\^([\w-]+)\]/);
i && (r = i[1], e = e.replace(i[0], "").trim());
const l = e.match(/\{([^}]+)\}/);
return l && (n = L(l[1]), e = e.replace(l[0], "").trim()), {
type: "node",
topic: e,
content: e,
refId: r,
style: n
};
}
function R(c, o) {
const d = c.match(
/\[\^([\w-]+)\](?:\s*\(([\d.-]+),([\d.-]+)\))?\s*<-([^-]*)->(?:\s*\(([\d.-]+),([\d.-]+)\))?\s*\[\^([\w-]+)\]/
);
if (d) {
const r = d[1], n = d[2], i = d[3], l = d[4].trim(), m = d[5], a = d[6], h = d[7];
return {
id: $(),
label: l,
from: o.nodeIdMap.get(r) || r,
to: o.nodeIdMap.get(h) || h,
delta1: n && i ? { x: Number(n), y: Number(i) } : void 0,
delta2: m && a ? { x: Number(m), y: Number(a) } : void 0,
bidirectional: !0
};
}
const e = c.match(/\[\^([\w-]+)\](?:\s*\(([\d.-]+),([\d.-]+)\))?\s*>-([^-]*)->(?:\s*\(([\d.-]+),([\d.-]+)\))?\s*\[\^([\w-]+)\]/);
if (e) {
const r = e[1], n = e[2], i = e[3], l = e[4].trim(), m = e[5], a = e[6], h = e[7];
return {
id: $(),
label: l,
from: o.nodeIdMap.get(r) || r,
to: o.nodeIdMap.get(h) || h,
delta1: n && i ? { x: Number(n), y: Number(i) } : void 0,
delta2: m && a ? { x: Number(m), y: Number(a) } : void 0
};
}
return null;
}
function O(c, o, d) {
const e = c.match(/^:(\d+)\s+(.*)/);
let r, n;
if (e ? (r = parseInt(e[1], 10), n = e[2]) : (r = o.length, n = c.trim()), o.length === 0 || r === 0)
return null;
const i = o.slice(-r), l = o.indexOf(i[0]), m = o.indexOf(i[i.length - 1]);
return {
id: $(),
label: n,
parent: d,
start: l,
end: m
};
}
export {
j as mindElixirToPlaintext,
k as plaintextExample,
A as plaintextToMindElixir
};