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 | |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 50 | export interface LinkSpan { |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 51 | start: number; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 52 | end: number; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 53 | href: string; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 54 | } |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 55 | |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 56 | export function linkSpans(text: string): LinkSpan[] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | if (!text || !text.includes('http')) return []; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 58 | const out: LinkSpan[] = []; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 59 | for (const match of text.matchAll(/https?:\/\/[^\s<>"'`\]]+/gi)) { |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 60 | const trimmed = trimUrl(match[0].slice(0, MAX_URL_LENGTH)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | if (trimmed.length < 8) continue; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 62 | let parsed: URL; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | try { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | parsed = new URL(trimmed); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | } catch { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | continue; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') continue; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 69 | out.push({ start: match.index, end: match.index + trimmed.length, href: parsed.toString() }); |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 70 | } |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 71 | return out; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 72 | } |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 73 | |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 74 | /** |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 75 | * Find http(s) URLs in message text without fetching anything: rendering is |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 76 | * purely local (hostname/path/link card) so no third-party request can leak |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 77 | * reader IPs. Callers MUST NOT auto-fetch link targets. |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 78 | */ |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 79 | export function findUrls(text: string): UrlPreview[] { |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 80 | const out: UrlPreview[] = []; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 81 | const seen = new Set<string>(); |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 82 | for (const span of linkSpans(text)) { |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 83 | if (out.length >= MAX_LINKS) break; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 84 | const parsed = new URL(span.href); |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 85 | if (parsed.hostname === 'matrix.to' || seen.has(span.href)) continue; |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 86 | seen.add(span.href); |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 87 | const trimmed = text.slice(span.start, span.end); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | const rest = `${parsed.pathname}${parsed.search}${parsed.hash}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | out.push({ |
705b1e1062c2 | atagen | 2026-10-23 09:05 UTC | 90 | href: span.href, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | display: trimmed.length > 72 ? `${trimmed.slice(0, 69)}…` : trimmed, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | hostname: parsed.hostname, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | path: rest && rest !== '/' ? (rest.length > 48 ? `${rest.slice(0, 45)}…` : rest) : '' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 96 | return out; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 97 | } |