SPB Git

Repository Public

next-nodes.ts 13.4 KB 173 lines typescript

1/**2 * File: src/lib/editor/next-nodes.ts3 * Description: text / Markdown rendering, attachment references and attr limits of the 3.0 document nodes (SPEC §2): mermaid, drawing,4 *   chart, queryView, citation (inline), bibliography, pdf, blockRef, runCode, audio, canvasLink. Pure functions over ProseMirror JSON5 *   (no DOM, no server import): shared by the server, the web client, and mirrored byte-for-byte by SPNotesCore `MarkdownExport`6 *   (fixtures: tests/fixtures/next-nodes/). The exact output rules are specified in docs/next/API-NEXT.md §2.7 * Author: Simon-Pierre Boucher · contact@spboucher.ai8 */9type N = { type: string; attrs?: Record<string, any>; content?: N[]; text?: string };1011/** Block-level leaf atoms added in 3.0. Each carries a block `id`, is a chunk of its own for search, and joins its siblings with a newline. */12export const NEXT_BLOCK_TYPES = ["mermaid", "drawing", "chart", "queryView", "bibliography", "pdf", "blockRef", "runCode", "audio", "canvasLink"] as const;13export const NEXT_INLINE_TYPES = ["citation"] as const;14const BLOCK = new Set<string>(NEXT_BLOCK_TYPES);15export const isNextBlock = (type: string) => BLOCK.has(type);16export const isNextNode = (type: string) => BLOCK.has(type) || type === "citation";1718const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;19const str = (v: unknown) => (typeof v === "string" ? v : "");20const id = (v: unknown) => (typeof v === "string" && UUID.test(v) ? v.toLowerCase() : null);21const oneLine = (v: unknown) => str(v).replace(/\s*\n\s*/g, " ").trim();22/** `/api/files/<id>`, or the link an export resolver substituted for it (rewriteDoc stores them in attrs.__href). */23const fileUrl = (n: N, attachmentId: string) => str(n.attrs?.__href?.[attachmentId]) || `/api/files/${attachmentId}`;2425/**26 * A chart / table cell as text. null and undefined → "". Booleans → "true" / "false". Numbers: integer-valued finite numbers print27 * without a decimal point ("3", never "3.0"); other finite numbers print the shortest decimal that round-trips (ECMAScript28 * Number::toString, e.g. "0.1", "1e+21"); NaN and ±Infinity → "". Strings are kept, newlines collapsed to one space.29 */30export function cellText(v: unknown): string {31  if (v === null || v === undefined) return "";32  if (typeof v === "number") return Number.isFinite(v) ? String(v) : "";33  if (typeof v === "boolean") return v ? "true" : "false";34  return oneLine(typeof v === "string" ? v : JSON.stringify(v));35}36/** [mm:ss] — minutes are not capped at 59 (a 2-hour recording reads [125:03]); negative or invalid times read [00:00]. */37export function timecode(t: unknown): string {38  const s = typeof t === "number" && Number.isFinite(t) && t > 0 ? Math.floor(t) : 0;39  return `${String(Math.floor(s / 60)).padStart(2, "0")}:${String(s % 60).padStart(2, "0")}`;40}41const rowsOf = (n: N): unknown[][] => (Array.isArray(n.attrs?.rows) ? n.attrs!.rows.filter((r: unknown) => Array.isArray(r)) : []);42const colsOf = (n: N): string[] => (Array.isArray(n.attrs?.columns) ? n.attrs!.columns.map((c: unknown) => cellText(c)) : []);43const annotationsOf = (n: N): Record<string, any>[] => (Array.isArray(n.attrs?.annotations) ? n.attrs!.annotations.filter((a: unknown) => a && typeof a === "object") : []);44const transcriptOf = (n: N): { t: unknown; text: string }[] => (Array.isArray(n.attrs?.transcript) ? n.attrs!.transcript.filter((s: any) => s && typeof s === "object" && oneLine(s.text)).map((s: any) => ({ t: s.t, text: oneLine(s.text) })) : []);45const annotationLine = (a: Record<string, any>) => [`p.${Number.isFinite(Number(a.page)) ? Math.max(1, Math.floor(Number(a.page))) : 1}`, oneLine(a.text), oneLine(a.note)].filter(Boolean).join(" — ");46const outputText = (n: N): string | null => (n.attrs?.output && typeof n.attrs.output === "object" && typeof n.attrs.output.text === "string" ? n.attrs.output.text : null);47const label = (prefix: string, title: unknown) => (oneLine(title) ? `${prefix}: ${oneLine(title)}` : prefix);4849/**50 * Plain text of a 3.0 node (search index, word count, excerpts): the Markdown form without its syntax. Transcript and annotation text51 * are included; identifiers (attachment, page, block ids) and time codes are not. Returns null for any other node type.52 */53export function nextNodeText(n: N): string | null {54  switch (n.type) {55    case "mermaid": return str(n.attrs?.code);56    case "runCode": { const out = outputText(n); return [str(n.attrs?.code), out ?? ""].filter(Boolean).join("\n"); }57    case "chart": return [oneLine(n.attrs?.title), colsOf(n).join(" "), ...rowsOf(n).map((r) => r.map(cellText).join(" "))].map((l) => l.trim()).filter(Boolean).join("\n");58    case "drawing": return "Dessin";59    case "pdf": return [label("PDF", n.attrs?.name), ...annotationsOf(n).map(annotationLine)].join("\n");60    case "audio": return [label("Audio", n.attrs?.title), ...transcriptOf(n).map((s) => s.text)].join("\n");61    case "queryView": return label("Vue", n.attrs?.title);62    case "blockRef": return "";63    case "citation": return oneLine(n.attrs?.label);64    case "bibliography": return oneLine(n.attrs?.title) || "Références";65    case "canvasLink": return "Canvas";66    default: return null;67  }68}6970const mdCell = (v: unknown) => cellText(v).replace(/\|/g, "\\|");71/**72 * Markdown of a 3.0 node. Block nodes end with exactly one "\n" (like every block of `toMarkdown`); `citation` is inline and has none.73 * Returns null for any other node type.74 */75export function nextNodeMarkdown(n: N): string | null {76  switch (n.type) {77    case "mermaid": return `\`\`\`mermaid\n${str(n.attrs?.code)}\n\`\`\`\n`;78    case "runCode": { const out = outputText(n); return `\`\`\`${str(n.attrs?.language)}\n${str(n.attrs?.code)}\n\`\`\`\n${out === null ? "" : `\n\`\`\`output\n${out}\n\`\`\`\n`}`; }79    case "chart": {80      const title = oneLine(n.attrs?.title); const cols = colsOf(n); const rows = rowsOf(n);81      if (!cols.length) return `${title || "[Graphique]"}\n`;82      const table = `| ${cols.map(mdCell).join(" | ")} |\n| ${cols.map(() => "---").join(" | ")} |\n${rows.map((r) => `| ${cols.map((_c, i) => mdCell(r[i])).join(" | ")} |\n`).join("")}`;83      return title ? `${title}\n\n${table}` : table;84    }85    case "drawing": { const p = id(n.attrs?.preview); return p ? `![Dessin](${fileUrl(n, p)})\n` : "[Dessin]\n"; }86    case "pdf": { const a = id(n.attrs?.attachmentId); const head = a ? `[${label("PDF", n.attrs?.name)}](${fileUrl(n, a)})` : `[${label("PDF", n.attrs?.name)}]`; const notes = annotationsOf(n).map((x) => `> ${annotationLine(x)}\n`).join(""); return `${head}\n${notes ? `\n${notes}` : ""}`; }87    case "audio": { const a = id(n.attrs?.attachmentId); const head = a ? `[${label("Audio", n.attrs?.title)}](${fileUrl(n, a)})` : `[${label("Audio", n.attrs?.title)}]`; const lines = transcriptOf(n).map((s) => `[${timecode(s.t)}] ${s.text}\n`).join(""); return `${head}\n${lines ? `\n${lines}` : ""}`; }88    case "queryView": return `[${label("Vue", n.attrs?.title)}]\n`;89    case "blockRef": return `[[${id(n.attrs?.pageId) ?? ""}${str(n.attrs?.blockId) ? `#${str(n.attrs?.blockId)}` : ""}]]\n`;90    case "citation": return `[@${oneLine(n.attrs?.label) || id(n.attrs?.refId) || "?"}]`;91    case "bibliography": return `## ${oneLine(n.attrs?.title) || "Références"}\n`;92    case "canvasLink": { const p = id(n.attrs?.pageId); return p ? `[Canvas](/p/${p})\n` : "[Canvas]\n"; }93    default: return null;94  }95}9697/** Attachment ids a 3.0 node points to (attrs only — binary data is never inline). */98export function nodeAttachmentIds(n: N): string[] {99  const a = n.attrs ?? {}; const out: (string | null)[] = [];100  if (n.type === "mermaid") out.push(id(a.preview));101  else if (n.type === "drawing") out.push(id(a.ink), id(a.preview), id(a.pk));102  else if (n.type === "pdf" || n.type === "audio") out.push(id(a.attachmentId));103  return out.filter((x): x is string => Boolean(x));104}105/** Pages a 3.0 node links to (blockRef, canvasLink) with the anchor block when there is one. */106export function nodePageRef(n: N): { pageId: string; blockId: string | null } | null {107  if (n.type !== "blockRef" && n.type !== "canvasLink") return null;108  const p = id(n.attrs?.pageId); return p ? { pageId: p, blockId: n.type === "blockRef" && str(n.attrs?.blockId) ? str(n.attrs?.blockId) : null } : null;109}110/** Reference cited by a `citation` node. */111export const nodeRefId = (n: N): string | null => (n.type === "citation" ? id(n.attrs?.refId) : null);112113/** Size limits of the new attrs, enforced by `assertDoc` (HTTP 400 / 413 `bad_document`). Kept here so clients can read the same numbers. */114export const NEXT_LIMITS = {115  codeChars: 200_000, // mermaid.code, runCode.code116  outputChars: 200_000, // runCode.output.text117  chartColumns: 100, chartRows: 5_000, chartCellChars: 2_000,118  annotations: 2_000, annotationTextChars: 10_000, annotationRects: 500, annotationInkPoints: 20_000,119  transcriptSegments: 20_000, transcriptTextChars: 5_000, summaryChars: 20_000,120  titleChars: 500, queryLimit: 500,121} as const;122123/**124 * Checks the attrs of a 3.0 node. Returns a human message when something is out of bounds, else null. Unknown attrs and unknown node125 * types are NOT rejected (forward compatibility: a newer client may add attrs; an unknown node must round-trip untouched).126 */127export function nextNodeProblem(n: N): string | null {128  if (!isNextNode(n.type)) return null;129  const a = n.attrs ?? {}; const L = NEXT_LIMITS;130  const optId = (k: string) => (a[k] === null || a[k] === undefined || id(a[k]) ? null : `${n.type}.${k} must be an attachment/page/reference uuid or null`);131  const maxStr = (k: string, max: number) => (a[k] === null || a[k] === undefined || (typeof a[k] === "string" && a[k].length <= max) ? null : `${n.type}.${k} must be a string of at most ${max} characters`);132  const first = (...p: (string | null)[]) => p.find(Boolean) ?? null;133  switch (n.type) {134    case "mermaid": return first(maxStr("code", L.codeChars), optId("preview"), a.previewHash == null || /^[0-9a-f]{64}$/i.test(str(a.previewHash)) ? null : "mermaid.previewHash must be a sha256 hex digest or null");135    case "drawing": return first(optId("ink"), optId("preview"), optId("pk"));136    case "runCode": return first(maxStr("code", L.codeChars), maxStr("language", 64), a.output == null || (typeof a.output === "object" && (a.output.text == null || (typeof a.output.text === "string" && a.output.text.length <= L.outputChars))) ? null : `runCode.output.text must be a string of at most ${L.outputChars} characters`);137    case "chart": {138      if (a.columns != null && (!Array.isArray(a.columns) || a.columns.length > L.chartColumns || a.columns.some((c: unknown) => typeof c !== "string" || c.length > L.chartCellChars))) return `chart.columns must be at most ${L.chartColumns} strings`;139      if (a.rows != null) {140        if (!Array.isArray(a.rows) || a.rows.length > L.chartRows) return `chart.rows must be at most ${L.chartRows} rows`;141        for (const r of a.rows) { if (!Array.isArray(r) || r.length > L.chartColumns) return `chart.rows: each row is an array of at most ${L.chartColumns} cells`; for (const c of r) if (!(c === null || typeof c === "number" || (typeof c === "string" && c.length <= L.chartCellChars))) return "chart.rows: a cell is a string, a number or null"; }142      }143      return maxStr("title", L.titleChars);144    }145    case "pdf": {146      if (a.annotations != null) {147        if (!Array.isArray(a.annotations) || a.annotations.length > L.annotations) return `pdf.annotations must be at most ${L.annotations} items`;148        for (const x of a.annotations) {149          if (!x || typeof x !== "object") return "pdf.annotations: each item is an object";150          if (str(x.text).length > L.annotationTextChars || str(x.note).length > L.annotationTextChars) return `pdf.annotations: text and note are at most ${L.annotationTextChars} characters`;151          if (x.rects != null && (!Array.isArray(x.rects) || x.rects.length > L.annotationRects)) return `pdf.annotations: at most ${L.annotationRects} rects`;152          if (x.ink != null && (!Array.isArray(x.ink) || x.ink.length > L.annotationInkPoints)) return `pdf.annotations: at most ${L.annotationInkPoints} ink points`;153        }154      }155      return first(optId("attachmentId"), maxStr("name", L.titleChars));156    }157    case "audio": {158      if (a.transcript != null) {159        if (!Array.isArray(a.transcript) || a.transcript.length > L.transcriptSegments) return `audio.transcript must be at most ${L.transcriptSegments} segments`;160        for (const s of a.transcript) if (!s || typeof s !== "object" || typeof s.text !== "string" || s.text.length > L.transcriptTextChars) return `audio.transcript: each segment is {t, text} with text of at most ${L.transcriptTextChars} characters`;161      }162      if (a.summary != null && (typeof a.summary !== "object" || str(a.summary.text).length > L.summaryChars)) return `audio.summary.text must be at most ${L.summaryChars} characters`;163      return first(optId("attachmentId"), maxStr("title", L.titleChars));164    }165    case "queryView": return first(optId("databaseId"), optId("viewId"), maxStr("title", L.titleChars), a.limit == null || (Number.isInteger(a.limit) && a.limit >= 1 && a.limit <= L.queryLimit) ? null : `queryView.limit must be an integer between 1 and ${L.queryLimit}`);166    case "blockRef": return first(optId("pageId"), maxStr("blockId", 64));167    case "canvasLink": return optId("pageId");168    case "citation": return first(optId("refId"), maxStr("locator", 200), maxStr("label", L.titleChars));169    case "bibliography": return first(maxStr("style", 32), maxStr("title", L.titleChars));170    default: return null;171  }172}173
Files main
Menu

Theme