973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | export interface UrlPreview { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | href: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 3 | display: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | hostname: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | path: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | const MAX_LINKS = 4; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | const MAX_URL_LENGTH = 2048; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | const TRAILING_PUNCT = /[.,;:!?'"~…—–-]+$/u; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | function countChar(text: string, char: string): number { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | let count = 0; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | for (const c of text) if (c === char) count += 1; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | return count; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | function trimUrl(raw: string): string { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | let out = raw; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | let changed = true; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | while (changed && out.length > 0) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | changed = false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | const stripped = out.replace(TRAILING_PUNCT, ''); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | if (stripped !== out) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | out = stripped; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | changed = true; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | // Strip a trailing closer only when it has no matching opener left in |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | // the URL itself, so "(https://x/a_(b))" keeps the inner pair and drops |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | // the outer markdown/paren closer. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | const pairs: Array<[string, string]> = [ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | ['(', ')'], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | ['[', ']'], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | ['{', '}'] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | ]; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | for (const [open, close] of pairs) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | if (out.endsWith(close) && countChar(out, close) > countChar(out, open)) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | out = out.slice(0, -1); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | changed = true; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | if (out.endsWith('>')) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | out = out.slice(0, -1); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | changed = true; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | return out; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | /** |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | * Find http(s) URLs in message text without fetching anything: rendering is |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | * purely local (hostname/path/link card) so no third-party request can leak |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | * reader IPs. Callers MUST NOT auto-fetch link targets. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | */ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | export function findUrls(text: string): UrlPreview[] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | if (!text || !text.includes('http')) return []; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | const out: UrlPreview[] = []; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | const seen = new Set<string>(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | const pattern = /https?:\/\/[^\s<>"'`\]]+/gi; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | let match: RegExpExecArray | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | while (out.length < MAX_LINKS && (match = pattern.exec(text)) !== null) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | let candidate = match[0]; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | if (candidate.length > MAX_URL_LENGTH) candidate = candidate.slice(0, MAX_URL_LENGTH); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | const trimmed = trimUrl(candidate); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | if (trimmed.length < 8) continue; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | let parsed: URL | null = null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | try { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | parsed = new URL(trimmed); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | } catch { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | continue; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') continue; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | const href = parsed.toString(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | if (seen.has(href)) continue; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | seen.add(href); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | const rest = `${parsed.pathname}${parsed.search}${parsed.hash}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | out.push({ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | href, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | display: trimmed.length > 72 ? `${trimmed.slice(0, 69)}…` : trimmed, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | hostname: parsed.hostname, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | path: rest && rest !== '/' ? (rest.length > 48 ? `${rest.slice(0, 45)}…` : rest) : '' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | return out; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | } |