The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode. |
| 2 | import { parseUrl } from "@t3tools/shared/url"; |
| 3 | |
| 4 | import 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:`. */ |
| 7 | const 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. */ |
| 10 | export function externalUrl(href: string) { |
| 11 | const url = parseUrl(href); |
| 12 | |
| 13 | return url && schemes.has(url.protocol) ? url.href : undefined; |
| 14 | } |
| 15 | |
| 16 | /** Where a Word link leads, if it is one a reader may follow: a bookmark in the document, or an `externalUrl`. */ |
| 17 | export 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; |
| 26 | } |