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 formula columns by rendered shape
Elian Doranb2ce8336dfe6
diff --git a/packages/trilium-core/src/services/import/notion/collection.ts b/packages/trilium-core/src/services/import/notion/collection.tsindex ee413b18b0..b7d3dbdebf 100644--- a/packages/trilium-core/src/services/import/notion/collection.ts+++ b/packages/trilium-core/src/services/import/notion/collection.ts@@ -72,6 +72,7 @@ export function resolveDatabaseContainers(pages: ParsedPage[], csvPaths: string[* - `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;* - `checkbox`: `checkbox-on`/`checkbox-off` → a `true`/`false` boolean label;+ * - `formula`: inferred from the rendered cell shape — checkbox → boolean, else numeric text → number, else text;* - `person`: each `<span class="user">` name (its avatar stripped) → an entry of a multi-valued property;* - `relation`: each linked page's `<a>` href → a multi-valued relation, resolved to a note in the second pass;* - `file`: each `<a>` href → a `role:"file"` attachment on the note (no promoted definition — it's content).@@ -125,11 +126,12 @@ export function extractProperties(root: HTMLElement): NotionProperty[] {} else if (type === "date") {properties.push(...parseDateProperties(name, cell));} else if (type === "checkbox") {- const checkbox = cell.querySelector("div.checkbox");- if (checkbox) {- const value = checkbox.classList.contains("checkbox-on") ? "true" : "false";- properties.push({ name, value, labelType: "boolean", multiplicity: "single" });+ const property = toBooleanProperty(name, cell);+ if (property) {+ properties.push(property);}+ } else if (type === "formula") {+ properties.push(...extractFormula(name, cell));} else if (type === "person") {// A person column can list several users; each is a `<span class="user">` whose leading avatar// (`.user-icon`, e.g. an initial) would otherwise bleed into the name, so drop it first.@@ -177,6 +179,53 @@ function toNumberValue(text: string | null | undefined): string | undefined {return normalized !== "" && Number.isFinite(Number(normalized)) ? normalized : trimmed;}+/** Reads a Notion checkbox cell (`<div class="checkbox checkbox-on|off">`) as a `true`/`false` boolean label. */+function toBooleanProperty(name: string, cell: HTMLElement): NotionProperty | undefined {+ const checkbox = cell.querySelector("div.checkbox");+ if (!checkbox) {+ return undefined;+ }+ const value = checkbox.classList.contains("checkbox-on") ? "true" : "false";+ return { name, value, labelType: "boolean", multiplicity: "single" };+}++/**+ * Reads a Notion formula column. A formula's result has no type signal on the row (the class is always+ * `property-row-formula`), so the Trilium type is inferred from the cell's shape — the only evidence the+ * export gives. A boolean result renders as a checkbox widget (→ a `boolean` label); every other result is+ * plain text, which becomes a `number` label when it's purely numeric and a `text` label otherwise. Notion+ * renders a *date* formula as plain text too (e.g. `June 24, 2026`, with no `<time>` wrapper, unlike a native+ * date column), so it lands in the text case — preserved verbatim, not typed as a Trilium date. Every value is+ * a snapshot: Trilium has no formula engine, so it reflects the export and won't recompute.+ */+function extractFormula(name: string, cell: HTMLElement): NotionProperty[] {+ const boolean = toBooleanProperty(name, cell);+ if (boolean) {+ return [boolean];+ }+ const text = cell.textContent?.trim();+ if (!text) {+ return [];+ }+ const number = numericFormulaValue(text);+ return [number !== undefined+ ? { name, value: number, labelType: "number", multiplicity: "single" }+ : { name, value: text, labelType: "text", multiplicity: "single" }];+}++/**+ * Returns a formula's text result as a bare number, or `undefined` when it isn't numeric. A real number has no+ * letters or date/time separators (so `June 24, 2026` and `06/24/2026` stay text); grouping separators and a+ * currency/percent symbol are stripped before the value is required to be a plain number.+ */+function numericFormulaValue(text: string): string | undefined {+ if (/[\p{L}\/:]/u.test(text)) {+ return undefined;+ }+ const core = text.replace(/[^\d.+-]/g, "");+ return /^[+-]?(\d+\.?\d*|\.\d+)$/.test(core) ? core : undefined;+}+/** 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 08e2dab959..5da2cf75bb 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@@ -528,6 +528,33 @@ describe("Notion importer — integration", () => {expect(row?.getOwnedLabelValue("Key")).toBe("TASK-1");});+ it("imports a formula column by its rendered shape: number, boolean (checkbox), or text (incl. a date formula)", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";+ // A formula has no type signal on the row — Notion renders the computed value with the matching widget.+ // A number stays bare text, a boolean is a checkbox, and a date renders as plain text (no <time> wrapper).+ const rows =+ `<tr class="property-row property-row-formula"><th><span class="icon property-icon"><img src="x.svg"/></span>Number formula</th><td>15</td></tr>` ++ `<tr class="property-row property-row-formula"><th>Bool formula</th><td><div class="checkbox checkbox-on"></div></td></tr>` ++ `<tr class="property-row property-row-formula"><th>Date formula</th><td>June 24, 2026</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:Number_formula")?.value).toBe("promoted,single,number,alias=Number formula");+ expect(db?.getOwnedLabel("label:Bool_formula")?.value).toBe("promoted,single,boolean,alias=Bool formula");+ expect(db?.getOwnedLabel("label:Date_formula")?.value).toBe("promoted,single,text,alias=Date formula");+ const row = db?.getChildNotes().find((n) => n.title === "Row");+ expect(row?.getOwnedLabelValue("Number_formula")).toBe("15");+ expect(row?.getOwnedLabelValue("Bool_formula")).toBe("true");+ expect(row?.getOwnedLabelValue("Date_formula")).toBe("June 24, 2026");+ });+it("imports url, email and phone columns as url-typed labels (mailto:/tel: schemes)", async () => {const dbId = "388c5eca1b8b8078a20fd18330d81306";const rowId = "388c5eca1b8b80929a78da7c68154bd7";