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 Notion text and background colors
Elian Doran3930ae9f9114
diff --git a/packages/trilium-core/src/services/import/notion/converter.spec.ts b/packages/trilium-core/src/services/import/notion/converter.spec.tsindex 36826cf7ed..9b4cf92ebf 100644--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts@@ -175,6 +175,32 @@ describe("convertNotionHtml — bookmarks", () => {});});+describe("convertNotionHtml — colors", () => {+ it("maps a text-color mark to a span with an opaque color style", () => {+ expect(convertNotionHtml(`<p><mark class="highlight-blue">Highlight</mark></p>`)).toBe(+ `<p><span style="color:rgb(56, 125, 201)">Highlight</span></p>`+ );+ });++ it("flattens a translucent background mark over white into a solid background-color", () => {+ // gray_background is rgba(42, 28, 0, 0.07); over white that resolves to a light tint, not near-black.+ expect(convertNotionHtml(`<p><mark class="highlight-gray_background">Highlight</mark></p>`)).toBe(+ `<p><span style="background-color:rgb(240, 239, 237)">Highlight</span></p>`+ );+ });++ it("drops the wrapper for default (uncolored) marks, keeping nested colored content", () => {+ // Notion nests a background mark inside a default text-color mark; only the inner color survives.+ expect(convertNotionHtml(`<p><mark class="highlight-default"><mark class="highlight-red_background">Highlight</mark></mark></p>`)).toBe(+ `<p><span style="background-color:rgb(252, 233, 231)">Highlight</span></p>`+ );+ });++ it("leaves a plain default mark as bare text", () => {+ expect(convertNotionHtml(`<p><mark class="highlight-default">Plain</mark></p>`)).toBe(`<p>Plain</p>`);+ });+});+describe("convertNotionHtml — tables", () => {it("rewrites a Notion simple-table into a canonical figure.table with scoped headers", () => {const input = `<div style="display:contents" dir="ltr"><table id="386c5eca" class="simple-table"><thead class="simple-table-header"><div style="display:contents" dir="ltr"><tr id="r0"><th id="?Hr:" class="simple-table-header-color simple-table-header" style="width:239.7px">Column 1</th><th id="SDzW" class="simple-table-header-color simple-table-header" style="width:239.7px">Column 2</th><th id="q@CU" class="simple-table-header-color simple-table-header" style="width:239.7px">Column 3</th></tr></div></thead><tbody><div style="display:contents" dir="ltr"><tr id="r1"><th id="?Hr:" class="simple-table-header-color simple-table-header" style="width:239.7px">Row 1</th><td id="SDzW" class="" style="width:239.7px">A</td><td id="q@CU" class="" style="width:239.7px">B</td></tr></div><div style="display:contents" dir="ltr"><tr id="r2"><th id="?Hr:" class="simple-table-header-color simple-table-header" style="width:239.7px">Row 2</th><td id="SDzW" class="" style="width:239.7px">C</td><td id="q@CU" class="" style="width:239.7px">D</td></tr></div></tbody></table></div>`;diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.tsindex 09733543c5..83764bd39c 100644--- a/packages/trilium-core/src/services/import/notion/converter.ts+++ b/packages/trilium-core/src/services/import/notion/converter.ts@@ -24,6 +24,7 @@ export function convertNotionHtml(html: string): string {convertCodeBlocks(root);convertCallouts(root);convertBookmarks(root);+ convertColors(root);convertLinkToPage(root);return root.toString();}@@ -393,6 +394,73 @@ function convertBookmarks(root: HTMLElement) {}// #endregion+// #region Colors+/**+ * Notion's fixed text/background palette, taken verbatim from the export's stylesheet (the `<style>` block+ * isn't passed to this converter, so the values are inlined here). Text colors are opaque; background+ * colors are translucent overlays that {@link flattenOverWhite} resolves to solid, sanitizer-safe colors.+ */+const NOTION_TEXT_COLORS: Record<string, string> = {+ gray: "rgba(125, 122, 117, 1)",+ brown: "rgba(159, 118, 90, 1)",+ orange: "rgba(210, 123, 45, 1)",+ yellow: "rgba(203, 148, 52, 1)",+ teal: "rgba(80, 148, 110, 1)",+ blue: "rgba(56, 125, 201, 1)",+ purple: "rgba(154, 107, 180, 1)",+ pink: "rgba(193, 76, 138, 1)",+ red: "rgba(207, 81, 72, 1)"+};+const NOTION_BG_COLORS: Record<string, string> = {+ gray: "rgba(42, 28, 0, 0.07)",+ brown: "rgba(139, 46, 0, 0.086)",+ orange: "rgba(224, 101, 1, 0.129)",+ yellow: "rgba(211, 168, 0, 0.137)",+ teal: "rgba(0, 100, 45, 0.09)",+ blue: "rgba(0, 124, 215, 0.094)",+ purple: "rgba(102, 0, 178, 0.078)",+ pink: "rgba(197, 0, 93, 0.086)",+ red: "rgba(223, 22, 0, 0.094)"+};++/**+ * Notion encodes both text and background color as `<mark class="highlight-<color>[_background]">`. Those+ * class names are meaningless to CKEditor (so every colored run collapses to the same default highlight),+ * and `<mark>` itself is CKEditor's highlight marker. Rewrite each into a `<span>` carrying an inline+ * `color`/`background-color` style — CKEditor's font-color form — dropping the wrapper for the `default`+ * (uncolored) variants. Runs in reverse document order so nested marks are resolved innermost-first.+ */+function convertColors(root: HTMLElement) {+ for (const mark of [...root.querySelectorAll("mark")].reverse()) {+ const style = markStyle(mark.getAttribute("class") ?? "");+ mark.insertAdjacentHTML("beforebegin", style ? `<span style="${style}">${mark.innerHTML}</span>` : mark.innerHTML);+ mark.remove();+ }+}++/** Maps a Notion `highlight-*` class to a CKEditor inline color style, or undefined for default/unknown. */+function markStyle(className: string): string | undefined {+ const match = className.match(/highlight-([a-z]+)(_background)?\b/);+ if (!match || match[1] === "default") {+ return undefined;+ }+ const [, name, isBackground] = match;+ const rgba = isBackground ? NOTION_BG_COLORS[name] : NOTION_TEXT_COLORS[name];+ return rgba ? `${isBackground ? "background-color" : "color"}:${flattenOverWhite(rgba)}` : undefined;+}++/** Blends an `rgba(r, g, b, a)` color over a white page into an opaque `rgb(r, g, b)` the sanitizer accepts. */+function flattenOverWhite(rgba: string): string {+ const match = rgba.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)/);+ if (!match) {+ return rgba;+ }+ const alpha = match[4] === undefined ? 1 : Number(match[4]);+ const blend = (channel: string) => Math.round(Number(channel) * alpha + 255 * (1 - alpha));+ return `rgb(${blend(match[1])}, ${blend(match[2])}, ${blend(match[3])})`;+}+// #endregion+// #region Link-to-page blocks/*** Notion's "link to page" block is `<figure class="link-to-page"><a href="…page.html">Title</a></figure>`.