Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/notion): import databases as table collections

Elian Doran98d5b3c8cd7a
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 20f4babca7..af0d5f6ecf 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
@@ -67,6 +67,9 @@ describe("Notion importer — integration", () => {
const readingList = importRoot.getChildNotes().find((note) => note.title === "Reading List");
const media = readingList?.getChildNotes().find((note) => note.title === "Media");
+ // The synthesized container is itself a table collection, like a database that has its own page.
+ expect(media?.type).toBe("book");
+ expect(media?.getLabelValue("viewType")).toBe("table");
expect(media?.getChildNotes().map((note) => note.title)).toEqual(["Bon Appetit"]);
});
@@ -82,9 +85,10 @@ describe("Notion importer — integration", () => {
expect(fortyFive[0].getChildNotes().map((note) => note.title)).toEqual(["Brown fox"]);
});
- it("imports a database's own page as an empty container, dropping its rendered collection table", async () => {
- // A Notion database page's body is the rendered collection table, not real content — a collection
- // note is empty (its data lives in the row notes), so the table must not be carried over as content.
+ it("imports a database's own page as an empty table collection, dropping its rendered collection table", async () => {
+ // A Notion database page's body is the rendered collection table, not real content — a Trilium
+ // collection note is empty (its data lives in the row notes), so the table must not become content.
+ // The database becomes a `book` with #viewType=table, the only view a Notion export preserves.
const collectionBody = `<div class="collection-content-wrapper"><table class="collection-content"><thead><tr><th>Name</th><th>Select column</th></tr></thead><tbody><tr id="388c5eca1b8b80929a78da7c68154bd7"><td class="cell-title"><a href="My%20basic%20database/Foo%20388c5eca1b8b80929a78da7c68154bd7.html">Foo</a></td><td><span class="selected-value">First</span></td></tr></tbody></table></div>`;
const importRoot = await importNotion({
"My basic database 388c5eca1b8b8078a20fd18330d81306.html": `<html><head><title>My basic database</title></head><body><div id="388c5eca1b8b8078a20fd18330d81306" class="page"><div class="page-body">${collectionBody}</div></div></body></html>`,
@@ -93,6 +97,8 @@ describe("Notion importer — integration", () => {
});
const database = importRoot.getChildNotes().find((note) => note.title === "My basic database");
+ expect(database?.type).toBe("book");
+ expect(database?.getLabelValue("viewType")).toBe("table");
expect(database?.getContent()).toBe("");
expect(database?.getChildNotes().map((note) => note.title)).toEqual(["Foo"]);
});
diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.ts
index 02268ce7ad..2b31bcd7fc 100644
--- a/packages/trilium-core/src/services/import/notion/importer.ts
+++ b/packages/trilium-core/src/services/import/notion/importer.ts
@@ -149,16 +149,23 @@ function createNotes(importRootNote: BNote, pages: ParsedPage[], resources: Map<
for (const page of ordered) {
const targetParentId = noteByFolder.get(parentFolderKey(page.path))?.noteId ?? rootNote.noteId;
+ // A Notion database imports as a Trilium table collection: an (empty) `book` whose rows are its
+ // children and whose columns are the promoted-attribute definitions added below. `table` is the
+ // only view the export preserves — every Notion database exports as a rendered table regardless of
+ // its actual view (board/calendar/…), so that information is gone and table is the faithful mapping.
const { note } = noteService.createNewNote({
parentNoteId: targetParentId,
title: page.title,
content: page.content,
- type: "text",
- mime: "text/html",
+ type: page.isDatabase ? "book" : "text",
+ mime: page.isDatabase ? "" : "text/html",
isProtected
});
noteByFolder.set(ownedFolderKey(page.path), note);
targetByPageId.set(page.id, { noteId: note.noteId, title: page.title });
+ if (page.isDatabase) {
+ note.addLabel("viewType", "table");
+ }
// Carry the page's own database property values over (file columns become attachments, the rest
// labels); relations are deferred to the second pass, once every target note exists.