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