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): preserve OneNote list marker types
Elian Dorana5fd329ec983
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.tsindex 2a4a38539e..5568c1ce06 100644--- a/apps/server/src/services/import/onenote/converter.spec.ts+++ b/apps/server/src/services/import/onenote/converter.spec.ts@@ -159,6 +159,38 @@ const ALIGNMENT_SAMPLE = `<html lang="en-US"></body></html>`;+// Real OneNote source (debug-captured): OneNote puts the list marker type on each <li> and wraps+// item text in a margin-0 <p>. The marker type belongs on the <ul>/<ol> (CKEditor's representation),+// and OneNote's lower-alpha/upper-alpha map to CKEditor's lower-latin/upper-latin.+const LISTS_SAMPLE = `<html lang="en-US">+ <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">+ <div style="position:absolute;left:48px;top:115px;width:624px">+ <p style="margin-top:0pt;margin-bottom:0pt">Normal:</p>+ <br />+ <ul>+ <li><p style="margin-top:0pt;margin-bottom:0pt">Normal bullet</p>+ <ul>+ <li style="list-style-type:circle"><p style="margin-top:0pt;margin-bottom:0pt">Sub-bullet</p></li>+ </ul>+ </li>+ </ul>+ <p style="margin-top:0pt;margin-bottom:0pt">Hollow:</p>+ <ul>+ <li style="list-style-type:circle">Hollow</li>+ </ul>+ <p style="margin-top:0pt;margin-bottom:0pt">Alpha:</p>+ <ol>+ <li value="1" style="list-style-type:lower-alpha">A</li>+ <li style="list-style-type:lower-alpha">B</li>+ </ol>+ <p style="margin-top:0pt;margin-bottom:0pt">Roman:</p>+ <ol>+ <li value="1" style="list-style-type:upper-roman">A</li>+ </ol>+ </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", () => {@@ -201,6 +233,25 @@ describe("convertPageHtml", () => {]);});+ it("moves OneNote list marker types from <li> onto the list, mapping alpha to latin", () => {+ const out = converter.convertPageHtml(LISTS_SAMPLE);+ const root = parse(out);+ const uls = root.querySelectorAll("ul"); // [Normal-top, Normal-nested, Hollow]+ const ols = root.querySelectorAll("ol"); // [Alpha, Roman]++ // Top-level list takes its marker from the first item; nested list drops it; default = no style.+ expect(uls[0].getAttribute("style") ?? "").not.toContain("list-style-type"); // Normal (default)+ expect(uls[1].getAttribute("style") ?? "").not.toContain("list-style-type"); // nested → dropped+ expect(uls[2].getAttribute("style")).toContain("list-style-type:circle"); // Hollow+ expect(ols[0].getAttribute("style")).toContain("list-style-type:lower-latin"); // lower-alpha → latin+ expect(ols[1].getAttribute("style")).toContain("list-style-type:upper-roman");++ // The per-item markers and the wrapping <p> are gone.+ expect(root.querySelectorAll("li").every((li) => !(li.getAttribute("style") ?? "").includes("list-style-type"))).toBe(true);+ expect(root.querySelectorAll("li p")).toHaveLength(0);+ expect(out).toContain("Normal bullet");+ });+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 9c7c40d3e3..af4e06e09c 100644--- a/apps/server/src/services/import/onenote/converter.ts+++ b/apps/server/src/services/import/onenote/converter.ts@@ -12,6 +12,13 @@** Still a first pass: images/attachments (served from authenticated Graph URLs) are not downloaded* yet, so embedded media will not render. See the summary for follow-up work.+ *+ * Known unrecoverable loss: paragraph indentation is not preserved. `margin-left`/`text-indent`/+ * `padding-left` are not in the OneNote API's supported output-style set (only background-color,+ * color, font-family, font-size, font-style, font-weight, text-decoration and text-align survive —+ * see https://learn.microsoft.com/en-us/graph/onenote-input-output-html), so Graph emits every+ * paragraph with identical `margin-top:0pt;margin-bottom:0pt` and the indent level never reaches us.+ * No fetch option recovers it; the only indentation Graph keeps is list nesting (ol/ul/li).*/import { sanitize } from "@triliumnext/core";@@ -20,6 +27,9 @@ 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"]);+/** OneNote uses the CSS alpha keywords; CKEditor (and the sanitizer) use the latin equivalents. */+const LIST_STYLE_MAP = new Map<string, string>([["lower-alpha", "lower-latin"], ["upper-alpha", "upper-latin"]]);+/** OneNote's highlight/font palette uses the 16 basic CSS color names; map them to hex (see below). */const NAMED_COLORS = new Map<string, string>([["yellow", "#ffff00"], ["lime", "#00ff00"], ["aqua", "#00ffff"], ["fuchsia", "#ff00ff"],@@ -36,6 +46,8 @@ export function convertPageHtml(rawHtml: string): string {convertTodoTags(scope);convertInlineFormatting(scope);normalizeNamedColors(scope);+ unwrapListItemParagraphs(scope);+ normalizeListMarkers(scope);removeEmptyListItems(scope);removeBlockLevelBreaks(scope);@@ -213,6 +225,50 @@ function serializeStyle(style: Map<string, string>): string {return [...style].map(([property, value]) => `${property}:${value}`).join(";");}+/** Unwraps OneNote's margin-0 <p> wrappers inside <li> so item text sits directly in the <li>. */+function unwrapListItemParagraphs(scope: HTMLElement) {+ for (const li of scope.querySelectorAll("li")) {+ for (const child of [...li.childNodes]) {+ if (child instanceof HTMLElement && child.tagName?.toLowerCase() === "p") {+ child.insertAdjacentHTML("beforebegin", child.innerHTML);+ child.remove();+ }+ }+ }+}++/**+ * OneNote puts the list marker type on every <li> (e.g. list-style-type:circle / lower-alpha). CKEditor+ * instead carries it once on the <ul>/<ol>, so move the first item's marker onto the (top-level) list,+ * mapping lower-alpha/upper-alpha to CKEditor's lower-latin/upper-latin, and strip it from all items.+ * Nested lists keep the default marker.+ */+function normalizeListMarkers(scope: HTMLElement) {+ for (const list of scope.querySelectorAll("ul, ol")) {+ const parent = list.parentNode;+ const nested = parent instanceof HTMLElement && parent.tagName?.toLowerCase() === "li";+ if (nested) {+ continue;+ }+ const firstItem = list.childNodes.find((node): node is HTMLElement => node instanceof HTMLElement && node.tagName?.toLowerCase() === "li");+ const marker = firstItem && parseStyle(firstItem.getAttribute("style") ?? "").get("list-style-type");+ if (marker) {+ list.setAttribute("style", `list-style-type:${LIST_STYLE_MAP.get(marker) ?? marker};`);+ }+ }++ for (const li of scope.querySelectorAll("li")) {+ const style = parseStyle(li.getAttribute("style") ?? "");+ if (style.delete("list-style-type")) {+ if (style.size === 0) {+ li.removeAttribute("style");+ } else {+ li.setAttribute("style", serializeStyle(style));+ }+ }+ }+}+/** 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")) {