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
fix(import): strip OneNote's default black text color for dark themes
Elian Dorana4da4e58e6f5
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.tsindex 572ea1c5b5..bc696dfe58 100644--- a/apps/server/src/services/import/onenote/converter.spec.ts+++ b/apps/server/src/services/import/onenote/converter.spec.ts@@ -459,10 +459,40 @@ describe("convertPageHtml", () => {expect(out).toContain("background-color:#ffff00"); // yellowexpect(out).toContain("color:#ffffff"); // whiteexpect(out).toContain("background-color:#ff00ff"); // fuchsia- expect(out).toContain("color:#000000"); // black- expect(out).toContain("background-color:#c0c0c0"); // silver+ expect(out).not.toContain("color:#000000"); // black is the default color and is stripped (see below)+ expect(out).toContain("background-color:#c0c0c0"); // silver kept even though its text was blackexpect(out).toContain("Yellow");expect(out).toContain("Magenta");+ expect(out).toContain("Light Gray");+ });++ // OneNote stamps an explicit color:#000000 (its automatic color, since a page is a white canvas) on+ // essentially every run of body text. Kept verbatim it overrides the theme foreground and renders as+ // unreadable black-on-dark under a dark theme, so default black is treated as "automatic" and dropped.+ it("strips OneNote's default black text color so text inherits the theme foreground", () => {+ const sample = `<html><body><div style="position:absolute;left:48px;top:115px;width:624px">+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="color:#000000">Default body text</span></p>+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="color:#000000"><strong>Bold black</strong></span></p>+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="color:black">Named black</span></p>+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="color:#7030a0"><strong>Purple</strong></span></p>+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="color:#000000;background-color:#ffff00">Black on yellow</span></p>+ </div></body></html>`;+ const out = converter.convertPageHtml(sample);++ // No black color survives, in either hex or named form.+ expect(out).not.toContain("color:#000000");+ expect(out).not.toContain("color:black");++ // The now-attribute-less spans are unwrapped, leaving the text (and its formatting) directly.+ expect(out).toContain("Default body text");+ expect(out).toContain("<strong>Bold black</strong>");+ expect(out).toContain("Named black");++ // Deliberate non-black colors are untouched.+ expect(out).toContain("color:#7030a0");+ // A span that also carries a highlight keeps the highlight but loses the black text color.+ expect(out).toContain("background-color:#ffff00");+ expect(out).toContain("Black on yellow");});it("keeps OneNote heading styles (level-shifted), cite, italic and font colors", () => {diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.tsindex 910bbbdc44..b31ad3e572 100644--- a/apps/server/src/services/import/onenote/converter.ts+++ b/apps/server/src/services/import/onenote/converter.ts@@ -110,6 +110,7 @@ export function convertPageHtml(rawHtml: string): string {convertTags(scope);convertInlineFormatting(scope);normalizeNamedColors(scope);+ removeDefaultTextColor(scope);unwrapListItemParagraphs(scope);normalizeListMarkers(scope);normalizeTableBorders(scope);@@ -350,6 +351,39 @@ function serializeStyle(style: Map<string, string>): string {return [...style].map(([property, value]) => `${property}:${value}`).join(";");}+/** OneNote's automatic/default text color in the forms it can reach us in (named black already mapped to hex). */+const DEFAULT_TEXT_COLORS = new Set(["#000000", "#000", "black"]);++/**+ * OneNote treats a page as a white canvas, so it stamps an explicit `color:#000000` (its automatic+ * text color) on essentially every run of body text. Preserved verbatim, that hard black overrides the+ * note's theme-inherited foreground and renders as unreadable black-on-dark under a dark theme. Treat+ * default black as "automatic" and strip it so the text inherits the theme color — identical under a+ * light theme, where the default already is black. A span left with no other styling is unwrapped so we+ * don't leave bare `<span>` noise behind; deliberately colored non-black text and background-colors+ * (e.g. highlights) are untouched. Runs after normalizeNamedColors so `color:black` has become hex.+ */+function removeDefaultTextColor(scope: HTMLElement) {+ for (const el of scope.querySelectorAll("[style]")) {+ const style = parseStyle(el.getAttribute("style") ?? "");+ if (!DEFAULT_TEXT_COLORS.has(style.get("color") ?? "")) {+ continue;+ }+ style.delete("color");++ if (style.size > 0) {+ el.setAttribute("style", serializeStyle(style));+ continue;+ }++ el.removeAttribute("style");+ if (el.tagName?.toLowerCase() === "span") {+ el.insertAdjacentHTML("beforebegin", el.innerHTML);+ el.remove();+ }+ }+}+/** 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")) {