|
| 1 | +/** URL spans: bare http(s) scanning plus splitting styled runs on hits. */ |
| 2 | +export interface LinkSpan { |
| 3 | + readonly text: string; |
| 4 | + readonly fg: string; |
| 5 | + readonly bold?: boolean | undefined; |
| 6 | + readonly url: string | null; |
| 7 | +} |
| 8 | + |
| 9 | +export interface LinkHit { |
| 10 | + readonly url: string; |
| 11 | + readonly start: number; |
| 12 | + readonly end: number; |
| 13 | +} |
| 14 | + |
| 15 | +const URL_PATTERN = /https?:\/\/[^\s<>"'`\]]+/gi; |
| 16 | +const TRAILING_PUNCTUATION = new Set([ |
| 17 | + ".", |
| 18 | + ",", |
| 19 | + ";", |
| 20 | + ":", |
| 21 | + "!", |
| 22 | + "?", |
| 23 | + "'", |
| 24 | + '"', |
| 25 | + "]", |
| 26 | + "}", |
| 27 | + ">", |
| 28 | +]); |
| 29 | + |
| 30 | +/** http(s) runs inside plain text, without trailing prose punctuation. */ |
| 31 | +export function findLinks(text: string): LinkHit[] { |
| 32 | + const hits: LinkHit[] = []; |
| 33 | + URL_PATTERN.lastIndex = 0; |
| 34 | + for (const match of text.matchAll(URL_PATTERN)) { |
| 35 | + const end = trimUrlEnd(text, match.index, match.index + match[0].length); |
| 36 | + if (end > match.index) |
| 37 | + hits.push({ url: text.slice(match.index, end), start: match.index, end }); |
| 38 | + } |
| 39 | + return hits; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * The end of a URL match once prose punctuation is out: trailing sentence |
| 44 | + * punctuation never belongs to the link, and a closing paren only does when |
| 45 | + * the match opened one to balance it. |
| 46 | + */ |
| 47 | +export function trimUrlEnd(text: string, start: number, end: number): number { |
| 48 | + let trimmed = end; |
| 49 | + while (trimmed > start) { |
| 50 | + const tail = text[trimmed - 1]; |
| 51 | + if (tail === undefined || !TRAILING_PUNCTUATION.has(tail)) break; |
| 52 | + trimmed -= 1; |
| 53 | + } |
| 54 | + let depth = 0; |
| 55 | + for (let i = start; i < trimmed; i += 1) { |
| 56 | + if (text[i] === "(") depth += 1; |
| 57 | + if (text[i] === ")") depth -= 1; |
| 58 | + } |
| 59 | + while (trimmed > start && text[trimmed - 1] === ")" && depth < 0) { |
| 60 | + trimmed -= 1; |
| 61 | + depth += 1; |
| 62 | + } |
| 63 | + return trimmed; |
| 64 | +} |
| 65 | + |
| 66 | +/** Split styled segments so URL runs become their own spans. */ |
| 67 | +export function splitLinkSpans( |
| 68 | + segments: readonly { text: string; fg: string; bold?: boolean | undefined }[], |
| 69 | +): LinkSpan[] { |
| 70 | + const spans: LinkSpan[] = []; |
| 71 | + for (const segment of segments) { |
| 72 | + spans.push( |
| 73 | + ...sliceSpans( |
| 74 | + segment, |
| 75 | + findLinks(segment.text).map((hit): SliceHit => ({ |
| 76 | + ...hit, |
| 77 | + text: hit.url, |
| 78 | + })), |
| 79 | + ), |
| 80 | + ); |
| 81 | + } |
| 82 | + return spans; |
| 83 | +} |
| 84 | + |
| 85 | +/** A hit with the exact text its span paints (trimmed of prose punctuation). */ |
| 86 | +export interface SliceHit extends LinkHit { |
| 87 | + readonly text: string; |
| 88 | +} |
| 89 | + |
| 90 | +/** Cut one segment on explicit hits; a hitless segment stays one null span. */ |
| 91 | +export function sliceSpans( |
| 92 | + segment: { text: string; fg: string; bold?: boolean | undefined }, |
| 93 | + hits: readonly SliceHit[], |
| 94 | +): LinkSpan[] { |
| 95 | + if (hits.length === 0) { |
| 96 | + return [ |
| 97 | + { text: segment.text, fg: segment.fg, bold: segment.bold, url: null }, |
| 98 | + ]; |
| 99 | + } |
| 100 | + const spans: LinkSpan[] = []; |
| 101 | + let cursor = 0; |
| 102 | + for (const hit of hits) { |
| 103 | + if (hit.start > cursor) |
| 104 | + spans.push({ |
| 105 | + text: segment.text.slice(cursor, hit.start), |
| 106 | + fg: segment.fg, |
| 107 | + bold: segment.bold, |
| 108 | + url: null, |
| 109 | + }); |
| 110 | + spans.push({ |
| 111 | + text: hit.text, |
| 112 | + fg: segment.fg, |
| 113 | + bold: segment.bold, |
| 114 | + url: hit.url, |
| 115 | + }); |
| 116 | + cursor = hit.end; |
| 117 | + } |
| 118 | + if (cursor < segment.text.length) |
| 119 | + spans.push({ |
| 120 | + text: segment.text.slice(cursor), |
| 121 | + fg: segment.fg, |
| 122 | + bold: segment.bold, |
| 123 | + url: null, |
| 124 | + }); |
| 125 | + return spans; |
| 126 | +} |
0 commit comments