Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/notion): render column layouts as borderless tables

Elian Doranafe2add6cbe7
diff --git a/packages/trilium-core/src/services/import/notion/converter.spec.ts b/packages/trilium-core/src/services/import/notion/converter.spec.ts
index 40d49f7f4f..22c86fd52b 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -211,6 +211,40 @@ describe("convertNotionHtml — list fragmentation", () => {
});
});
+describe("convertNotionHtml — columns", () => {
+ // Notion column layouts are a <div class="column-list"> of <div class="column" style="width:N%"> divs,
+ // each wrapping its content in display:contents. They render as a single-row borderless table.
+ const dc = (inner: string) => `<div style="display:contents" dir="auto">${inner}</div>`;
+ const column = (width: string, text: string) =>
+ dc(`<div style="width:${width}" class="column">${dc(`<p class="">${text}</p>`)}</div>`);
+ const columnList = (...cols: string[]) => dc(`<div id="x" class="column-list">${cols.join("")}</div>`);
+
+ it("renders a two-column layout as a borderless table with per-cell widths", () => {
+ const input = columnList(column("50%", "First column"), column("50%", "Second column"));
+ expect(convertNotionHtml(input)).toBe(
+ `<figure class="table"><table style="border-color:transparent;"><tbody><tr>` +
+ `<td style="border-color:transparent;width:50%;">First column</td>` +
+ `<td style="border-color:transparent;width:50%;">Second column</td>` +
+ `</tr></tbody></table></figure>`
+ );
+ });
+
+ it("rounds an awkward column width to two decimals", () => {
+ const input = columnList(
+ column("33.33333333333333%", "First column"),
+ column("33.33333333333333%", "Second column"),
+ column("33.33333333333333%", "Third column")
+ );
+ expect(convertNotionHtml(input)).toBe(
+ `<figure class="table"><table style="border-color:transparent;"><tbody><tr>` +
+ `<td style="border-color:transparent;width:33.33%;">First column</td>` +
+ `<td style="border-color:transparent;width:33.33%;">Second column</td>` +
+ `<td style="border-color:transparent;width:33.33%;">Third column</td>` +
+ `</tr></tbody></table></figure>`
+ );
+ });
+});
+
describe("convertNotionHtml — callouts", () => {
const callout = (emoji: string, body: string) =>
`<div style="display:contents" dir="ltr"><figure class="block-color-gray_background callout" style="white-space:pre-wrap;display:flex" id="386c5eca"><div style="font-size:1.5em"><span class="icon">${emoji}</span></div><div style="width:100%"><div style="display:contents" dir="auto">${body}</div></div></figure></div>`;
diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.ts
index 699657563f..923421761e 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -22,6 +22,7 @@ export function convertNotionHtml(html: string): string {
dropTableOfContents(root);
unwrapDisplayContents(root);
mergeFragmentedLists(root);
+ convertColumns(root);
convertTables(root);
convertImages(root);
convertAttachments(root);
@@ -325,6 +326,48 @@ function isMergeableList(el: HTMLElement): boolean {
}
// #endregion
+// #region Columns
+/**
+ * Notion column layouts (`<div class="column-list">` of `<div class="column" style="width:N%">`) have no
+ * Trilium/CKEditor equivalent, so render them as a single-row borderless table — one `<td>` per column,
+ * carrying that column's width. Both the table and every cell get `border-color:transparent` so the result
+ * reads as side-by-side content rather than a grid, and the width is rounded to two decimals to match
+ * CKEditor's column widths. A column whose only block is a `<p>` is unwrapped, so a one-paragraph column
+ * becomes a plain cell. (Nested column lists aren't flattened yet — an inner list is left in its cell.)
+ */
+function convertColumns(root: HTMLElement) {
+ for (const columnList of root.querySelectorAll("div.column-list")) {
+ if (!columnList.parentNode) {
+ continue; // a nested list already removed together with its parent
+ }
+ const columns = directChildren(columnList, "div").filter((column) => column.classList.contains("column"));
+ const evenWidth = `${Math.round((100 / columns.length) * 100) / 100}%`;
+ const cells = columns
+ .map((column) => `<td style="border-color:transparent;width:${columnWidth(column) ?? evenWidth};">${cellContent(column)}</td>`)
+ .join("");
+ columnList.insertAdjacentHTML("beforebegin",
+ `<figure class="table"><table style="border-color:transparent;"><tbody><tr>${cells}</tr></tbody></table></figure>`);
+ columnList.remove();
+ }
+}
+
+/** A Notion column's width (`style="width:N%"`) rounded to two decimals (`33.33333%` → `33.33%`). */
+function columnWidth(column: HTMLElement): string | undefined {
+ const raw = column.getAttribute("style")?.match(/width:\s*([\d.]+)%/)?.[1];
+ return raw !== undefined ? `${Math.round(parseFloat(raw) * 100) / 100}%` : undefined;
+}
+
+/** A column's cell content: a sole `<p>`'s inner HTML (so a one-paragraph column is a plain cell), else all of it. */
+function cellContent(column: HTMLElement): string {
+ const elements = column.childNodes.filter((node): node is HTMLElement => node instanceof HTMLElement);
+ const [only] = elements;
+ if (elements.length === 1 && only && isTag(only, "p")) {
+ return only.innerHTML;
+ }
+ return column.innerHTML;
+}
+// #endregion
+
// #region Tables
/**
* Notion tables are `<table class="simple-table">` with Notion-specific classes, ids and pixel widths on