Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): import email and phone columns as their typed labels

Elian Doran07fd5bd3fdd1
diff --git a/packages/trilium-core/src/services/import/anytype/collection.spec.ts b/packages/trilium-core/src/services/import/anytype/collection.spec.ts
index cc2a35529d..dc40437622 100644
--- a/packages/trilium-core/src/services/import/anytype/collection.spec.ts
+++ b/packages/trilium-core/src/services/import/anytype/collection.spec.ts
@@ -71,7 +71,7 @@ describe("collection properties", () => {
]);
describe("parseObject — property values", () => {
- it("maps supported property values to labels, schemes email/phone with mailto:/tel:", () => {
+ it("maps supported property values to labels, keeping email/phone as bare addresses", () => {
const details = {
id: "obj",
name: "Row",
@@ -93,8 +93,8 @@ describe("collection properties", () => {
{ name: "dateTime", value: localDate(1782461208, true) },
{ name: "checkbox", value: "true" },
{ name: "url", value: "https://triliumnotes.org" },
- { name: "email", value: "mailto:[email protected]" },
- { name: "phone", value: "tel:12345" }
+ { name: "email", value: "[email protected]" },
+ { name: "phone", value: "12345" }
]);
});
@@ -131,16 +131,16 @@ describe("collection properties", () => {
expect(result.properties).toEqual([]);
});
- it("ignores system relations (non-hex keys), unset values and an existing scheme", () => {
+ it("ignores system relations (non-hex keys) and unset values, and strips a scheme an email carries", () => {
const details = {
id: "obj",
name: "Named", // the title, not a property
description: "a system longtext", // system relation, non-hex key → not a property
"6a3e335dcafa6953a4661c74": "", // unset url → skipped
- "6a3e336dcafa6953a4661c75": "mailto:[email protected]" // keeps its existing scheme
+ "6a3e336dcafa6953a4661c75": "mailto:[email protected]" // scheme stripped to the bare address
};
const result = parseObject(snapshot([{ id: "obj", childrenIds: [] }], details), undefined, rels);
- expect(result.properties).toEqual([{ name: "email", value: "mailto:[email protected]" }]);
+ expect(result.properties).toEqual([{ name: "email", value: "[email protected]" }]);
});
it("returns no properties when no relation map is supplied", () => {
@@ -353,7 +353,7 @@ describe("collection properties", () => {
const m2 = { id: "m2", "6a3e336dcafa6953a4661c75": "[email protected]", "6a3e335dcafa6953a4661c74": "https://y" }; // Email + URL (already seen)
expect(synthesizeColumns([m1, m2], rels)).toEqual([
{ name: "url", labelType: "url", alias: "URL", multiplicity: "single" },
- { name: "email", labelType: "url", alias: "Email", multiplicity: "single" }
+ { name: "email", labelType: "email", alias: "Email", multiplicity: "single" }
]);
});
});
diff --git a/packages/trilium-core/src/services/import/anytype/collection.ts b/packages/trilium-core/src/services/import/anytype/collection.ts
index fdbb1e31e0..9612a89916 100644
--- a/packages/trilium-core/src/services/import/anytype/collection.ts
+++ b/packages/trilium-core/src/services/import/anytype/collection.ts
@@ -18,7 +18,7 @@ import { dayjs } from "@triliumnext/commons";
import type BNote from "../../../becca/entities/bnote.js";
import noteService from "../../notes.js";
import { basename } from "../../utils/path.js";
-import { applyUrlScheme, attachmentReferenceLink, buildPromotedDefinition, saveFileAttachment, toAttributeName } from "../collection_utils.js";
+import { attachmentReferenceLink, buildPromotedDefinition, saveFileAttachment, stripUrlScheme, toAttributeName } from "../collection_utils.js";
import mimeService from "../mime.js";
import type { AnytypeBlock, AnytypeDetails, AnytypeSnapshot, CollectionViewType, FileObjectInfo, Multiplicity, ParsedCollection, ParsedColumn, ParsedObject, ParsedProperty, PropertyLabelType, RelationInfo } from "./model.js";
@@ -76,9 +76,9 @@ const FILE_FORMAT = 5;
/**
* Maps an Anytype relation to how its values import, or undefined for a format not yet supported (file, …).
- * Date and date-time share format 4, told apart by the relation's `includeTime` flag; email and phone reuse
- * the `url` type with a `mailto:`/`tel:` scheme. Select and multi-select are `optionBacked` (their value is
- * a list of option ids resolved to names) and follow the Notion importer's single/multi text mapping.
+ * Date and date-time share format 4, told apart by the relation's `includeTime` flag. Select and
+ * multi-select are `optionBacked` (their value is a list of option ids resolved to names) and follow the
+ * Notion importer's single/multi text mapping.
*/
function propertyMapping(info: RelationInfo): PropertyMapping | undefined {
switch (info.format) {
@@ -96,9 +96,9 @@ function propertyMapping(info: RelationInfo): PropertyMapping | undefined {
case 7: // url
return { labelType: "url", multiplicity: "single" };
case 8: // email
- return { labelType: "url", scheme: "mailto:", multiplicity: "single" };
+ return { labelType: "email", multiplicity: "single" };
case 9: // phone
- return { labelType: "url", scheme: "tel:", multiplicity: "single" };
+ return { labelType: "phone", multiplicity: "single" };
case 11: // tag / multi-select
return { labelType: "text", multiplicity: "multi", optionBacked: true };
default:
@@ -114,10 +114,9 @@ function isCustomRelationKey(key: string): boolean {
/**
* Reads an object's custom property values as `{ attributeName, value }` pairs. Only the supported formats
- * (see {@link propertyMapping}) of user-defined relations are taken; unset values are dropped, and email /
- * phone values gain a `mailto:`/`tel:` scheme so they stay clickable as url labels. A select / multi-select
- * value is a list of option ids, each resolved to its display name via `options` (a multi-select yields one
- * label per option; an unresolvable option is skipped).
+ * (see {@link propertyMapping}) of user-defined relations are taken; unset values are dropped. A select /
+ * multi-select value is a list of option ids, each resolved to its display name via `options` (a
+ * multi-select yields one label per option; an unresolvable option is skipped).
*/
export function parseProperties(details: AnytypeDetails, relations: Map<string, RelationInfo>, options: Map<string, string>): ParsedProperty[] {
const properties: ParsedProperty[] = [];
@@ -139,7 +138,7 @@ export function parseProperties(details: AnytypeDetails, relations: Map<string,
}
}
} else {
- const value = formatPropertyValue(raw, mapping.labelType, mapping.scheme);
+ const value = formatPropertyValue(raw, mapping.labelType);
if (value !== undefined) {
properties.push({ name, value });
}
@@ -255,9 +254,9 @@ function columnsFromKeys(keys: (string | undefined)[], relations: Map<string, Re
* becomes `"true"`/`"false"`; a date (Anytype stores an epoch in *seconds*) becomes a local `YYYY-MM-DD` or
* `YYYY-MM-DDTHH:mm` string the promoted date/datetime inputs round-trip; a number is stringified (a
* non-numeric value rejected); a text/url value is trimmed-checked for emptiness; an email/phone value is
- * given its `mailto:`/`tel:` scheme unless it already carries one.
+ * stripped of a `mailto:`/`tel:` scheme it may carry, since the typed inputs hold the bare address.
*/
-function formatPropertyValue(raw: unknown, labelType: PropertyLabelType, scheme: string | undefined): string | undefined {
+function formatPropertyValue(raw: unknown, labelType: PropertyLabelType): string | undefined {
if (raw === null || raw === undefined) {
return undefined;
}
@@ -285,7 +284,13 @@ function formatPropertyValue(raw: unknown, labelType: PropertyLabelType, scheme:
if (!value.trim()) {
return undefined;
}
- return scheme ? applyUrlScheme(value, scheme) : value;
+ if (labelType === "email") {
+ return stripUrlScheme(value, "mailto:");
+ }
+ if (labelType === "phone") {
+ return stripUrlScheme(value, "tel:");
+ }
+ return value;
}
/**
@@ -362,12 +367,11 @@ export function applyFiles(note: BNote, page: ParsedObject, fileObjects: Map<str
}
}
-/** How a relation's values import: its Trilium label type, value count, an optional clickable scheme
- * (email/phone), and whether the value is option-backed (a select / multi-select, resolved via the options
- * map). Internal to {@link propertyMapping}; the shared `Parsed*` shapes live in {@link ./model.js}. */
+/** How a relation's values import: its Trilium label type, value count, and whether the value is
+ * option-backed (a select / multi-select, resolved via the options map). Internal to
+ * {@link propertyMapping}; the shared `Parsed*` shapes live in {@link ./model.js}. */
interface PropertyMapping {
labelType: PropertyLabelType;
multiplicity: Multiplicity;
- scheme?: string;
optionBacked?: boolean;
}
diff --git a/packages/trilium-core/src/services/import/anytype/model.ts b/packages/trilium-core/src/services/import/anytype/model.ts
index d4baab2326..9e1ef8982c 100644
--- a/packages/trilium-core/src/services/import/anytype/model.ts
+++ b/packages/trilium-core/src/services/import/anytype/model.ts
@@ -217,8 +217,8 @@ export interface FileObjectInfo {
source: string;
}
-/** The Trilium label types a supported Anytype property maps to (email/phone reuse `url`). */
-export type PropertyLabelType = "text" | "number" | "url" | "date" | "datetime" | "boolean";
+/** The Trilium label types a supported Anytype property maps to. */
+export type PropertyLabelType = "text" | "number" | "url" | "email" | "phone" | "date" | "datetime" | "boolean";
/** Whether a property holds a single value or several (a multi-select). */
export type Multiplicity = "single" | "multi";
diff --git a/packages/trilium-core/src/services/import/collection_utils.spec.ts b/packages/trilium-core/src/services/import/collection_utils.spec.ts
index e32eaea03a..b5d7e3eaff 100644
--- a/packages/trilium-core/src/services/import/collection_utils.spec.ts
+++ b/packages/trilium-core/src/services/import/collection_utils.spec.ts
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from "vitest";
import type BNote from "../../becca/entities/bnote.js";
-import { buildPromotedDefinition, saveFileAttachment, toAttributeName } from "./collection_utils.js";
+import { buildPromotedDefinition, saveFileAttachment, stripUrlScheme, toAttributeName } from "./collection_utils.js";
describe("toAttributeName", () => {
it("camelCases a multi-word name, lower-casing the first word", () => {
@@ -69,6 +69,16 @@ describe("buildPromotedDefinition", () => {
});
});
+describe("stripUrlScheme", () => {
+ it("strips the given scheme and leaves a value without it untouched", () => {
+ expect(stripUrlScheme("mailto:[email protected]", "mailto:")).toBe("[email protected]");
+ expect(stripUrlScheme("tel:12345", "tel:")).toBe("12345");
+ expect(stripUrlScheme("[email protected]", "mailto:")).toBe("[email protected]");
+ // Only the scheme asked for is stripped — a mismatched one is somebody else's value.
+ expect(stripUrlScheme("tel:12345", "mailto:")).toBe("tel:12345");
+ });
+});
+
describe("saveFileAttachment", () => {
it("prefers the given MIME, else derives it from the title, else falls back to octet-stream", () => {
// Only the attachment payload is under test here; persisting it is covered by the importer specs.
diff --git a/packages/trilium-core/src/services/import/collection_utils.ts b/packages/trilium-core/src/services/import/collection_utils.ts
index d69be3d9b3..0438be9913 100644
--- a/packages/trilium-core/src/services/import/collection_utils.ts
+++ b/packages/trilium-core/src/services/import/collection_utils.ts
@@ -42,9 +42,10 @@ export function attachmentReferenceLink(noteId: string, attachmentId: string, ti
return `<a class="reference-link" href="#root/${noteId}?viewMode=attachments&attachmentId=${attachmentId}">${escapeHtml(title)}</a>`;
}
-/** Gives a value a clickable scheme (`mailto:` for an email, `tel:` for a phone) unless it already carries it. */
-export function applyUrlScheme(value: string, scheme: string): string {
- return value.startsWith(scheme) ? value : `${scheme}${value}`;
+/** Strips a value's clickable scheme (`mailto:` for an email, `tel:` for a phone) if it carries one, leaving
+ * the bare address the typed `email`/`phone` inputs hold. */
+export function stripUrlScheme(value: string, scheme: string): string {
+ return value.startsWith(scheme) ? value.substring(scheme.length) : value;
}
/** Saves bytes as a `role:"file"` attachment on `note`, defaulting the MIME from the title (or octet-stream). */
diff --git a/packages/trilium-core/src/services/import/notion/collection.spec.ts b/packages/trilium-core/src/services/import/notion/collection.spec.ts
index d46990a97a..e3616a4453 100644
--- a/packages/trilium-core/src/services/import/notion/collection.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/collection.spec.ts
@@ -39,6 +39,20 @@ describe("extractProperties", () => {
)).toEqual([]);
});
+ it("types a url/email/phone cell from its anchor's href, stripping an email/phone scheme", () => {
+ expect(propertiesOf(
+ row("url", "Site", `<td><a class="url-value" href="https://triliumnotes.org">Site</a></td>`),
+ row("email", "Email", `<td><a class="url-value" href="mailto:[email protected]">Email</a></td>`),
+ row("phone_number", "Phone", `<td><a class="url-value" href="12345">Phone</a></td>`)
+ )).toEqual([
+ { name: "Site", value: "https://triliumnotes.org", labelType: "url", multiplicity: "single" },
+ // The typed email/phone inputs hold the bare address, so a scheme on the href is dropped
+ // and a bare href is kept as it is.
+ { name: "Email", value: "[email protected]", labelType: "email", multiplicity: "single" },
+ { name: "Phone", value: "12345", labelType: "phone", multiplicity: "single" }
+ ]);
+ });
+
it("normalizes a formatted number cell and keeps a non-numeric one verbatim", () => {
expect(propertiesOf(
row("number", "Price", "<td>$1,200.50</td>"),
diff --git a/packages/trilium-core/src/services/import/notion/collection.ts b/packages/trilium-core/src/services/import/notion/collection.ts
index 5a45dd8354..3ff4c1e743 100644
--- a/packages/trilium-core/src/services/import/notion/collection.ts
+++ b/packages/trilium-core/src/services/import/notion/collection.ts
@@ -13,7 +13,7 @@ import { dayjs } from "@triliumnext/commons";
import type { HTMLElement } from "node-html-parser";
import type BNote from "../../../becca/entities/bnote.js";
-import { applyUrlScheme, attachmentReferenceLink, buildPromotedDefinition, saveFileAttachment, toAttributeName } from "../collection_utils.js";
+import { attachmentReferenceLink, buildPromotedDefinition, saveFileAttachment, stripUrlScheme, toAttributeName } from "../collection_utils.js";
import type { LinkTarget, NotionProperty, ParsedPage } from "./model.js";
import { getNotionId, stripNotionId } from "./notion_id.js";
import { baseName, internalPageId, ownedFolderKey, parentFolderKey, removeExtension, resolveResourcePath } from "./paths.js";
@@ -69,7 +69,7 @@ export function resolveDatabaseContainers(pages: ParsedPage[], csvPaths: string[
* - `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:`);
+ * - `url` / `email` / `phone_number`: the anchor's href → one single-valued `url`/`email`/`phone` property (bare address, schemes stripped);
* - `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` / `rollup`: a computed value with no type signal, inferred from the rendered cell shape —
@@ -119,11 +119,11 @@ export function extractProperties(root: HTMLElement): NotionProperty[] {
}
}
} else if (type === "url" || type === "email" || type === "phone_number") {
- // All three render as `<a class="url-value">`; the href is the canonical value. Email/phone hrefs
- // are bare addresses, so give them a `mailto:`/`tel:` scheme to stay clickable as url labels.
+ // All three render as `<a class="url-value">`; the href is the canonical value. Email and phone
+ // become typed labels of their own, holding the bare address their inputs expect.
const href = cell.querySelector("a")?.getAttribute("href")?.trim();
if (href) {
- properties.push({ name, value: toUrlValue(type, href), labelType: "url", multiplicity: "single" });
+ properties.push({ name, ...toLinkProperty(type, href), multiplicity: "single" });
}
} else if (type === "date") {
properties.push(...parseDateProperties(name, cell));
@@ -248,15 +248,19 @@ function numericFormulaValue(text: string): string | undefined {
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 {
+/**
+ * Types a `url`/`email`/`phone_number` column's href. An email/phone href is usually the bare address
+ * already, but a `mailto:`/`tel:` scheme is stripped in case one sneaks in — the typed inputs hold the
+ * address alone (and reject a schemed one as invalid).
+ */
+function toLinkProperty(type: string, href: string): Pick<NotionProperty, "value" | "labelType"> {
if (type === "email") {
- return applyUrlScheme(href, "mailto:");
+ return { value: stripUrlScheme(href, "mailto:"), labelType: "email" };
}
if (type === "phone_number") {
- return applyUrlScheme(href, "tel:");
+ return { value: stripUrlScheme(href, "tel:"), labelType: "phone" };
}
- return href;
+ return { value: href, labelType: "url" };
}
/**