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): map database ID columns to number/text labels
Elian Doranfbffca9c156b
diff --git a/packages/trilium-core/src/services/import/notion/collection.ts b/packages/trilium-core/src/services/import/notion/collection.tsindex 10aafa5e65..ee413b18b0 100644--- a/packages/trilium-core/src/services/import/notion/collection.ts+++ b/packages/trilium-core/src/services/import/notion/collection.ts@@ -67,6 +67,7 @@ export function resolveDatabaseContainers(pages: ParsedPage[], csvPaths: string[* which carries no text) and `<td>` the value. Handled so far:* - `text` / `select` / `status` / `place`: the cell's text → one single-valued property;* - `number`: the cell's text, normalized to a bare number → one single-valued `number` label;+ * - `auto_increment_id`: a bare integer → a `number` label, a prefixed id (e.g. `TASK-1`) → a `text` label;* - `multi_select`: each `<span class="selected-value">` option → one entry of a multi-valued property;* - `url` / `email` / `phone_number`: the anchor's href → one single-valued url-typed property (email gets `mailto:`, phone `tel:`);* - `date`: the `<time>` value → a `date`/`datetime` label; a range adds a separate `<name> end` column;@@ -99,6 +100,14 @@ export function extractProperties(root: HTMLElement): NotionProperty[] {if (value !== undefined) {properties.push({ name, value, labelType: "number", multiplicity: "single" });}+ } else if (type === "auto_increment_id") {+ // Notion's ID is an integer counter, but a configured prefix turns it into an identifier like+ // "TASK-1"; keep a bare integer as a `number` label and a prefixed id verbatim as `text`.+ const value = cell.textContent?.trim();+ if (value) {+ const labelType = /^\d+$/.test(value) ? "number" : "text";+ properties.push({ name, value, labelType, multiplicity: "single" });+ }} else if (type === "multi_select") {for (const option of cell.querySelectorAll("span.selected-value")) {const value = option.textContent?.trim();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 6c19af11f2..08e2dab959 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@@ -504,6 +504,30 @@ describe("Notion importer — integration", () => {expect(second?.getLabelValue("label:A")).toBe("promoted,single,number,alias=A");});+ it("imports an auto-increment ID column as a number label, keeping a prefixed ID as text", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";+ // Notion's ID is an integer counter (a bare number), but a configured prefix turns it into an+ // identifier like "TASK-1" that must stay verbatim. Both render as a property-row-auto_increment_id.+ const rows =+ `<tr class="property-row property-row-auto_increment_id"><th><span class="icon property-icon"><img src="x.svg"/></span>ID</th><td>1</td></tr>` ++ `<tr class="property-row property-row-auto_increment_id"><th>Key</th><td>TASK-1</td></tr>`;+ const props = `<table class="properties"><tbody>${rows}</tbody></table>`;+ const importRoot = await importNotion({+ "DB 388c5eca1b8b8078a20fd18330d81306.html":+ `<html><head><title>DB</title></head><body><div id="${dbId}"><div class="page-body"></div></div></body></html>`,+ "DB/Row 388c5eca1b8b80929a78da7c68154bd7.html":+ `<html><head><title>Row</title></head><body><div id="${rowId}">${props}<div class="page-body"><p>x</p></div></div></body></html>`+ });++ const db = importRoot.getChildNotes().find((n) => n.title === "DB");+ expect(db?.getOwnedLabel("label:ID")?.value).toBe("promoted,single,number,alias=ID");+ expect(db?.getOwnedLabel("label:Key")?.value).toBe("promoted,single,text,alias=Key");+ const row = db?.getChildNotes().find((n) => n.title === "Row");+ expect(row?.getOwnedLabelValue("ID")).toBe("1");+ expect(row?.getOwnedLabelValue("Key")).toBe("TASK-1");+ });+it("imports url, email and phone columns as url-typed labels (mailto:/tel: schemes)", async () => {const dbId = "388c5eca1b8b8078a20fd18330d81306";const rowId = "388c5eca1b8b80929a78da7c68154bd7";