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): import date columns as date/datetime labels
Elian Doranb17470d93c57
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 4de110b22b..f29acf659f 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@@ -477,6 +477,70 @@ describe("Notion importer — integration", () => {expect(row?.getOwnedLabelValue("Phone")).toBe("tel:12345678");});+ it("imports a dated column with a clock time as a datetime label", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";+ const props = `<table class="properties"><tbody><tr class="property-row property-row-date"><th><span class="icon property-icon"><img src="x.svg"/></span>Date</th><td><time>June 23, 2026 7:00 PM</time></td></tr></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:Date")?.value).toBe("promoted,single,datetime,alias=Date");+ const row = db?.getChildNotes().find((n) => n.title === "Row");+ // Local datetime-local format; "7:00 PM" → 19:00 (parse-local + format-local is timezone-independent).+ expect(row?.getOwnedLabelValue("Date")).toBe("2026-06-23T19:00");+ });++ it("splits a timeless date range into separate start and end date columns", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";+ // A date range joins start and end with an arrow (U+2192).+ const props = `<table class="properties"><tbody><tr class="property-row property-row-date"><th><span class="icon property-icon"><img src="x.svg"/></span>Date</th><td><time>June 24, 2026 → June 30, 2026</time></td></tr></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");+ // The range becomes two date columns: the original (start) and a separate "<name> end".+ expect(db?.getOwnedLabel("label:Date")?.value).toBe("promoted,single,date,alias=Date");+ expect(db?.getOwnedLabel("label:Date_end")?.value).toBe("promoted,single,date,alias=Date end");+ const row = db?.getChildNotes().find((n) => n.title === "Row");+ expect(row?.getOwnedLabelValue("Date")).toBe("2026-06-24");+ expect(row?.getOwnedLabelValue("Date_end")).toBe("2026-06-30");+ });++ it("normalizes a date column that mixes dates and date-times to datetime", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const withTimeId = "388c5eca1b8b80929a78da7c68154bd7";+ const noTimeId = "388c5eca1b8b80e5903ef480b0523eb1";+ const dateRow = (time: string) =>+ `<table class="properties"><tbody><tr class="property-row property-row-date"><th><span class="icon property-icon"><img src="x.svg"/></span>Date</th><td><time>${time}</time></td></tr></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/WithTime 388c5eca1b8b80929a78da7c68154bd7.html":+ `<html><head><title>WithTime</title></head><body><div id="${withTimeId}">${dateRow("June 23, 2026 7:00 PM")}<div class="page-body"><p>x</p></div></div></body></html>`,+ "DB/NoTime 388c5eca1b8b80e5903ef480b0523eb1.html":+ `<html><head><title>NoTime</title></head><body><div id="${noTimeId}">${dateRow("June 24, 2026")}<div class="page-body"><p>y</p></div></div></body></html>`+ });++ const db = importRoot.getChildNotes().find((n) => n.title === "DB");+ // One row uses a time, so the whole column becomes datetime.+ expect(db?.getOwnedLabel("label:Date")?.value).toBe("promoted,single,datetime,alias=Date");+ const withTime = db?.getChildNotes().find((n) => n.title === "WithTime");+ const noTime = db?.getChildNotes().find((n) => n.title === "NoTime");+ expect(withTime?.getOwnedLabelValue("Date")).toBe("2026-06-23T19:00");+ // The time-less value is normalized to midnight so it stays valid for the datetime-local input.+ expect(noTime?.getOwnedLabelValue("Date")).toBe("2026-06-24T00:00");+ });+it("neutralizes commas in a column name so the alias can't corrupt the definition", async () => {const dbId = "388c5eca1b8b8078a20fd18330d81306";const rowId = "388c5eca1b8b80929a78da7c68154bd7";diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.tsindex a51e144aa1..65ecee4df7 100644--- a/packages/trilium-core/src/services/import/notion/importer.ts+++ b/packages/trilium-core/src/services/import/notion/importer.ts@@ -12,7 +12,7 @@* this service just builds the tree and returns its root note, like the zip/enex importers.*/-import type { LabelType, Multiplicity } from "@triliumnext/commons";+import { dayjs, type LabelType, type Multiplicity } from "@triliumnext/commons";import { t } from "i18next";import { HTMLElement, parse } from "node-html-parser";@@ -57,6 +57,7 @@ interface ParsedPage {async function importNotion(taskContext: TaskContext<"importNotes">, fileBuffer: Uint8Array, importRootNote: BNote): Promise<BNote> {const { pages, resources, csvPaths } = await parseZip(fileBuffer);addDatabaseContainers(pages, csvPaths);+ reconcileDateColumns(pages);taskContext.setTotalCount(pages.length);return createNotes(importRootNote, pages, resources, taskContext);@@ -484,7 +485,8 @@ export function firstChildNotionId(body: HTMLElement | null): string | undefined* which carries no text) and `<td>` the value. Handled so far:* - `text` / `select` / `status`: the cell's text → one single-valued property;* - `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:`).+ * - `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.* The importer turns each `{ name, value }` into a Trilium label; blank names/values are skipped (Notion* sometimes emits an empty cell, e.g. an unset multi-select, which should contribute no label). Other types* (dates handled separately by extractDate) fall through untouched.@@ -520,6 +522,8 @@ function extractProperties(root: HTMLElement): NotionProperty[] {if (href) {properties.push({ name, value: toUrlValue(type, href), labelType: "url", multiplicity: "single" });}+ } else if (type === "date") {+ properties.push(...parseDateProperties(name, cell));}}return properties;@@ -536,6 +540,80 @@ function toUrlValue(type: string, href: string): string {return href;}+/**+ * Parses a Notion date column. The value is one `<time>`; a date range joins its start and end with an+ * arrow, which becomes two columns: the original (start) and a separate `<name> end` (end).+ */+function parseDateProperties(name: string, cell: HTMLElement): NotionProperty[] {+ const text = cell.querySelector("time")?.textContent;+ if (!text) {+ return [];+ }++ const [start, end] = text.split("→").map((part) => part.trim());+ return [toDateProperty(name, start), toDateProperty(`${name} end`, end)].filter((p): p is NotionProperty => p !== undefined);+}++/**+ * Turns one date string into a property. A clock time is present only when the column's "include time"+ * option is on, which selects a `datetime` label (local `YYYY-MM-DDTHH:mm`) over a plain `date`+ * (`YYYY-MM-DD`) — the formats the promoted date / datetime-local inputs round-trip. dayjs formats in local+ * time, matching the wall-clock of Notion's timezone-less string. (Mixed date/date-time columns are then+ * reconciled to a single type by {@link reconcileDateColumns}.)+ */+function toDateProperty(name: string, text: string | undefined): NotionProperty | undefined {+ if (!text) {+ return undefined;+ }++ const date = new Date(text);+ if (Number.isNaN(date.getTime())) {+ return undefined;+ }++ const hasTime = /\d{1,2}:\d{2}/.test(text);+ return hasTime+ ? { name, value: dayjs(date).format("YYYY-MM-DD[T]HH:mm"), labelType: "datetime", multiplicity: "single" }+ : { name, value: dayjs(date).format("YYYY-MM-DD"), labelType: "date", multiplicity: "single" };+}++/**+ * Notion's "include time" is toggled per date value, so one date column can mix dates and date-times. A+ * Trilium promoted attribute has a single type, so resolve each date column (scoped to its database, keyed+ * by sanitized name) to `datetime` if *any* of its values carries a time, then normalize every value to+ * that type — a time-less value in a datetime column gets midnight (`T00:00`) so it stays valid for the+ * `datetime-local` input. Mutates the parsed pages in place before notes (and their labels) are created.+ */+function reconcileDateColumns(pages: ParsedPage[]) {+ const columnsWithTime = new Set<string>();+ for (const page of pages) {+ for (const property of page.properties) {+ if (property.labelType === "datetime") {+ columnsWithTime.add(dateColumnKey(page.path, property.name));+ }+ }+ }++ for (const page of pages) {+ for (const property of page.properties) {+ const isDateColumn = property.labelType === "date" || property.labelType === "datetime";+ if (isDateColumn && columnsWithTime.has(dateColumnKey(page.path, property.name))) {+ property.labelType = "datetime";+ if (!property.value.includes("T")) {+ property.value = `${property.value}T00:00`;+ }+ }+ }+ }+}++/** Identifies a date column within its database: the row's container folder plus the sanitized column name. */+function dateColumnKey(path: string, name: string): string {+ // A space separates the parts unambiguously: a sanitized attribute name never contains a space, so the+ // text after the last space is always the column name and everything before it is the container folder.+ return `${parentFolderKey(path)} ${sanitizeAttributeName(name)}`;+}+/*** Reads a Notion property-row timestamp (created/last-edited) from the page's properties table and* converts it to Trilium's UTC DB format. Returns undefined when the row, the <time> element or the