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/notion): treat a database's own page as an empty collection
Elian Doran88be841da4c1
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.tsindex 181b92f826..fd737c20c9 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@@ -82,6 +82,21 @@ 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.+ 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>`,+ "My basic database 388c5eca1b8b8078a20fd18330d81306.csv": "Name,Select column\nFoo,First",+ "My basic database/Foo 388c5eca1b8b80929a78da7c68154bd7.html": pageHtml("Foo", "388c5eca1b8b80929a78da7c68154bd7")+ });++ const database = importRoot.getChildNotes().find((note) => note.title === "My basic database");+ expect(database?.getContent()).toBe("");+ expect(database?.getChildNotes().map((note) => note.title)).toEqual(["Foo"]);+ });+it("saves a referenced file as a role:file attachment and links to it from the content", async () => {// Notion exports a file block as <figure><div class="source"><a href="<file>">name</a></figure>,// with the file bundled in the zip alongside the page.diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.tsindex f51bd941f6..b2cfb8c8f8 100644--- a/packages/trilium-core/src/services/import/notion/importer.ts+++ b/packages/trilium-core/src/services/import/notion/importer.ts@@ -57,13 +57,19 @@ interface ParsedPage {content: string;/** Database properties from the page's Notion properties table, mapped to Trilium labels on import. */properties: NotionProperty[];+ /**+ * Whether this page is a Notion database (collection) rather than an ordinary page. Set by+ * {@link resolveDatabaseContainers}: such a page's body is the rendered collection table — dropped on+ * import, since a collection note is empty — and it is the note its database's rows nest under.+ */+ isDatabase?: boolean;utcDateCreated?: string;utcDateModified?: string;}async function importNotion(taskContext: TaskContext<"importNotes">, fileBuffer: Uint8Array, importRootNote: BNote): Promise<BNote> {const { pages, resources, csvPaths, csvColumnsByFolder } = await parseZip(fileBuffer);- addDatabaseContainers(pages, csvPaths);+ resolveDatabaseContainers(pages, csvPaths);reconcileDateColumns(pages);taskContext.setTotalCount(pages.length);@@ -71,13 +77,30 @@ async function importNotion(taskContext: TaskContext<"importNotes">, fileBuffer:}/**- * A Notion inline/linked database exports as a `<Name> <id>.csv` with no matching `.html` page, while its- * rows are `.html` files in a sibling `<Name>/` folder — so nothing owns that folder and the rows would- * orphan to the import root. Synthesize an (empty) container page for each such database, named after it,- * so its rows nest under it. A database that also has its own page is left to that page (no duplicate).+ * Identifies which pages are Notion databases (collections) and ensures each has an empty container note.+ *+ * A Notion database always exports a `<Name> <id>.csv`; its rows are `.html` files in a sibling `<Name>/`+ * folder. Whether the database also has its own `<Name> <id>.html` page decides how its container is found:+ * - With an own page (its `ownedFolderKey` matches the CSV's), that page's body is the rendered collection+ * table, not real content — so flag it as a database and clear its body, since a collection note is empty.+ * - Without one, nothing owns the rows' folder and they'd orphan to the import root — so synthesize an+ * empty container page named after the database in its place.+ * Either way the database page ends up flagged {@link ParsedPage.isDatabase} and empty, ready to become a+ * Trilium collection, with its rows nested beneath it.*/-function addDatabaseContainers(pages: ParsedPage[], csvPaths: string[]) {+function resolveDatabaseContainers(pages: ParsedPage[], csvPaths: string[]) {+ const databaseKeys = new Set(csvPaths.map((csvPath) => ownedFolderKey(csvPath)));const owned = new Set(pages.map((page) => ownedFolderKey(page.path)));++ // A database with its own page: its body is the collection table, so drop it and flag the page.+ for (const page of pages) {+ if (databaseKeys.has(ownedFolderKey(page.path))) {+ page.isDatabase = true;+ page.content = "";+ }+ }++ // A database with no own page: synthesize an empty container so its rows don't orphan to the root.for (const csvPath of csvPaths) {const key = ownedFolderKey(csvPath);if (owned.has(key)) {@@ -89,7 +112,8 @@ function addDatabaseContainers(pages: ParsedPage[], csvPaths: string[]) {title: stripNotionId(removeExtension(baseName(csvPath))) || "Database",path: csvPath,content: "",- properties: []+ properties: [],+ isDatabase: true});}}