Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): normalize OneNote page HTML on import

Elian Dorancfa659bebe0d
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.ts
new file mode 100644
index 0000000000..25c43ee4b6
--- /dev/null
+++ b/apps/server/src/services/import/onenote/converter.spec.ts
@@ -0,0 +1,76 @@
+import { parse } from "node-html-parser";
+import { describe, expect, it } from "vitest";
+
+import converter from "./converter.js";
+
+// A representative OneNote page as returned by the Graph API: margin-0 paragraphs, a numbered and
+// two bulleted lists, a trailing empty list item, and bare block-level <br> elements that OneNote
+// uses for vertical spacing between blocks.
+const SAMPLE = `<html lang="en-US">
+ <head>
+ <title>2020-09-02. Customer Daily</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <meta name="created" content="2020-09-02T09:37:00.0000000" />
+ </head>
+ <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
+ <div style="position:absolute;left:48px;top:115px;width:576px">
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Participants:</span></p>
+ <ol>
+ <li>Participant 1</li>
+ <li>Participant 2</li>
+ </ol>
+ <br />
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Agenda:</span></p>
+ <ul>
+ <li>ACME-1234</li>
+ <li>ACME-1235</li>
+ <li><br />
+ </li>
+ </ul>
+ <br />
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Discussion:</span></p>
+ <ul>
+ <li>Subject 1</li>
+ <li>Subject 2</li>
+ </ul>
+ </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", () => {
+ const out = converter.convertPageHtml(SAMPLE);
+ const root = parse(out);
+
+ // The two bare <br> after </ol>/</ul> and the <br> in the empty <li> are all gone.
+ expect(root.querySelectorAll("br")).toHaveLength(0);
+
+ // The trailing empty Agenda item is removed; the three lists keep their real items.
+ const ol = root.querySelectorAll("ol");
+ const uls = root.querySelectorAll("ul");
+ expect(ol).toHaveLength(1);
+ expect(uls).toHaveLength(2);
+ expect(ol[0].querySelectorAll("li")).toHaveLength(2); // Participants 1-2
+ expect(uls[0].querySelectorAll("li")).toHaveLength(2); // Agenda: ACME-1234/1235 (empty dropped)
+ expect(uls[1].querySelectorAll("li")).toHaveLength(2); // Discussion 1-2
+
+ // Headings and list contents survive.
+ expect(out).toContain("Participants:");
+ expect(out).toContain("Participant 1");
+ expect(out).toContain("ACME-1234");
+ expect(out).toContain("Discussion:");
+ expect(out).toContain("Subject 2");
+ });
+
+ it("keeps genuine soft line breaks inside a paragraph", () => {
+ const out = converter.convertPageHtml(`<body><p>line one<br />line two</p></body>`);
+ expect(parse(out).querySelectorAll("br")).toHaveLength(1);
+ });
+
+ it("converts OneNote to-do tags into task-list checkboxes", () => {
+ const out = converter.convertPageHtml(`<body><p data-tag="to-do:completed">done</p><p data-tag="to-do">todo</p></body>`);
+ expect(out).toContain("[x] done");
+ expect(out).toContain("[ ] todo");
+ });
+});
diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.ts
index 8563187bf5..8999f78c07 100644
--- a/apps/server/src/services/import/onenote/converter.ts
+++ b/apps/server/src/services/import/onenote/converter.ts
@@ -1,34 +1,40 @@
/**
* Converts a OneNote page (a full HTML document returned by the Graph API) into the HTML body that
- * Trilium stores for a text note.
+ * Trilium stores for a text note: structural normalization followed by sanitization.
*
- * This is a deliberately minimal first pass: it extracts the document body and sanitizes it. The
- * Obsidian importer additionally rebuilds code blocks from Consolas-styled spans, converts MathML to
- * LaTeX, downloads attachments, and rasterizes ink to SVG — all of which are good future additions
- * but out of scope for this prototype. The OneNote "to-do" tags are converted here because they map
- * cleanly onto Trilium task-list checkboxes and are cheap to handle.
+ * Background on the normalization: OneNote's content model is paragraph-based (each Enter is a new
+ * <p>), but paragraphs render with zero spacing, and OneNote leans on bare <br> elements — including
+ * at the BLOCK level, as siblings of <p>/<ol>/<ul> — to create vertical gaps. CKEditor has no place
+ * for a block-level <br> (a soft break only lives inside a block) and provides its own inter-block
+ * spacing, so importing OneNote's <br>-based spacing verbatim produces doubled gaps and stray empty
+ * bullets. We therefore drop block-level <br> spacing and empty list items while preserving genuine
+ * in-paragraph soft breaks.
*
- * NOTE: images and file attachments are referenced via authenticated Graph URLs and are NOT yet
- * downloaded, so embedded media will not render after import. See the summary for follow-up work.
+ * Still a first pass: text formatting carried as inline styles (e.g. font-weight:bold) is currently
+ * dropped by the sanitizer rather than converted to semantic tags, and images/attachments (served
+ * from authenticated Graph URLs) are not downloaded yet. See the summary for follow-up work.
*/
import { sanitize } from "@triliumnext/core";
-import { parse } from "node-html-parser";
+import { HTMLElement, parse } from "node-html-parser";
+
+/** Block containers whose direct <br> children are spacing artifacts rather than soft breaks. */
+const BLOCK_CONTAINERS = new Set(["body", "div", "section", "article", "blockquote", "td", "th", "ol", "ul", "dl"]);
export function convertPageHtml(rawHtml: string): string {
const root = parse(rawHtml);
+ const scope = root.querySelector("body") ?? root;
- convertTodoTags(root);
-
- const body = root.querySelector("body");
- const inner = body ? body.innerHTML : rawHtml;
+ convertTodoTags(scope);
+ removeEmptyListItems(scope);
+ removeBlockLevelBreaks(scope);
- return sanitize.sanitizeHtml(inner);
+ return sanitize.sanitizeHtml(scope.innerHTML);
}
/** OneNote marks checkboxes with `data-tag="to-do"` / `"to-do:completed"`; render them as task items. */
-function convertTodoTags(root: ReturnType<typeof parse>) {
- for (const el of root.querySelectorAll("[data-tag]")) {
+function convertTodoTags(scope: HTMLElement) {
+ for (const el of scope.querySelectorAll("[data-tag]")) {
const tag = el.getAttribute("data-tag");
if (tag === "to-do" || tag === "to-do:completed") {
const checkbox = tag === "to-do:completed" ? "[x]" : "[ ]";
@@ -37,4 +43,25 @@ function convertTodoTags(root: ReturnType<typeof parse>) {
}
}
+/** Drops list items that hold nothing but whitespace/<br> (OneNote's "exited the list" remnant). */
+function removeEmptyListItems(scope: HTMLElement) {
+ for (const li of scope.querySelectorAll("li")) {
+ const hasText = li.textContent.trim().length > 0;
+ const hasNonBreakElement = li.querySelectorAll("*").some((el) => el.tagName?.toLowerCase() !== "br");
+ if (!hasText && !hasNonBreakElement) {
+ li.remove();
+ }
+ }
+}
+
+/** Removes <br> elements used as block-level spacing; soft breaks inside <p>/<span>/etc. are kept. */
+function removeBlockLevelBreaks(scope: HTMLElement) {
+ for (const br of scope.querySelectorAll("br")) {
+ const parent = br.parentNode;
+ if (parent instanceof HTMLElement && BLOCK_CONTAINERS.has(parent.tagName?.toLowerCase() ?? "")) {
+ br.remove();
+ }
+ }
+}
+
export default { convertPageHtml };