Skip to content

Build your personal knowledge base with Trilium Notes

fix(notion): read dates from the time element's attribute (closes #11205)

Elian Doran415d8488e7a4
diff --git a/packages/trilium-core/src/services/import/notion/collection.ts b/packages/trilium-core/src/services/import/notion/collection.ts
index 3ff4c1e743..a86240fee0 100644
--- a/packages/trilium-core/src/services/import/notion/collection.ts
+++ b/packages/trilium-core/src/services/import/notion/collection.ts
@@ -268,13 +268,16 @@ function toLinkProperty(type: string, href: string): Pick<NotionProperty, "value
* 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;
+ const time = cell.querySelector("time");
+ const text = 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);
+ // A `datetime` attribute describes a single instant, so a range reads both of its ends from the text.
+ return [toDateProperty(name, end ? start : notionTimeValue(time) ?? start), toDateProperty(`${name} end`, end)]
+ .filter((p): p is NotionProperty => p !== undefined);
}
/**
@@ -289,8 +292,8 @@ function toDateProperty(name: string, text: string | undefined): NotionProperty
return undefined;
}
- const date = new Date(text);
- if (Number.isNaN(date.getTime())) {
+ const date = parseNotionDate(text);
+ if (!date) {
return undefined;
}
@@ -300,6 +303,26 @@ function toDateProperty(name: string, text: string | undefined): NotionProperty
: { name, value: dayjs(date).format("YYYY-MM-DD"), labelType: "date", multiplicity: "single" };
}
+/**
+ * Reads a `<time>` element's value, preferring the machine-readable `datetime` attribute over the visible
+ * text. Notion renders that text in the export's own locale, and `new Date()` matches a month name only by
+ * its first three letters against the English abbreviations — so a French export keeps "mars" and
+ * "septembre" but drops "juillet" and "décembre".
+ */
+export function notionTimeValue(time: HTMLElement | null): string | undefined {
+ return (time?.getAttribute("datetime") ?? time?.textContent)?.replace(/@/g, "").trim() || undefined;
+}
+
+/**
+ * Parses one Notion date string. A bare `YYYY-MM-DD` is pinned to local midnight, since `new Date()` reads
+ * a date-only ISO string as UTC while the value is rendered back in local time, shifting the day west of
+ * Greenwich. Returns undefined when the string doesn't parse.
+ */
+export function parseNotionDate(text: string): Date | undefined {
+ const date = new Date(/^\d{4}-\d{2}-\d{2}$/.test(text) ? `${text}T00:00` : text);
+ return Number.isNaN(date.getTime()) ? undefined : date;
+}
+
/**
* 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
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.ts
index b4c6414b4d..71e7000cc9 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
@@ -476,6 +476,43 @@ describe("Notion importer — integration", () => {
expect(undated?.utcDateCreated.startsWith("2024-01-02")).toBe(false);
});
+ it("reads a page's timestamps from the <time> element's datetime attribute", async () => {
+ const id = "2c6c5eca1b8b80f7b9eaf4f396b755dc";
+ // A non-English export renders the visible text in its own locale, which `new Date()` parses only
+ // when the month name opens with its English abbreviation ("mars" does, "juillet" doesn't). The
+ // datetime attribute carries the same instant as ISO 8601, so read that instead.
+ const propertyTable =
+ `<table class="properties"><tbody>` +
+ `<tr class="property-row property-row-created_time"><th>Date de création</th><td><time datetime="2020-07-14T10:10">14 juillet 2020 10:10</time></td></tr>` +
+ `<tr class="property-row property-row-last_edited_time"><th>Date de modification</th><td><time datetime="2023-12-30T14:26">30 décembre 2023 14:26</time></td></tr>` +
+ `</tbody></table>`;
+ const importRoot = await importNotion({
+ "Houmous 2c6c5eca1b8b80f7b9eaf4f396b755dc.html":
+ `<html><head><title>Houmous</title></head><body><div id="${id}">${propertyTable}<div class="page-body"><p>x</p></div></div></body></html>`
+ });
+
+ // Notion's timestamps carry no offset, so they are local wall-clock; assert the local columns to
+ // keep the expectation independent of the runner's timezone.
+ const note = importRoot.getChildNotes().find((n) => n.title === "Houmous");
+ expect(note?.dateCreated?.startsWith("2020-07-14 10:10:00")).toBe(true);
+ expect(note?.dateModified?.startsWith("2023-12-30 14:26:00")).toBe(true);
+ });
+
+ it("falls back to the visible text when a timestamp's <time> element carries no datetime attribute", async () => {
+ const id = "2c6c5eca1b8b80f7b9eaf4f396b755dc";
+ const propertyTable =
+ `<table class="properties"><tbody>` +
+ `<tr class="property-row property-row-created_time"><th>Created</th><td><time>January 2, 2024 3:04 AM</time></td></tr>` +
+ `</tbody></table>`;
+ const importRoot = await importNotion({
+ "TextOnly 2c6c5eca1b8b80f7b9eaf4f396b755dc.html":
+ `<html><head><title>TextOnly</title></head><body><div id="${id}">${propertyTable}<div class="page-body"><p>x</p></div></div></body></html>`
+ });
+
+ const note = importRoot.getChildNotes().find((n) => n.title === "TextOnly");
+ expect(note?.dateCreated?.startsWith("2024-01-02 03:04:00")).toBe(true);
+ });
+
it("imports a page's text property as a Trilium label", async () => {
const id = "2c6c5eca1b8b80f7b9eaf4f396b755dc";
// Real Notion markup: the <th> leads with an icon span (no text) before the column name.
@@ -794,6 +831,42 @@ describe("Notion importer — integration", () => {
expect(row?.getOwnedLabelValue("date")).toBe("2026-06-23T19:00");
});
+ it("reads a dated column from the <time> element's datetime attribute", async () => {
+ const dbId = "388c5eca1b8b8078a20fd18330d81306";
+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";
+ // "juin" is unparseable to `new Date()`, so the value has to come from the datetime attribute.
+ 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 datetime="2026-06-23T19:00">23 juin 2026 19:00</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");
+ expect(row?.getOwnedLabelValue("date")).toBe("2026-06-23T19:00");
+ });
+
+ it("keeps a time-less dated column on its own day, whatever the runner's timezone", async () => {
+ const dbId = "388c5eca1b8b8078a20fd18330d81306";
+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";
+ // A bare `YYYY-MM-DD` is UTC to `new Date()` but local to the label, which would shift the day west
+ // of Greenwich.
+ 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 datetime="2026-06-24">24 juin 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");
+ const row = db?.getChildNotes().find((n) => n.title === "Row");
+ expect(row?.getOwnedLabelValue("date")).toBe("2026-06-24");
+ });
+
it("splits a timeless date range into separate start and end date columns", 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.ts
index b7c3aacf5f..d4a4722e51 100644
--- a/packages/trilium-core/src/services/import/notion/importer.ts
+++ b/packages/trilium-core/src/services/import/notion/importer.ts
@@ -29,7 +29,7 @@ import dateUtils from "../../utils/date.js";
import { getZipProvider, type ZipSource } from "../../zip_provider.js";
import mimeService from "../mime.js";
import { toAttributeName } from "../collection_utils.js";
-import { applyDatabaseSchemas, applyOwnedProperties, applyRelationProperties, extractProperties, reconcileDateColumns, resolveDatabaseContainers } from "./collection.js";
+import { applyDatabaseSchemas, applyOwnedProperties, applyRelationProperties, extractProperties, notionTimeValue, parseNotionDate, reconcileDateColumns, resolveDatabaseContainers } from "./collection.js";
import { convertNotionHtml } from "./converter.js";
import type { LinkTarget, ParsedPage } from "./model.js";
import { getNotionId, stripNotionId } from "./notion_id.js";
@@ -416,12 +416,9 @@ export function rewriteCollectionIncludes(html: string, resolve: (notionId: stri
* parsed date is missing/invalid.
*/
function extractDate(root: HTMLElement, rowClass: string): string | undefined {
- const text = root.querySelector(`tr.${rowClass} time`)?.textContent?.replace(/@/g, "").trim();
- if (!text) {
- return undefined;
- }
- const date = new Date(text);
- return Number.isNaN(date.getTime()) ? undefined : dateUtils.utcDateTimeStr(date);
+ const text = notionTimeValue(root.querySelector(`tr.${rowClass} time`));
+ const date = text ? parseNotionDate(text) : undefined;
+ return date ? dateUtils.utcDateTimeStr(date) : undefined;
}
export default { importNotion };