Skip to content

Build your personal knowledge base with Trilium Notes

fix(links): keep tel: and sms: addresses as typed in Add link

Elian Doranf36358438ef1
diff --git a/apps/client/src/widgets/dialogs/add_link.spec.tsx b/apps/client/src/widgets/dialogs/add_link.spec.tsx
index 9a2f533708..844b4d4609 100644
--- a/apps/client/src/widgets/dialogs/add_link.spec.tsx
+++ b/apps/client/src/widgets/dialogs/add_link.spec.tsx
@@ -30,6 +30,10 @@ describe("normalizeWebLink", () => {
expect(normalizeWebLink("localhost:8080")).toBe("https://localhost:8080");
expect(normalizeWebLink("http://example.com")).toBe("http://example.com");
expect(normalizeWebLink("mailto:[email protected]")).toBe("mailto:[email protected]");
+ expect(normalizeWebLink("tel:123456789")).toBe("tel:123456789");
+ expect(normalizeWebLink("SMS:+123456789")).toBe("SMS:+123456789");
+ expect(normalizeWebLink("geo:47.6,-122.3")).toBe("geo:47.6,-122.3");
+ expect(normalizeWebLink("example.com:443/a")).toBe("https://example.com:443/a");
expect(normalizeWebLink(" ")).toBeNull();
});
});
diff --git a/apps/client/src/widgets/dialogs/add_link.tsx b/apps/client/src/widgets/dialogs/add_link.tsx
index 6ae860f38c..6aac89f662 100644
--- a/apps/client/src/widgets/dialogs/add_link.tsx
+++ b/apps/client/src/widgets/dialogs/add_link.tsx
@@ -1,5 +1,6 @@
import "./add_link.css";
+import { ALLOWED_PROTOCOLS } from "@triliumnext/commons";
import { useEffect, useRef, useState } from "preact/hooks";
import froca from "../../services/froca";
@@ -461,6 +462,9 @@ function hasScheme(text: string) {
export function normalizeWebLink(text: string): string | null {
const link = text.trim();
if (!link) return null;
- // A colon followed by a digit is a port, as in `localhost:8080`, not the end of a scheme.
- return /^[a-z][a-z0-9+.-]*:(?!\d)/i.test(link) ? link : `https://${link}`;
+ const scheme = /^([a-z][a-z0-9+.-]*):(\d?)/i.exec(link);
+ // A colon followed by a digit is a port, as in `localhost:8080`, unless it ends a scheme
+ // Trilium allows, as in `tel:123`.
+ const hasScheme = scheme && (!scheme[2] || ALLOWED_PROTOCOLS.includes(scheme[1].toLowerCase()));
+ return hasScheme ? link : `https://${link}`;
}