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 number columns to number labels
Elian Doranb6d9511cca33
diff --git a/packages/trilium-core/src/services/import/notion/collection.ts b/packages/trilium-core/src/services/import/notion/collection.tsindex 7f79d3ad16..d45adaad72 100644--- a/packages/trilium-core/src/services/import/notion/collection.ts+++ b/packages/trilium-core/src/services/import/notion/collection.ts@@ -66,6 +66,7 @@ export function resolveDatabaseContainers(pages: ParsedPage[], csvPaths: string[* `<tr class="property-row property-row-<type>">` whose `<th>` holds the column name (after an icon span,* 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;* - `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;@@ -93,6 +94,11 @@ export function extractProperties(root: HTMLElement): NotionProperty[] {if (value) {properties.push({ name, value, labelType: "text", multiplicity: "single" });}+ } else if (type === "number") {+ const value = toNumberValue(cell.textContent);+ if (value !== undefined) {+ properties.push({ name, value, labelType: "number", multiplicity: "single" });+ }} else if (type === "multi_select") {for (const option of cell.querySelectorAll("span.selected-value")) {const value = option.textContent?.trim();@@ -147,6 +153,21 @@ export function extractProperties(root: HTMLElement): NotionProperty[] {return properties;}+/**+ * Normalizes a Notion number cell to a bare numeric string Trilium's `number` input accepts. Notion renders+ * the *formatted* value (e.g. `1,200`, `$12.00`, `12%`), so strip everything but digits, a decimal point and a+ * sign; the cleaned value is used only when it parses as a finite number, otherwise the trimmed original is+ * kept so an unexpectedly non-numeric cell still imports rather than vanishing. A blank cell yields nothing.+ */+function toNumberValue(text: string | null | undefined): string | undefined {+ const trimmed = text?.trim();+ if (!trimmed) {+ return undefined;+ }+ const normalized = trimmed.replace(/[^\d.-]/g, "");+ return normalized !== "" && Number.isFinite(Number(normalized)) ? normalized : trimmed;+}+/** Gives an email/phone href a clickable scheme (`mailto:`/`tel:`); a plain url href is returned as-is. */function toUrlValue(type: string, href: string): string {if (type === "email") {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 af0d5f6ecf..bb6a17dbe5 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@@ -468,6 +468,42 @@ describe("Notion importer — integration", () => {expect(row?.getOwnedLabelValue("Status_column")).toBe("Another in progress");});+ it("imports number columns as number-typed labels, normalizing formatted values and inheriting the schema", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const firstId = "388c5eca1b8b80929a78da7c68154bd7";+ const secondId = "388c5eca1b8b80deb7f9ede99b0b2036";+ // A number cell holds the *formatted* display (commas, currency, percent), which the importer+ // normalizes to a bare number. Notion drops the property row entirely when a number is empty, so+ // "Second" carries no number rows at all — yet still inherits the column definitions from the container.+ const numberRows =+ `<tr class="property-row property-row-number"><th><span class="icon property-icon"><img src="x.svg"/></span>A</th><td>12</td></tr>` ++ `<tr class="property-row property-row-number"><th>B</th><td>$1,200.50</td></tr>` ++ `<tr class="property-row property-row-number"><th>C</th><td>25%</td></tr>`;+ const props = `<table class="properties"><tbody>${numberRows}</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/First 388c5eca1b8b80929a78da7c68154bd7.html":+ `<html><head><title>First</title></head><body><div id="${firstId}">${props}<div class="page-body"><p>x</p></div></div></body></html>`,+ "DB/Second 388c5eca1b8b80deb7f9ede99b0b2036.html":+ `<html><head><title>Second</title></head><body><div id="${secondId}"><div class="page-body"><p>y</p></div></div></body></html>`+ });++ const db = importRoot.getChildNotes().find((n) => n.title === "DB");+ expect(db?.getOwnedLabel("label:A")?.value).toBe("promoted,single,number,alias=A");+ expect(db?.getOwnedLabel("label:B")?.value).toBe("promoted,single,number,alias=B");++ const first = db?.getChildNotes().find((n) => n.title === "First");+ expect(first?.getOwnedLabelValue("A")).toBe("12");+ expect(first?.getOwnedLabelValue("B")).toBe("1200.50"); // "$1,200.50" → bare number+ expect(first?.getOwnedLabelValue("C")).toBe("25"); // "25%" → bare number++ // "Second" has no number values of its own, but inherits the column definitions from the container.+ const second = db?.getChildNotes().find((n) => n.title === "Second");+ expect(second?.getOwnedLabelValue("A")).toBeNull();+ expect(second?.getLabelValue("label:A")).toBe("promoted,single,number,alias=A");+ });+it("imports url, email and phone columns as url-typed labels (mailto:/tel: schemes)", async () => {const dbId = "388c5eca1b8b8078a20fd18330d81306";const rowId = "388c5eca1b8b80929a78da7c68154bd7";