Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): preserve OneNote highlight and font colors

Elian Doran59c8186a799e
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.ts
index 864e1df531..2e103aed88 100644
--- a/apps/server/src/services/import/onenote/converter.spec.ts
+++ b/apps/server/src/services/import/onenote/converter.spec.ts
@@ -71,6 +71,18 @@ const SUPERSCRIPT_SAMPLE = `<html lang="en-US">
</body>
</html>`;
+// Real OneNote source (debug-captured): highlight and font color use CSS *named* colors, which the
+// sanitizer's colorRegex (hex/rgb/hsl only) would otherwise strip.
+const HIGHLIGHT_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"><span style="background-color:yellow">Yellow</span></p>
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="color:white;background-color:fuchsia">Magenta</span></p>
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="color:black;background-color:silver">Light Gray</span></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", () => {
@@ -124,4 +136,15 @@ describe("convertPageHtml", () => {
expect(out).toContain("<sup>sup</sup>");
expect(out).toContain("<del>strikethrough</del>");
});
+
+ it("maps OneNote named highlight/font colors to hex so they survive sanitization", () => {
+ const out = converter.convertPageHtml(HIGHLIGHT_SAMPLE);
+ expect(out).toContain("background-color:#ffff00"); // yellow
+ expect(out).toContain("color:#ffffff"); // white
+ expect(out).toContain("background-color:#ff00ff"); // fuchsia
+ expect(out).toContain("color:#000000"); // black
+ expect(out).toContain("background-color:#c0c0c0"); // silver
+ expect(out).toContain("Yellow");
+ expect(out).toContain("Magenta");
+ });
});
diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.ts
index f6cc24fbd0..50444b180f 100644
--- a/apps/server/src/services/import/onenote/converter.ts
+++ b/apps/server/src/services/import/onenote/converter.ts
@@ -20,12 +20,21 @@ 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'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"],
+ ["blue", "#0000ff"], ["red", "#ff0000"], ["navy", "#000080"], ["teal", "#008080"],
+ ["green", "#008000"], ["purple", "#800080"], ["maroon", "#800000"], ["olive", "#808000"],
+ ["gray", "#808080"], ["silver", "#c0c0c0"], ["black", "#000000"], ["white", "#ffffff"]
+]);
+
export function convertPageHtml(rawHtml: string): string {
const root = parse(rawHtml);
const scope = root.querySelector("body") ?? root;
convertTodoTags(scope);
convertInlineFormatting(scope);
+ normalizeNamedColors(scope);
removeEmptyListItems(scope);
removeBlockLevelBreaks(scope);
@@ -96,6 +105,32 @@ function parseStyle(style: string): Map<string, string> {
return declarations;
}
+/**
+ * Rewrites CSS named color/background-color values (e.g. `yellow`) to hex so they pass the sanitizer's
+ * color allowlist (which only permits hex/rgb/hsl). The styles stay as <span style="color|
+ * background-color"> — the representation Trilium's FontColor / FontBackgroundColor editor plugins use.
+ */
+function normalizeNamedColors(scope: HTMLElement) {
+ for (const el of scope.querySelectorAll("[style]")) {
+ const style = parseStyle(el.getAttribute("style") ?? "");
+ let changed = false;
+ for (const property of ["color", "background-color"]) {
+ const hex = NAMED_COLORS.get(style.get(property) ?? "");
+ if (hex) {
+ style.set(property, hex);
+ changed = true;
+ }
+ }
+ if (changed) {
+ el.setAttribute("style", serializeStyle(style));
+ }
+ }
+}
+
+function serializeStyle(style: Map<string, string>): string {
+ return [...style].map(([property, value]) => `${property}:${value}`).join(";");
+}
+
/** 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")) {