Skip to content

Build your personal knowledge base with Trilium Notes

Embed full-export inline Notion databases as include-notes

Elian Doran4c2649ae9257
diff --git a/packages/trilium-core/src/services/import/notion/importer.integration.spec.ts b/packages/trilium-core/src/services/import/notion/importer.integration.spec.ts
index c84e687fd2..c01072478f 100644
--- a/packages/trilium-core/src/services/import/notion/importer.integration.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/importer.integration.spec.ts
@@ -141,6 +141,37 @@ describe("Notion importer — integration", () => {
expect(importRoot.getChildNotes().map((note) => note.title).sort()).toEqual(["Page A", "Page B"]);
});
+ it("embeds a full-export inline database as an include-note pointing at the imported collection", async () => {
+ // A workspace ("database-wide") export renders an inline database as a link to its separately-exported
+ // CSV (not a table). The database is imported as a child collection; the link must become an
+ // include-note embedding it, rather than a dangling reference to a `.csv` file.
+ const pageId = "38ac5eca1b8b8075b965f506658aeb1f";
+ const dbId = "38ac5eca1b8b808babeaf10c0980fa5b";
+ // Faithful to the real export: the block is wrapped in a display:contents div and carries the
+ // database id on the (sanitizer-stripped) div id, with the resolvable id living in the .csv href.
+ const collectionBlock =
+ `<div style="display:contents" dir="ltr"><div id="38ac5eca-1b8b-808b-abea-f10c0980fa5b" class="collection-content"><h4 class="collection-title">Database title</h4>` +
+ `<a href="Inline%20database%20test/Database%20title%20${dbId}.csv"><code>Inline database test/Database title ${dbId}.csv</code></a></div></div>`;
+ const importRoot = await importNotion({
+ [`Inline database test ${pageId}.html`]:
+ `<html><head><title>Inline database test</title></head><body><div id="${pageId}"><div class="page-body"><p>Before</p>${collectionBlock}<p>After</p></div></div></body></html>`,
+ [`Inline database test/Database title ${dbId}.csv`]: "Name\nFirst\nSecond",
+ [`Inline database test/Database title/First 38ac5eca1b8b8069afb5c9fe40e53c42.html`]: pageHtml("First", "38ac5eca1b8b8069afb5c9fe40e53c42"),
+ [`Inline database test/Database title/Second 38ac5eca1b8b8025bf73d86d81713eb3.html`]: pageHtml("Second", "38ac5eca1b8b8025bf73d86d81713eb3")
+ });
+
+ const page = importRoot.getChildNotes().find((note) => note.title === "Inline database test");
+ const database = page?.getChildNotes().find((note) => note.title === "Database title");
+ // The database imported as a table collection with its rows nested under it.
+ expect(database?.type).toBe("book");
+ expect(database?.getChildNotes().map((note) => note.title).sort()).toEqual(["First", "Second"]);
+ // The page body embeds that collection inline and no longer references the raw CSV.
+ const content = page?.getContent();
+ expect(content).toContain(`<section class="include-note" data-note-id="${database?.noteId}" data-box-size="medium">`);
+ expect(content).not.toContain(".csv");
+ expect(content).not.toContain("collection-content");
+ });
+
it("groups a CSV-only database's rows under a container note, instead of orphaning them to the root", async () => {
// A Notion inline/linked database exports as a `.csv` with no sibling `.html`; its rows live in a
// folder named after the database. Nothing owns that folder, so without a container they'd be flat.
diff --git a/packages/trilium-core/src/services/import/notion/importer.spec.ts b/packages/trilium-core/src/services/import/notion/importer.spec.ts
index a9434970c9..53e0204924 100644
--- a/packages/trilium-core/src/services/import/notion/importer.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/importer.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { rewriteLinks } from "./importer.js";
+import { rewriteCollectionIncludes, rewriteLinks } from "./importer.js";
import type { LinkTarget } from "./model.js";
describe("rewriteLinks", () => {
@@ -47,3 +47,32 @@ describe("rewriteLinks", () => {
expect(rewriteLinks(input, resolveSubpage)).toBe(input);
});
});
+
+describe("rewriteCollectionIncludes", () => {
+ const dbId = "38ac5eca1b8b808babeaf10c0980fa5b";
+ const resolveDatabase = (notionId: string): LinkTarget | null =>
+ notionId === dbId ? { noteId: "dbNote123", title: "Database title" } : null;
+
+ // The full-export inline-database shape: a div linking to the separately-exported CSV.
+ const block = (id: string) =>
+ `<div class="collection-content"><h4 class="collection-title">Database title</h4>` +
+ `<a href="Inline%20database%20test/Database%20title%20${id}.csv"><code>x</code></a></div>`;
+
+ it("replaces an inline-database CSV reference with an include-note for the imported collection", () => {
+ const input = `<p>Before</p>${block(dbId)}<p>After</p>`;
+ expect(rewriteCollectionIncludes(input, resolveDatabase)).toBe(
+ `<p>Before</p><section class="include-note" data-note-id="dbNote123" data-box-size="medium">&nbsp;</section><p>After</p>`
+ );
+ });
+
+ it("leaves the block untouched when the referenced database wasn't imported", () => {
+ const input = block("ffffffffffffffffffffffffffffffff");
+ expect(rewriteCollectionIncludes(input, resolveDatabase)).toBe(input);
+ });
+
+ it("ignores a rendered collection table (the partial-export shape)", () => {
+ // A partial export inlines the actual rows as a <table>, which has no CSV to resolve and is kept.
+ const table = `<table class="collection-content"><tbody><tr><td>Foo</td></tr></tbody></table>`;
+ expect(rewriteCollectionIncludes(table, resolveDatabase)).toBe(table);
+ });
+});
diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.ts
index 477a969527..1150b0ee4f 100644
--- a/packages/trilium-core/src/services/import/notion/importer.ts
+++ b/packages/trilium-core/src/services/import/notion/importer.ts
@@ -239,7 +239,7 @@ function createNotes(importRootNote: BNote, pages: ParsedPage[], resources: Map<
// Second pass: now that every page has a note, resolve cross-page links and relations, then persist.
const resolveTarget = (notionId: string): LinkTarget | null => targetByPageId.get(notionId) ?? null;
for (const { note, content, page } of created) {
- const finalContent = rewriteLinks(content, resolveTarget);
+ const finalContent = rewriteCollectionIncludes(rewriteLinks(content, resolveTarget), resolveTarget);
if (finalContent !== page.content) {
note.setContent(finalContent);
}
@@ -352,6 +352,53 @@ export function rewriteLinks(html: string, resolve: (notionId: string) => LinkTa
return changed ? root.toString() : html;
}
+/**
+ * Replaces a full-export inline-database reference with a Trilium include-note embedding the imported
+ * collection. Notion's workspace ("database-wide") export renders an inline database not as a table but as
+ * `<div class="collection-content"><h4 class="collection-title">Name</h4><a href="…<id>.csv">…</a></div>` —
+ * a bare link to the separately-exported CSV. That database is imported as a collection note (built from its
+ * CSV plus rows folder), so resolve it from the id in the `.csv` href and embed it inline, dropping the link.
+ * A partial export inlines the rendered `<table class="collection-content">` instead, which is left untouched.
+ */
+export function rewriteCollectionIncludes(html: string, resolve: (notionId: string) => LinkTarget | null): string {
+ const root = parse(html);
+ let changed = false;
+
+ for (const block of root.querySelectorAll("div.collection-content")) {
+ const notionId = collectionCsvId(block);
+ const target = notionId ? resolve(notionId) : null;
+ if (!target) {
+ continue;
+ }
+
+ block.insertAdjacentHTML("beforebegin", `<section class="include-note" data-note-id="${target.noteId}" data-box-size="medium">&nbsp;</section>`);
+ block.remove();
+ changed = true;
+ }
+
+ return changed ? root.toString() : html;
+}
+
+/** The Notion id of the `.csv` an inline-database `collection-content` block links to, or null. */
+function collectionCsvId(block: HTMLElement): string | null {
+ for (const anchor of block.querySelectorAll("a")) {
+ const href = anchor.getAttribute("href");
+ if (!href) {
+ continue;
+ }
+ let path = href;
+ try {
+ path = decodeURIComponent(href);
+ } catch {
+ // Leave malformed percent-encoding as-is rather than throwing.
+ }
+ if (path.split(/[?#]/)[0].toLowerCase().endsWith(".csv")) {
+ return getNotionId(path) ?? null;
+ }
+ }
+ return null;
+}
+
/**
* Reads a Notion property-row timestamp (created/last-edited) from the page's properties table and
* converts it to Trilium's UTC DB format. Returns undefined when the row, the <time> element or the