Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

links.ts

26 lines1.0 KB

1// Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode.
2import { parseUrl } from "@t3tools/shared/url";
3​
4import type { WordLink } from "./word-protocol";
5​
6/** The schemes the host opens outside the app; it refuses any other, such as `javascript:`, `file:` or `tel:`. */
7const schemes = new Set(["http:", "https:", "mailto:"]);
8​
9/** A document's link address, normalized, when the host may open it; undefined otherwise, so the link stays text. */
10export function externalUrl(href: string) {
11 const url = parseUrl(href);
12​
13 return url && schemes.has(url.protocol) ? url.href : undefined;
15​
16/** Where a Word link leads, if it is one a reader may follow: a bookmark in the document, or an `externalUrl`. */
17export function wordLink(href: string | undefined): WordLink | undefined {
18 if (!href) return undefined;
19​
20 if (href.startsWith("#"))
21 return href.length > 1 ? { kind: "internal", bookmark: href.slice(1) } : undefined;
22​
23 const url = externalUrl(href);
24​
25 return url ? { kind: "external", url } : undefined;