Skip to content

Build your personal knowledge base with Trilium Notes

feat(script_deployer): backend-script to import to XOPP

Elian Dorana0449ff7bf90
diff --git a/apps/script-deployer/scripts/auto-import-xopp.ts b/apps/script-deployer/scripts/auto-import-xopp.ts
new file mode 100644
index 0000000000..5a15d7ad28
--- /dev/null
+++ b/apps/script-deployer/scripts/auto-import-xopp.ts
@@ -0,0 +1,324 @@
+/**
+ * @trilium-script
+ *
+ * id: auto-import-xopp
+ * type: backend
+ * title: Auto-import XOPP to Canvas
+ * executeButton: true
+ * executeDescription: Converts all .xopp children of this note into Excalidraw canvas notes.
+ */
+
+const zlib = require("zlib");
+
+// ── Color mapping ────────────────────────────────────────────────────────────
+
+const XOPP_COLORS: Record<string, string> = {
+ black: "#000000",
+ blue: "#3333cc",
+ red: "#ff0000",
+ green: "#008000",
+ gray: "#808080",
+ lightblue: "#00c0ff",
+ lightgreen: "#00ff00",
+ magenta: "#ff00ff",
+ orange: "#ff8000",
+ yellow: "#ffff00",
+ white: "#ffffff",
+};
+
+function convertColor(color: string | null): string {
+ if (!color) return "#000000";
+ const lower = color.toLowerCase();
+ if (XOPP_COLORS[lower]) return XOPP_COLORS[lower];
+ if (lower.startsWith("#")) {
+ return lower.length > 7 ? lower.slice(0, 7) : lower;
+ }
+ return "#000000";
+}
+
+function extractOpacity(color: string | null): number {
+ if (!color) return 100;
+ if (color.startsWith("#") && color.length === 9) {
+ const alpha = parseInt(color.slice(7, 9), 16);
+ return Math.round((alpha / 255) * 100);
+ }
+ return 100;
+}
+
+// ── Excalidraw element helpers ───────────────────────────────────────────────
+
+function generateId(): string {
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+ let id = "";
+ for (let i = 0; i < 20; i++) {
+ id += chars[Math.floor(Math.random() * chars.length)];
+ }
+ return id;
+}
+
+function makeBaseElement(type: string, x: number, y: number, width: number, height: number) {
+ return {
+ id: generateId(),
+ type,
+ x,
+ y,
+ width,
+ height,
+ angle: 0,
+ strokeColor: "#000000",
+ backgroundColor: "transparent",
+ fillStyle: "solid",
+ strokeWidth: 2,
+ strokeStyle: "solid",
+ roughness: 0,
+ opacity: 100,
+ groupIds: [] as string[],
+ frameId: null,
+ index: null,
+ roundness: null,
+ seed: Math.floor(Math.random() * 2_000_000_000),
+ version: 1,
+ versionNonce: Math.floor(Math.random() * 2_000_000_000),
+ isDeleted: false,
+ boundElements: null,
+ updated: Date.now(),
+ link: null,
+ locked: false,
+ } as Record<string, unknown>;
+}
+
+// ── xml2js element accessors ─────────────────────────────────────────────────
+
+/** Get attribute from an xml2js node. */
+function attr(node: Record<string, unknown>, name: string): string | null {
+ const attrs = node.$ as Record<string, string> | undefined;
+ return attrs?.[name] ?? null;
+}
+
+/** Get text content from an xml2js node. */
+function text(node: Record<string, unknown>): string {
+ return (node._ as string | undefined)?.trim() ?? "";
+}
+
+/** Get child elements by tag name, always as array. */
+function children(node: Record<string, unknown>, tag: string): Record<string, unknown>[] {
+ const val = node[tag];
+ if (!val) return [];
+ return Array.isArray(val) ? val : [val];
+}
+
+// ── Converters ───────────────────────────────────────────────────────────────
+
+interface ExcalidrawFile {
+ mimeType: string;
+ id: string;
+ dataURL: string;
+ created: number;
+ lastRetrieved: number;
+}
+
+function convertStroke(strokeNode: Record<string, unknown>, yOffset: number) {
+ const coordText = text(strokeNode);
+ const coords = coordText.split(/\s+/).map(Number);
+ if (coords.length < 4) return null;
+
+ const originX = coords[0];
+ const originY = coords[1] + yOffset;
+ const points: number[][] = [];
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
+
+ for (let i = 0; i < coords.length; i += 2) {
+ const px = coords[i] - originX;
+ const py = (coords[i + 1] + yOffset) - originY;
+ points.push([px, py]);
+ minX = Math.min(minX, px);
+ minY = Math.min(minY, py);
+ maxX = Math.max(maxX, px);
+ maxY = Math.max(maxY, py);
+ }
+
+ const widthAttr = attr(strokeNode, "width") || "2";
+ const widths = widthAttr.trim().split(/\s+/).map(Number);
+ const hasPressure = widths.length > 1;
+
+ let pressures: number[] = [];
+ if (hasPressure) {
+ const maxW = Math.max(...widths);
+ pressures = widths.map((w) => maxW > 0 ? w / maxW : 0.5);
+ while (pressures.length < points.length) {
+ pressures.push(pressures[pressures.length - 1] || 0.5);
+ }
+ pressures = pressures.slice(0, points.length);
+ }
+
+ const color = attr(strokeNode, "color") || "black";
+ const tool = attr(strokeNode, "tool") || "pen";
+ const isHighlighter = tool === "highlighter";
+
+ const el = makeBaseElement("freedraw", originX, originY, maxX - minX, maxY - minY);
+ el.points = points;
+ el.pressures = hasPressure ? pressures : [];
+ el.simulatePressure = !hasPressure;
+ el.strokeColor = convertColor(color);
+ el.strokeWidth = widths[0] || 2;
+ el.opacity = isHighlighter ? 40 : extractOpacity(color);
+ el.lastCommittedPoint = points[points.length - 1] || null;
+
+ return el;
+}
+
+function convertText(textNode: Record<string, unknown>, yOffset: number) {
+ const x = parseFloat(attr(textNode, "x") ?? "0");
+ const y = parseFloat(attr(textNode, "y") ?? "0") + yOffset;
+ const size = parseFloat(attr(textNode, "size") ?? "12");
+ const color = attr(textNode, "color") || "black";
+ const content = text(textNode);
+
+ if (!content) return null;
+
+ const lines = content.split("\n");
+ const estWidth = Math.max(...lines.map((l) => l.length)) * size * 0.6;
+ const estHeight = lines.length * size * 1.3;
+
+ const el = makeBaseElement("text", x, y, estWidth, estHeight);
+ el.text = content;
+ el.fontSize = size;
+ el.fontFamily = 5;
+ el.textAlign = "left";
+ el.verticalAlign = "top";
+ el.strokeColor = convertColor(color);
+ el.opacity = extractOpacity(color);
+ el.containerId = null;
+ el.originalText = content;
+ el.autoResize = true;
+ el.lineHeight = 1.25;
+
+ return el;
+}
+
+function convertImage(
+ imageNode: Record<string, unknown>,
+ yOffset: number,
+ files: Record<string, ExcalidrawFile>,
+) {
+ const left = parseFloat(attr(imageNode, "left") ?? "");
+ const top = parseFloat(attr(imageNode, "top") ?? "") + yOffset;
+ const right = parseFloat(attr(imageNode, "right") ?? "");
+ const bottom = parseFloat(attr(imageNode, "bottom") ?? "") + yOffset;
+
+ if (isNaN(left) || isNaN(top) || isNaN(right) || isNaN(bottom)) return null;
+
+ const imgData = text(imageNode);
+ if (!imgData) return null;
+
+ const fileId = generateId();
+ files[fileId] = {
+ mimeType: "image/png",
+ id: fileId,
+ dataURL: `data:image/png;base64,${imgData}`,
+ created: Date.now(),
+ lastRetrieved: Date.now(),
+ };
+
+ const el = makeBaseElement("image", left, top, right - left, bottom - top);
+ el.fileId = fileId;
+ el.status = "saved";
+ el.scale = [1, 1];
+
+ return el;
+}
+
+// ── Main conversion ──────────────────────────────────────────────────────────
+
+function convertXoppXml(doc: Record<string, unknown>) {
+ const xournal = doc.xournal as Record<string, unknown> | undefined;
+ if (!xournal) throw new Error("Not a valid .xopp file: missing <xournal> root element");
+
+ const pages = children(xournal, "page");
+ const elements: Record<string, unknown>[] = [];
+ const files: Record<string, ExcalidrawFile> = {};
+ let yOffset = 0;
+ const PAGE_GAP = 80;
+
+ for (const page of pages) {
+ const pageHeight = parseFloat(attr(page, "height") ?? "792");
+ const pageWidth = parseFloat(attr(page, "width") ?? "612");
+
+ // Page boundary rectangle
+ const bgRect = makeBaseElement("rectangle", 0, yOffset, pageWidth, pageHeight);
+ bgRect.strokeColor = "#d0d0d0";
+ bgRect.strokeWidth = 1;
+ bgRect.strokeStyle = "dashed";
+ bgRect.backgroundColor = "transparent";
+ bgRect.opacity = 50;
+ elements.push(bgRect);
+
+ const layers = children(page, "layer");
+ for (const layer of layers) {
+ for (const stroke of children(layer, "stroke")) {
+ const el = convertStroke(stroke, yOffset);
+ if (el) elements.push(el);
+ }
+ for (const txt of children(layer, "text")) {
+ const el = convertText(txt, yOffset);
+ if (el) elements.push(el);
+ }
+ for (const img of children(layer, "image")) {
+ const el = convertImage(img, yOffset, files);
+ if (el) elements.push(el);
+ }
+ }
+
+ yOffset += pageHeight + PAGE_GAP;
+ }
+
+ return {
+ type: "excalidraw",
+ version: 2,
+ elements,
+ files,
+ appState: {
+ gridModeEnabled: false,
+ viewBackgroundColor: "#ffffff",
+ },
+ };
+}
+
+// ── Entry point (manually run) ───────────────────────────────────────────────
+
+const scriptNote = api.currentNote;
+const childNotes = scriptNote.getChildNotes();
+
+for (const note of childNotes) {
+ if (note.type !== "file" || !note.title?.endsWith(".xopp")) {
+ continue;
+ }
+
+ const content = note.getContent();
+ const xmlBuffer = zlib.gunzipSync(content);
+ const xmlString = xmlBuffer.toString("utf-8");
+
+ api.xml2js.parseString(xmlString, (err: Error | null, result: Record<string, unknown>) => {
+ if (err) {
+ api.log(`auto-import-xopp: failed to parse XML for "${note.title}": ${err.message}`);
+ return;
+ }
+
+ try {
+ const excalidrawData = convertXoppXml(result);
+ const title = note.title.replace(/\.xopp$/, "");
+
+ const { note: canvasNote } = api.createNewNote({
+ parentNoteId: scriptNote.noteId,
+ title,
+ content: JSON.stringify(excalidrawData),
+ type: "canvas",
+ });
+
+ api.log(`auto-import-xopp: converted "${note.title}" → canvas note ${canvasNote.noteId}`);
+ } catch (convErr) {
+ const msg = convErr instanceof Error ? convErr.message : String(convErr);
+ api.log(`auto-import-xopp: conversion failed for "${note.title}": ${msg}`);
+ }
+ });
+}
diff --git a/apps/script-deployer/scripts/import-xopp-to-canvas.tsx b/apps/script-deployer/scripts/import-xopp-to-canvas.tsx
deleted file mode 100644
index 71e761d164..0000000000
--- a/apps/script-deployer/scripts/import-xopp-to-canvas.tsx
+++ /dev/null
@@ -1,436 +0,0 @@
-/**
- * @trilium-script
- *
- * id: import-xopp-to-canvas
- * type: render
- * title: Import XOPP to Canvas
- */
-
-import { activateNote, runOnBackend, showError, showMessage } from "trilium:api";
-import { useCallback, useRef, useState } from "trilium:preact";
-
-interface ExcalidrawElement {
- [key: string]: unknown;
- id: string;
- type: string;
- x: number;
- y: number;
- width: number;
- height: number;
- strokeColor: string;
- opacity: number;
-}
-
-interface ExcalidrawFile {
- mimeType: string;
- id: string;
- dataURL: string;
- created: number;
- lastRetrieved: number;
-}
-
-interface ImportStats {
- elementCount: number;
- fileCount: number;
- title: string;
-}
-
-// Xournal++ named colors → hex
-const XOPP_COLORS: Record<string, string> = {
- black: "#000000",
- blue: "#3333cc",
- red: "#ff0000",
- green: "#008000",
- gray: "#808080",
- lightblue: "#00c0ff",
- lightgreen: "#00ff00",
- magenta: "#ff00ff",
- orange: "#ff8000",
- yellow: "#ffff00",
- white: "#ffffff",
-};
-
-function convertColor(color: string | null): string {
- if (!color) return "#000000";
- const lower = color.toLowerCase();
- if (XOPP_COLORS[lower]) return XOPP_COLORS[lower];
- // #RRGGBBAA or #RRGGBB
- if (lower.startsWith("#")) {
- return lower.length > 7 ? lower.slice(0, 7) : lower;
- }
- return "#000000";
-}
-
-function extractOpacity(color: string | null): number {
- if (!color) return 100;
- if (color.startsWith("#") && color.length === 9) {
- const alpha = parseInt(color.slice(7, 9), 16);
- return Math.round((alpha / 255) * 100);
- }
- return 100;
-}
-
-function generateId() {
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- let id = "";
- for (let i = 0; i < 20; i++) {
- id += chars[Math.floor(Math.random() * chars.length)];
- }
- return id;
-}
-
-function makeBaseElement(type: string, x: number, y: number, width: number, height: number): ExcalidrawElement {
- return {
- id: generateId(),
- type,
- x,
- y,
- width,
- height,
- angle: 0,
- strokeColor: "#000000",
- backgroundColor: "transparent",
- fillStyle: "solid",
- strokeWidth: 1,
- strokeStyle: "solid",
- roughness: 0,
- opacity: 100,
- groupIds: [],
- frameId: null,
- index: null,
- roundness: null,
- seed: Math.floor(Math.random() * 2000000000),
- version: 1,
- versionNonce: Math.floor(Math.random() * 2000000000),
- isDeleted: false,
- boundElements: null,
- updated: Date.now(),
- link: null,
- locked: false,
- };
-}
-
-function convertStroke(strokeEl: Element, yOffset: number): ExcalidrawElement | null {
- const coordText = strokeEl.textContent.trim();
- const coords = coordText.split(/\s+/).map(Number);
- if (coords.length < 4) return null;
-
- const originX = coords[0];
- const originY = coords[1] + yOffset;
- const points: number[][] = [];
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
-
- for (let i = 0; i < coords.length; i += 2) {
- const px = coords[i] - originX;
- const py = (coords[i + 1] + yOffset) - originY;
- points.push([px, py]);
- minX = Math.min(minX, px);
- minY = Math.min(minY, py);
- maxX = Math.max(maxX, px);
- maxY = Math.max(maxY, py);
- }
-
- const widthAttr = strokeEl.getAttribute("width") || "2";
- const widths = widthAttr.trim().split(/\s+/).map(Number);
- const hasPressure = widths.length > 1;
-
- // Normalize pressures to 0-1 range
- let pressures: number[] = [];
- if (hasPressure) {
- const maxW = Math.max(...widths);
- pressures = widths.map((w) => maxW > 0 ? w / maxW : 0.5);
- // Pad or trim to match point count
- while (pressures.length < points.length) {
- pressures.push(pressures[pressures.length - 1] || 0.5);
- }
- pressures = pressures.slice(0, points.length);
- }
-
- const color = strokeEl.getAttribute("color") || "black";
- const tool = strokeEl.getAttribute("tool") || "pen";
-
- // Highlighter strokes should be semi-transparent
- const isHighlighter = tool === "highlighter";
-
- const el = makeBaseElement(
- "freedraw",
- originX,
- originY,
- maxX - minX,
- maxY - minY
- );
- el.points = points;
- el.pressures = hasPressure ? pressures : [];
- el.simulatePressure = !hasPressure;
- el.strokeColor = convertColor(color);
- el.strokeWidth = Math.floor(widths[0] || 1);
- console.log("Stroke width ", widths[0], el.strokeWidth);
- el.opacity = isHighlighter ? 40 : extractOpacity(color);
- el.lastCommittedPoint = points[points.length - 1] || null;
-
- return el;
-}
-
-function convertText(textEl: Element, yOffset: number): ExcalidrawElement | null {
- const x = parseFloat(textEl.getAttribute("x") ?? "0");
- const y = parseFloat(textEl.getAttribute("y") ?? "0") + yOffset;
- const size = parseFloat(textEl.getAttribute("size") ?? "12");
- const color = textEl.getAttribute("color") || "black";
- const content = textEl.textContent?.trim();
-
- if (!content) return null;
-
- // Rough estimate of text dimensions
- const lines = content.split("\n");
- const estWidth = Math.max(...lines.map((l) => l.length)) * size * 0.6;
- const estHeight = lines.length * size * 1.3;
-
- const el = makeBaseElement("text", x, y, estWidth, estHeight);
- el.text = content;
- el.fontSize = size;
- el.fontFamily = 5; // Excalidraw "Normal" font (Nunito)
- el.textAlign = "left";
- el.verticalAlign = "top";
- el.strokeColor = convertColor(color);
- el.opacity = extractOpacity(color);
- el.containerId = null;
- el.originalText = content;
- el.autoResize = true;
- el.lineHeight = 1.25;
-
- return el;
-}
-
-function convertImage(imageEl: Element, yOffset: number, files: Record<string, ExcalidrawFile>): ExcalidrawElement | null {
- const leftAttr = imageEl.getAttribute("left");
- const topAttr = imageEl.getAttribute("top");
- const rightAttr = imageEl.getAttribute("right");
- const bottomAttr = imageEl.getAttribute("bottom");
-
- if (!leftAttr || !topAttr || !rightAttr || !bottomAttr) return null;
-
- const left = parseFloat(leftAttr);
- const top = parseFloat(topAttr) + yOffset;
- const right = parseFloat(rightAttr);
- const bottom = parseFloat(bottomAttr) + yOffset;
-
- const imgData = imageEl.textContent?.trim();
- if (!imgData) return null;
-
- const fileId = generateId();
- files[fileId] = {
- mimeType: "image/png",
- id: fileId,
- dataURL: `data:image/png;base64,${imgData}`,
- created: Date.now(),
- lastRetrieved: Date.now(),
- };
-
- const el = makeBaseElement("image", left, top, right - left, bottom - top);
- el.fileId = fileId;
- el.status = "saved";
- el.scale = [1, 1];
-
- return el;
-}
-
-async function gunzip(buffer: ArrayBuffer): Promise<string> {
- const ds = new DecompressionStream("gzip");
- const stream = new Blob([buffer]).stream().pipeThrough(ds);
- return await new Response(stream).text();
-}
-
-async function convertXopp(buffer: ArrayBuffer) {
- const xmlString = await gunzip(buffer);
- const parser = new DOMParser();
- const doc = parser.parseFromString(xmlString, "text/xml");
-
- const parserError = doc.querySelector("parsererror");
- if (parserError) {
- throw new Error(`Failed to parse .xopp XML: ${ parserError.textContent}`);
- }
-
- const pages = doc.querySelectorAll("page");
- const elements: ExcalidrawElement[] = [];
- const files: Record<string, ExcalidrawFile> = {};
- let yOffset = 0;
- const PAGE_GAP = 80;
-
- for (const page of pages) {
- const pageHeight = parseFloat(page.getAttribute("height") ?? "792");
-
- // Draw a subtle page boundary rectangle
- const pageWidth = parseFloat(page.getAttribute("width") ?? "612");
- const bgRect = makeBaseElement("rectangle", 0, yOffset, pageWidth, pageHeight);
- bgRect.strokeColor = "#d0d0d0";
- bgRect.strokeWidth = 1;
- bgRect.strokeStyle = "dashed";
- bgRect.backgroundColor = "transparent";
- bgRect.opacity = 50;
- elements.push(bgRect);
-
- const layers = page.querySelectorAll("layer");
- for (const layer of layers) {
- for (const child of layer.children) {
- const tag = child.tagName.toLowerCase();
- let el: ExcalidrawElement | null = null;
-
- if (tag === "stroke") {
- el = convertStroke(child, yOffset);
- } else if (tag === "text") {
- el = convertText(child, yOffset);
- } else if (tag === "image") {
- el = convertImage(child, yOffset, files);
- }
-
- if (el) elements.push(el);
- }
- }
-
- yOffset += pageHeight + PAGE_GAP;
- }
-
- return {
- type: "excalidraw",
- version: 2,
- elements,
- files,
- appState: {
- gridModeEnabled: false,
- viewBackgroundColor: "#ffffff",
- },
- };
-}
-
-export default function XoppImporter() {
- const [status, setStatus] = useState<"idle" | "converting" | "done" | "error">("idle");
- const [message, setMessage] = useState("");
- const [stats, setStats] = useState<ImportStats | null>(null);
- const fileInputRef = useRef<HTMLInputElement>(null);
-
- const handleFile = useCallback(async (e: Event) => {
- const file = (e.target as HTMLInputElement)?.files?.[0];
- if (!file) return;
-
- if (!file.name.endsWith(".xopp")) {
- showError("Please select a .xopp file");
- return;
- }
-
- setStatus("converting");
- setMessage(`Converting ${file.name}...`);
-
- try {
- const buffer = await file.arrayBuffer();
- const excalidrawData = await convertXopp(buffer);
-
- const elementCount = excalidrawData.elements.length;
- const fileCount = Object.keys(excalidrawData.files).length;
- const title = file.name.replace(/\.xopp$/, "");
-
- // Create the canvas note on the backend
- const noteId = await runOnBackend(
- (title: string, content: string) => {
- const { note } = api.createNewNote({
- parentNoteId: "root",
- title,
- content,
- type: "canvas",
- mime: "application/json",
- });
- return note.noteId;
- },
- [title, JSON.stringify(excalidrawData)]
- );
-
- setStatus("done");
- setStats({ elementCount, fileCount, title });
- setMessage(`Created canvas note "${title}"`);
- showMessage(`Imported ${file.name} as canvas note`);
-
- // Navigate to the new note
- if (noteId) {
- activateNote(noteId);
- }
- } catch (err: unknown) {
- const msg = err instanceof Error ? err.message : String(err);
- setStatus("error");
- setMessage(`Error: ${msg}`);
- showError(`Failed to convert: ${msg}`);
- }
-
- // Reset file input
- if (fileInputRef.current) {
- fileInputRef.current.value = "";
- }
- }, []);
-
- return (
- <div style={{ padding: "20px", maxWidth: "600px" }}>
- <h2 style={{ marginTop: 0 }}>Xournal-- Importer</h2>
- <p style={{ color: "#666" }}>
- Convert <code>.xopp</code> files to Excalidraw canvas notes.
- Multi-page documents are stacked vertically.
- </p>
-
- <div
- style={{
- border: "2px dashed #aaa",
- borderRadius: "8px",
- padding: "30px",
- textAlign: "center",
- marginBottom: "16px",
- backgroundColor: status === "converting" ? "#fff8e1" : "#fafafa",
- }}
- >
- <input
- ref={fileInputRef}
- type="file"
- accept=".xopp"
- onChange={handleFile}
- disabled={status === "converting"}
- style={{ fontSize: "14px" }}
- />
- <p style={{ margin: "10px 0 0", fontSize: "13px", color: "#888" }}>
- Select a .xopp file from Xournal++
- </p>
- </div>
-
- {status === "converting" && (
- <div style={{ color: "#f57f17", fontWeight: "bold" }}>
- {message}
- </div>
- )}
-
- {status === "done" && stats && (
- <div
- style={{
- backgroundColor: "#e8f5e9",
- padding: "12px",
- borderRadius: "6px",
- }}
- >
- <strong>Done!</strong> {message}
- <br />
- <span style={{ fontSize: "13px", color: "#555" }}>
- {stats.elementCount} elements
- {stats.fileCount > 0 ? `, ${stats.fileCount} embedded images` : ""}
- </span>
- </div>
- )}
-
- {status === "error" && (
- <div
- style={{
- backgroundColor: "#ffebee",
- padding: "12px",
- borderRadius: "6px",
- color: "#c62828",
- }}
- >
- {message}
- </div>
- )}
- </div>
- );
-}