Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(import): order OneNote positioned text boxes by position
Elian Doran1e1d8279c245
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.tsindex 3beb960b3e..de5555ae87 100644--- a/apps/server/src/services/import/onenote/converter.spec.ts+++ b/apps/server/src/services/import/onenote/converter.spec.ts@@ -116,6 +116,25 @@ const TAGS_SAMPLE = `<html lang="en-US"></body></html>`;+// Real OneNote source (debug-captured): a free-form page with four absolutely-positioned text boxes+// whose document order differs from their visual (top-to-bottom) reading order.+const ALIGNMENT_SAMPLE = `<html lang="en-US">+ <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">+ <div style="position:absolute;left:512px;top:210px;width:624px">+ <p style="margin-top:0pt;margin-bottom:0pt">The quick</p>+ </div>+ <div style="position:absolute;left:318px;top:333px;width:624px">+ <p style="margin-top:0pt;margin-bottom:0pt">Brown fox</p>+ </div>+ <div style="position:absolute;left:579px;top:441px;width:624px">+ <p style="margin-top:0pt;margin-bottom:0pt">Jumps over the lazy dog</p>+ </div>+ <div style="position:absolute;left:222px;top:277px;width:624px">+ <p style="margin-top:0pt;margin-bottom:0pt">And then it jumps again.</p>+ </div>+ </body>+</html>`;+// Tests assert the end result (the HTML actually stored on the note, i.e. after sanitization).describe("convertPageHtml", () => {it("strips OneNote's block-level <br> spacing and empty list items, keeping real content", () => {@@ -147,6 +166,17 @@ describe("convertPageHtml", () => {expect(parse(out).querySelectorAll("br")).toHaveLength(1);});+ it("orders absolutely-positioned text boxes by position (top, then left)", () => {+ const out = converter.convertPageHtml(ALIGNMENT_SAMPLE);+ const paragraphs = parse(out).querySelectorAll("p").map((p) => p.textContent.trim());+ expect(paragraphs).toEqual([+ "The quick",+ "And then it jumps again.",+ "Brown fox",+ "Jumps over the lazy dog"+ ]);+ });+it("converts OneNote to-do tags into a Trilium task list", () => {const out = converter.convertPageHtml(TAGS_SAMPLE);const root = parse(out);diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.tsindex e3050794d0..5283c8f7f9 100644--- a/apps/server/src/services/import/onenote/converter.ts+++ b/apps/server/src/services/import/onenote/converter.ts@@ -32,6 +32,7 @@ export function convertPageHtml(rawHtml: string): string {const root = parse(rawHtml);const scope = root.querySelector("body") ?? root;+ sortPositionedOutlines(scope);convertTodoTags(scope);convertInlineFormatting(scope);normalizeNamedColors(scope);@@ -41,6 +42,30 @@ export function convertPageHtml(rawHtml: string): string {return sanitize.sanitizeHtml(scope.innerHTML);}+/**+ * A OneNote page is a free-form canvas of absolutely-positioned text boxes (the top-level outline+ * <div>s). Their document order need not match their visual order, so reorder them top-to-bottom then+ * left-to-right to linearize the page into a sensible reading order. A no-op for the common+ * single-outline page.+ */+function sortPositionedOutlines(scope: HTMLElement) {+ const outlines = scope.childNodes.filter(+ (node): node is HTMLElement =>+ node instanceof HTMLElement && node.tagName?.toLowerCase() === "div" && parseStyle(node.getAttribute("style") ?? "").has("position")+ );+ if (outlines.length < 2) {+ return;+ }++ const coord = (el: HTMLElement, property: string) => parseFloat(parseStyle(el.getAttribute("style") ?? "").get(property) ?? "") || 0;+ const sorted = [...outlines].sort((a, b) => coord(a, "top") - coord(b, "top") || coord(a, "left") - coord(b, "left"));++ for (const outline of sorted) {+ outline.remove();+ scope.appendChild(outline);+ }+}+/*** OneNote marks checkboxes as `<p data-tag="to-do">` / `"to-do:completed"` paragraphs. Group runs of* consecutive to-do paragraphs into a single CKEditor task list (<ul class="todo-list">…), matching