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): convert Notion tables to canonical figure.table
Elian Doran18368a0c37c3
diff --git a/packages/trilium-core/src/services/import/notion/converter.spec.ts b/packages/trilium-core/src/services/import/notion/converter.spec.tsindex 2640a0ec69..325586b28d 100644--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts@@ -167,6 +167,15 @@ describe("convertNotionHtml — bookmarks", () => {});});+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>`;+ expect(convertNotionHtml(input)).toBe(+ `<figure class="table"><table><thead><tr><th scope="col">Column 1</th><th scope="col">Column 2</th><th scope="col">Column 3</th></tr></thead><tbody><tr><th scope="row">Row 1</th><td>A</td><td>B</td></tr><tr><th scope="row">Row 2</th><td>C</td><td>D</td></tr></tbody></table></figure>`+ );+ });+});+describe("convertNotionHtml — date mentions", () => {it("strips the @ prefix Notion puts on inline <time> date mentions", () => {expect(convertNotionHtml(`<p><time>@June 21, 2026</time></p>`)).toBe(`<p><time>June 21, 2026</time></p>`);diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.tsindex ad02263475..9ea7b2d153 100644--- a/packages/trilium-core/src/services/import/notion/converter.ts+++ b/packages/trilium-core/src/services/import/notion/converter.ts@@ -17,11 +17,47 @@ export function convertNotionHtml(html: string): string {convertTodoLists(root);convertToggles(root);unwrapDisplayContents(root);+ convertTables(root);convertCallouts(root);convertBookmarks(root);return root.toString();}+/**+ * Notion tables are `<table class="simple-table">` with Notion-specific classes, ids and pixel widths on+ * every element, and the `<tr>`s wrapped in `display:contents` divs (already removed by the time this+ * runs). Rewrite each into Trilium's canonical form: strip the Notion attributes, mark header cells with+ * `scope` (col in the head, row in the body), and wrap the table in `<figure class="table">`.+ */+function convertTables(root: HTMLElement) {+ for (const table of root.querySelectorAll("table.simple-table")) {+ stripTableAttributes(table);+ for (const th of table.querySelectorAll("thead th")) {+ th.setAttribute("scope", "col");+ }+ for (const th of table.querySelectorAll("tbody th")) {+ th.setAttribute("scope", "row");+ }+ table.insertAdjacentHTML("beforebegin", `<figure class="table">${table.toString()}</figure>`);+ table.remove();+ }+}++/** Removes Notion's class/id/style from the table and every cell/row/section, keeping colspan/rowspan. */+function stripTableAttributes(table: HTMLElement) {+ const strip = (el: HTMLElement) => {+ el.removeAttribute("class");+ el.removeAttribute("id");+ el.removeAttribute("style");+ };+ strip(table);+ for (const tag of ["thead", "tbody", "tr", "th", "td"]) {+ for (const el of table.querySelectorAll(tag)) {+ strip(el);+ }+ }+}+/*** Notion bookmark cards are `<figure><a class="bookmark source" href="…"><div class="bookmark-info">…`,* carrying a title, description and favicon. Trilium has the equivalent link-embed, so rewrite each intodiff --git a/packages/trilium-core/src/services/sanitizer.spec.ts b/packages/trilium-core/src/services/sanitizer.spec.tsindex c4242a5ea9..319e0cd0aa 100644--- a/packages/trilium-core/src/services/sanitizer.spec.ts+++ b/packages/trilium-core/src/services/sanitizer.spec.ts@@ -63,6 +63,11 @@ describe("sanitize", () => {expect(sanitizeHtml(dirty)).toBe(dirty);});+ it("keeps the scope attribute on table header cells", () => {+ const dirty = `<table><thead><tr><th scope="col">C</th></tr></thead><tbody><tr><th scope="row">R</th><td>A</td></tr></tbody></table>`;+ expect(sanitizeHtml(dirty)).toBe(dirty);+ });+describe("bookmark anchors", () => {it("preserves id attribute on empty <a> tags (CKEditor bookmarks)", () => {const dirty = `<a id="my-bookmark"></a>`;diff --git a/packages/trilium-core/src/services/sanitizer.ts b/packages/trilium-core/src/services/sanitizer.tsindex fd068e1275..53ed10951f 100644--- a/packages/trilium-core/src/services/sanitizer.ts+++ b/packages/trilium-core/src/services/sanitizer.ts@@ -52,7 +52,7 @@ export function sanitizeHtml(dirtyHtml: string) {input: ["type", "checked"],img: ["width", "height"],td: ["colspan", "rowspan"],- th: ["colspan", "rowspan"],+ th: ["colspan", "rowspan", "scope"],code: [ "spellcheck" ]},allowedStyles: {