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
fix(import): group Notion database rows under a container note
Elian Doranabd6a642cfb3
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 37867debf0..27385ead9e 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@@ -56,6 +56,32 @@ describe("Notion importer — integration", () => {expect(examples?.getChildNotes().map((note) => note.title).sort()).toEqual(["Hello world", "Quick Note"]);});+ 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.+ const importRoot = await importNotion({+ "Reading List 08d361c59a9940c2a9d7237a4e6cd09a.html": pageHtml("Reading List", "08d361c59a9940c2a9d7237a4e6cd09a"),+ "Reading List/Media 0a556b01dd9c4b70bbba51bf2ee0409b.csv": "Title,Author\nx,y",+ "Reading List/Media/Bon Appetit 4f195d8c55fb44f4b94a063e643b0297.html": pageHtml("Bon Appetit", "4f195d8c55fb44f4b94a063e643b0297")+ });++ const readingList = importRoot.getChildNotes().find((note) => note.title === "Reading List");+ const media = readingList?.getChildNotes().find((note) => note.title === "Media");+ expect(media?.getChildNotes().map((note) => note.title)).toEqual(["Bon Appetit"]);+ });++ it("does not create a duplicate container when the database also has its own page", async () => {+ const importRoot = await importNotion({+ "45 cff419ddfc69457aad14bd0381cf0172.html": pageHtml("45", "cff419ddfc69457aad14bd0381cf0172"),+ "45 cff419ddfc69457aad14bd0381cf0172.csv": "a,b\n1,2",+ "45/Brown fox 4731219279d04d78aca408c0a60c4263.html": pageHtml("Brown fox", "4731219279d04d78aca408c0a60c4263")+ });++ const fortyFive = importRoot.getChildNotes().filter((note) => note.title === "45");+ expect(fortyFive.length).toBe(1);+ expect(fortyFive[0].getChildNotes().map((note) => note.title)).toEqual(["Brown fox"]);+ });+it("recurses into a root-level nested export zip (the part Notion makes you extract)", async () => {const innerZip = await createZipBuffer({ "Inner page 386c5eca1b8b80439520cad27a0d2749.html": pageHtml("Inner page") });const importRoot = await importNotion({ "Export-Part-1.zip": innerZip });diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.tsindex 7ae409fdd1..e73517e6f8 100644--- a/packages/trilium-core/src/services/import/notion/importer.ts+++ b/packages/trilium-core/src/services/import/notion/importer.ts@@ -39,12 +39,36 @@ interface ParsedPage {}async function importNotion(taskContext: TaskContext<"importNotes">, fileBuffer: Uint8Array, importRootNote: BNote): Promise<BNote> {- const { pages, resources } = await parseZip(fileBuffer);+ const { pages, resources, csvPaths } = await parseZip(fileBuffer);+ addDatabaseContainers(pages, csvPaths);taskContext.setTotalCount(pages.length);return createNotes(importRootNote, pages, resources, taskContext);}+/**+ * 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).+ */+function addDatabaseContainers(pages: ParsedPage[], csvPaths: string[]) {+ const owned = new Set(pages.map((page) => ownedFolderKey(page.path)));+ for (const csvPath of csvPaths) {+ const key = ownedFolderKey(csvPath);+ if (owned.has(key)) {+ continue;+ }+ owned.add(key);+ pages.push({+ id: getNotionId(baseName(csvPath)) ?? "",+ title: stripNotionId(removeExtension(baseName(csvPath))) || "Database",+ path: csvPath,+ content: ""+ });+ }+}+/*** Reads the zip: HTML entries become parsed pages; every other file (images, attachments) is kept by its* normalized path so page content can later reference it. The `index.html` summary is skipped.@@ -55,10 +79,11 @@ async function importNotion(taskContext: TaskContext<"importNotes">, fileBuffer:*/const MAX_NESTED_ZIP_DEPTH = 2;-async function parseZip(fileBuffer: Uint8Array): Promise<{ pages: ParsedPage[]; resources: Map<string, Uint8Array> }> {+async function parseZip(fileBuffer: Uint8Array): Promise<{ pages: ParsedPage[]; resources: Map<string, Uint8Array>; csvPaths: string[] }> {const provider = getZipProvider();const pages: ParsedPage[] = [];const resources = new Map<string, Uint8Array>();+ const csvPaths: string[] = [];const readArchive = async (buffer: Uint8Array, depth: number): Promise<void> => {const filenameEncoding = await provider.detectFilenameEncoding(buffer);@@ -79,6 +104,9 @@ async function parseZip(fileBuffer: Uint8Array): Promise<{ pages: ParsedPage[];if (parsed) {pages.push(parsed);}+ } else if (path.toLowerCase().endsWith(".csv")) {+ // Notion databases export as CSVs; we only need the path to reconstruct the hierarchy.+ csvPaths.push(path);} else {resources.set(normalizePath(path), await readContent());}@@ -87,7 +115,7 @@ async function parseZip(fileBuffer: Uint8Array): Promise<{ pages: ParsedPage[];await readArchive(fileBuffer, 0);- return { pages, resources };+ return { pages, resources, csvPaths };}function parsePage(path: string, html: string): ParsedPage | null {