The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Root, RootContent } from "mdast"; |
| 2 | import remarkGfm from "remark-gfm"; |
| 3 | import remarkParse from "remark-parse"; |
| 4 | import { unified } from "unified"; |
| 5 | |
| 6 | import { GITHUB_EMOJI_ALIASES } from "./githubEmojiAliases.ts"; |
| 7 | import { sourceControlClients, type ReferenceAutolink } from "./sourceControlClients.ts"; |
| 8 | |
| 9 | /** `id` is positional on purpose: the same attachment can be embedded twice in one body. */ |
| 10 | export type PullRequestBodySegment = |
| 11 | | { |
| 12 | readonly id: string; |
| 13 | readonly kind: "markdown"; |
| 14 | readonly text: string; |
| 15 | /** |
| 16 | * `sourceOffset + i` is where `text[i]` sits in the body, so an edit can be written back. |
| 17 | * Holds past the body's reference definitions, which `text` may lead with. |
| 18 | */ |
| 19 | readonly sourceOffset: number; |
| 20 | } |
| 21 | | { |
| 22 | readonly id: string; |
| 23 | readonly kind: "attachment"; |
| 24 | readonly url: string; |
| 25 | readonly media: "video" | "image" | "unknown"; |
| 26 | } |
| 27 | | { |
| 28 | readonly id: string; |
| 29 | readonly kind: "table"; |
| 30 | readonly rows: ReadonlyArray<ReadonlyArray<PullRequestBodyTableCell>>; |
| 31 | }; |
| 32 | |
| 33 | type PullRequestBodyTableCell = { |
| 34 | readonly header: boolean; |
| 35 | readonly segments: ReadonlyArray<PullRequestBodySegment>; |
| 36 | }; |
| 37 | |
| 38 | // `.` stops at the `\r` of a CRLF body, which is how GitHub stores one written in its web editor. |
| 39 | const FENCE_PATTERN = /^\s{0,3}((?:`{3,})|(?:~{3,}))(.*)\r?$/u; |
| 40 | /** |
| 41 | * How far a `<video>` tag may reach for its closing tag. Real embeds are one to three lines, |
| 42 | * and the bound keeps an unclosed tag from making each one rescan the rest of the body. |
| 43 | */ |
| 44 | const VIDEO_TAG_MAX_LINES = 8; |
| 45 | |
| 46 | // A tab after up to three spaces still reaches the fourth column. |
| 47 | const INDENTED_CODE_PATTERN = /^(?: {4}| {0,3}\t)/u; |
| 48 | const BARE_URL_PATTERN = /^<?(https?:\/\/\S+?)>?$/u; |
| 49 | const IMAGE_EXTENSION_PATTERN = /\.(?:png|jpe?g|gif|webp|avif|heic|heif|svg)(?:$|[?#])/iu; |
| 50 | const MARKDOWN_LINK_PATTERN = /^\[[^\]]*\]\((https?:\/\/[^\s)]+)\)$/u; |
| 51 | const VIDEO_EXTENSION_PATTERN = /\.(?:mp4|webm|mov|m4v|ogv)(?:$|[?#])/iu; |
| 52 | |
| 53 | const VIDEO_TAG_SRC_PATTERN = /<(?:video|source)\b[^>]*\bsrc\s*=\s*["']([^"']+)["']/iu; |
| 54 | |
| 55 | const STANDALONE_VIDEO_TAG_PATTERN = /^\s*<video\b/iu; |
| 56 | const VIDEO_TAG_END_PATTERN = /<\/video>\s*$/iu; |
| 57 | |
| 58 | /** Bounded like `VIDEO_TAG_MAX_LINES`; room for a before/after table with several rows. */ |
| 59 | const TABLE_MAX_LINES = 64; |
| 60 | const TABLE_START_PATTERN = /^\s*<table\b/iu; |
| 61 | const TABLE_END_PATTERN = /<\/table>\s*$/iu; |
| 62 | const TABLE_OPEN_PATTERN = /<table\b/giu; |
| 63 | const TABLE_CLOSE_PATTERN = /<\/table\s*>/giu; |
| 64 | /** |
| 65 | * Quoted attribute values may hold `>`. The alternatives start on disjoint characters and an |
| 66 | * unquoted run stops at the next `<`, so a run of unterminated tags stays linear. |
| 67 | */ |
| 68 | const TABLE_STRUCTURE_TAG_PATTERN = |
| 69 | /<(\/?)(table|thead|tbody|tfoot|tr|td|th)\b((?:[^<>"']|"[^"]*"|'[^']*')*)>/giu; |
| 70 | /** Rows are laid out independently, so a spanning cell would shift the cells after it. */ |
| 71 | const CELL_SPAN_PATTERN = /\b(?:col|row)span\b/iu; |
| 72 | |
| 73 | function isWebUrl(url: string): boolean { |
| 74 | try { |
| 75 | const parsed = new URL(url); |
| 76 | return parsed.protocol === "https:" || parsed.protocol === "http:"; |
| 77 | } catch { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function attachmentFromLine( |
| 83 | line: string, |
| 84 | ): { url: string; media: "video" | "image" | "unknown" } | null { |
| 85 | const url = |
| 86 | BARE_URL_PATTERN.exec(line.trim())?.[1] ?? MARKDOWN_LINK_PATTERN.exec(line.trim())?.[1]; |
| 87 | if (url === undefined || !isWebUrl(url)) return null; |
| 88 | if (VIDEO_EXTENSION_PATTERN.test(url) || sourceControlClients.mediaUrl(url)?.kind === "video") { |
| 89 | return { url, media: "video" }; |
| 90 | } |
| 91 | if (IMAGE_EXTENSION_PATTERN.test(url)) return { url, media: "image" }; |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | type TableRows = ReadonlyArray<ReadonlyArray<PullRequestBodyTableCell>>; |
| 96 | |
| 97 | /** |
| 98 | * Cells of a table made only of rows and cells, or null when anything else is in it: a caption, |
| 99 | * text between cells, a nested table. Cell contents are split on their own, so tables never nest. |
| 100 | */ |
| 101 | function tableRows(html: string, htmlOffset: number): TableRows | null { |
| 102 | const rows: PullRequestBodyTableCell[][] = []; |
| 103 | let row: PullRequestBodyTableCell[] | null = null; |
| 104 | let cell: { tag: string; contentStart: number } | null = null; |
| 105 | let opened = false; |
| 106 | let closed = false; |
| 107 | let cursor = 0; |
| 108 | for (const match of html.matchAll(TABLE_STRUCTURE_TAG_PATTERN)) { |
| 109 | const closing = match[1] === "/"; |
| 110 | const tag = match[2]!.toLowerCase(); |
| 111 | if (cell !== null) { |
| 112 | if (!closing || tag !== cell.tag) return null; |
| 113 | const content = html.slice(cell.contentStart, match.index); |
| 114 | // Indented HTML would render as a code block once the cell is markdown on its own. |
| 115 | if (content.split("\n").some((line) => INDENTED_CODE_PATTERN.test(line) && line.trim())) |
| 116 | return null; |
| 117 | row!.push({ |
| 118 | header: tag === "th", |
| 119 | segments: splitBodyAt(content, htmlOffset + cell.contentStart), |
| 120 | }); |
| 121 | cell = null; |
| 122 | } else { |
| 123 | if (html.slice(cursor, match.index).trim().length > 0) return null; |
| 124 | const tableOpen = opened && !closed; |
| 125 | if (tag === "table") { |
| 126 | if (closing ? !tableOpen || row !== null : opened) return null; |
| 127 | opened = true; |
| 128 | closed = closing; |
| 129 | } else if (tag === "tr") { |
| 130 | if (!tableOpen || closing === (row === null)) return null; |
| 131 | if (closing) rows.push(row!); |
| 132 | row = closing ? null : []; |
| 133 | } else if (tag === "td" || tag === "th") { |
| 134 | if (closing || row === null || CELL_SPAN_PATTERN.test(match[3]!)) return null; |
| 135 | cell = { tag, contentStart: match.index + match[0].length }; |
| 136 | } else if (!tableOpen || row !== null) { |
| 137 | return null; |
| 138 | } |
| 139 | } |
| 140 | cursor = match.index + match[0].length; |
| 141 | } |
| 142 | if (!closed || html.slice(cursor).trim().length > 0) return null; |
| 143 | return rows.filter((cells) => cells.length > 0); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * The table opening at `start`, through its matching close. `end` is the last line looked at |
| 148 | * even when the table is not usable, so the caller never re-reads an inner table on its own. |
| 149 | */ |
| 150 | function mediaTableAt( |
| 151 | lines: ReadonlyArray<string>, |
| 152 | start: number, |
| 153 | startOffset: number, |
| 154 | ): { end: number; rows: TableRows | null } { |
| 155 | const lastCandidate = Math.min(start + TABLE_MAX_LINES, lines.length) - 1; |
| 156 | let depth = 0; |
| 157 | let end = start; |
| 158 | for (; ; end += 1) { |
| 159 | const line = lines[end]!; |
| 160 | depth += |
| 161 | (line.match(TABLE_OPEN_PATTERN)?.length ?? 0) - |
| 162 | (line.match(TABLE_CLOSE_PATTERN)?.length ?? 0); |
| 163 | if (depth <= 0 || end === lastCandidate) break; |
| 164 | } |
| 165 | if (depth !== 0 || !TABLE_END_PATTERN.test(lines[end]!)) return { end, rows: null }; |
| 166 | // Lifting attachment links out of a table's cells would tear the table apart. |
| 167 | const rows = tableRows(lines.slice(start, end + 1).join("\n"), startOffset); |
| 168 | const hasMedia = rows?.some((cells) => |
| 169 | cells.some((cell) => cell.segments.some((segment) => segment.kind === "attachment")), |
| 170 | ); |
| 171 | return { end, rows: hasMedia ? rows : null }; |
| 172 | } |
| 173 | |
| 174 | const definitionParser = unified().use(remarkParse).use(remarkGfm); |
| 175 | |
| 176 | /** |
| 177 | * The first definition of each label in the body, as authored, each joined onto one line so it |
| 178 | * stands outside the quote or list it was written in. |
| 179 | */ |
| 180 | function referenceDefinitions(body: string): string[] { |
| 181 | const definitions = new Map<string, string>(); |
| 182 | const visit = (node: Root | RootContent, quotes: number) => { |
| 183 | if (node.type === "definition" && !definitions.has(node.identifier)) { |
| 184 | const wrap = new RegExp(`(?:\\r\\n|\\r|\\n)(?:[ \\t]*>){0,${quotes}}[ \\t]*`, "gu"); |
| 185 | const source = body.slice(node.position?.start.offset, node.position?.end.offset); |
| 186 | definitions.set(node.identifier, source.replace(wrap, " ")); |
| 187 | } |
| 188 | if (!("children" in node)) return; |
| 189 | for (const child of node.children) visit(child, quotes + (node.type === "blockquote" ? 1 : 0)); |
| 190 | }; |
| 191 | visit(definitionParser.parse(body), 0); |
| 192 | return [...definitions.values()]; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Each markdown segment renders as its own document, so it leads with the body's definitions. |
| 197 | * Leading, they win over a later redefinition in the segment, as the first one does in the body. |
| 198 | */ |
| 199 | function withDefinitions( |
| 200 | segment: PullRequestBodySegment, |
| 201 | definitions: string, |
| 202 | ): PullRequestBodySegment { |
| 203 | if (segment.kind === "attachment") return segment; |
| 204 | if (segment.kind === "table") { |
| 205 | return { |
| 206 | ...segment, |
| 207 | rows: segment.rows.map((cells) => |
| 208 | cells.map((cell) => ({ |
| 209 | ...cell, |
| 210 | segments: cell.segments.map((inner) => withDefinitions(inner, definitions)), |
| 211 | })), |
| 212 | ), |
| 213 | }; |
| 214 | } |
| 215 | if (!segment.text.includes("[")) return segment; |
| 216 | return { |
| 217 | ...segment, |
| 218 | text: definitions + segment.text, |
| 219 | sourceOffset: segment.sourceOffset - definitions.length, |
| 220 | }; |
| 221 | } |
| 222 | |
| 223 | export function splitPullRequestBody(body: string): ReadonlyArray<PullRequestBodySegment> { |
| 224 | const segments = splitBodyAt(body, 0); |
| 225 | // One markdown segment is the whole body, which resolves its own references. |
| 226 | const whole = segments.length === 1 && segments[0]!.kind === "markdown"; |
| 227 | if (whole || !body.includes("]:")) return segments; |
| 228 | const definitions = referenceDefinitions(body); |
| 229 | if (definitions.length === 0) return segments; |
| 230 | const leading = `${definitions.join("\n")}\n\n`; |
| 231 | return segments.map((segment) => withDefinitions(segment, leading)); |
| 232 | } |
| 233 | |
| 234 | /** `baseOffset` is where `body` starts in the whole body, so a table cell's edits write back. */ |
| 235 | function splitBodyAt(body: string, baseOffset: number): ReadonlyArray<PullRequestBodySegment> { |
| 236 | const segments: PullRequestBodySegment[] = []; |
| 237 | const markdown: string[] = []; |
| 238 | let markdownStart = 0; |
| 239 | let openFence: string | null = null; |
| 240 | |
| 241 | // Blank lines around a run are dropped, but never leading spaces: four of them open an |
| 242 | // indented code block, so trimming a run would silently turn code into prose. |
| 243 | const flushMarkdown = () => { |
| 244 | const joined = markdown.join("\n"); |
| 245 | const text = joined.replace(/^(?:\r?\n)+/u, ""); |
| 246 | const sourceOffset = markdownStart + joined.length - text.length; |
| 247 | markdown.length = 0; |
| 248 | if (text.trim().length > 0) { |
| 249 | segments.push({ |
| 250 | id: `markdown:${segments.length}`, |
| 251 | kind: "markdown", |
| 252 | text: text.replace(/\s+$/u, ""), |
| 253 | sourceOffset, |
| 254 | }); |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | const lines = body.split("\n"); |
| 259 | let tableScannedThrough = -1; |
| 260 | let lineStart = baseOffset; |
| 261 | for (let index = 0; index < lines.length; lineStart += lines[index]!.length + 1, index += 1) { |
| 262 | const line = lines[index]!; |
| 263 | if (markdown.length === 0) markdownStart = lineStart; |
| 264 | const fenceMatch = FENCE_PATTERN.exec(line); |
| 265 | // ```` ```npm test``` ```` is a code span, not an opener that would flip every later fence. |
| 266 | // A span that runs past this line is left an opener, so its lines stay out of media. |
| 267 | const isInlineCode = |
| 268 | openFence === null && |
| 269 | fenceMatch?.[1]![0] === "`" && |
| 270 | (fenceMatch[2]!.match(/`+/gu) ?? []).some((run) => run.length === fenceMatch[1]!.length); |
| 271 | if (fenceMatch !== null && !isInlineCode) { |
| 272 | const fence = fenceMatch[1]!; |
| 273 | // A fence closes only on the same marker, at least as long as the one that opened it, |
| 274 | // and with nothing after it: `~~~` cannot end a ``` block, and ```` ```ts ```` is an |
| 275 | // info string opening a nested fence, not a close. |
| 276 | const closes = |
| 277 | openFence !== null && |
| 278 | fence[0] === openFence[0] && |
| 279 | fence.length >= openFence.length && |
| 280 | fenceMatch[2]!.trim().length === 0; |
| 281 | if (openFence === null) { |
| 282 | openFence = fence; |
| 283 | } else if (closes) { |
| 284 | openFence = null; |
| 285 | } |
| 286 | markdown.push(line); |
| 287 | continue; |
| 288 | } |
| 289 | if (openFence !== null || INDENTED_CODE_PATTERN.test(line)) { |
| 290 | markdown.push(line); |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | if (index > tableScannedThrough && TABLE_START_PATTERN.test(line)) { |
| 295 | const table = mediaTableAt(lines, index, lineStart); |
| 296 | tableScannedThrough = table.end; |
| 297 | if (table.rows !== null) { |
| 298 | flushMarkdown(); |
| 299 | segments.push({ id: `table:${segments.length}`, kind: "table", rows: table.rows }); |
| 300 | for (; index < table.end; index += 1) lineStart += lines[index]!.length + 1; |
| 301 | continue; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | const bareAttachment = attachmentFromLine(line); |
| 306 | if (bareAttachment !== null) { |
| 307 | flushMarkdown(); |
| 308 | segments.push({ id: `attachment:${segments.length}`, kind: "attachment", ...bareAttachment }); |
| 309 | continue; |
| 310 | } |
| 311 | |
| 312 | if (!STANDALONE_VIDEO_TAG_PATTERN.test(line)) { |
| 313 | markdown.push(line); |
| 314 | continue; |
| 315 | } |
| 316 | const lastCandidate = Math.min(index + VIDEO_TAG_MAX_LINES, lines.length) - 1; |
| 317 | let cursor = index; |
| 318 | while (cursor < lastCandidate && !VIDEO_TAG_END_PATTERN.test(lines[cursor]!)) { |
| 319 | cursor += 1; |
| 320 | } |
| 321 | const source = VIDEO_TAG_END_PATTERN.test(lines[cursor]!) |
| 322 | ? VIDEO_TAG_SRC_PATTERN.exec(lines.slice(index, cursor + 1).join("\n"))?.[1] |
| 323 | : undefined; |
| 324 | if (source !== undefined && isWebUrl(source)) { |
| 325 | flushMarkdown(); |
| 326 | segments.push({ |
| 327 | id: `attachment:${segments.length}`, |
| 328 | kind: "attachment", |
| 329 | url: source, |
| 330 | |
| 331 | media: "video", |
| 332 | }); |
| 333 | for (; index < cursor; index += 1) lineStart += lines[index]!.length + 1; |
| 334 | } else { |
| 335 | // Unclosed, or nothing linkable in it: prose. Only this line is consumed, so the |
| 336 | // lines that were looked at are still parsed on their own terms. |
| 337 | markdown.push(line); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | flushMarkdown(); |
| 342 | return segments; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Finds the references the change request's host links in plain text, resolved against the change |
| 347 | * request's repository. Null when the host links none. |
| 348 | */ |
| 349 | export function changeRequestReferenceAutolinker( |
| 350 | changeRequest: { readonly provider: string; readonly url: string } | null | undefined, |
| 351 | ): ((text: string) => ReadonlyArray<ReferenceAutolink>) | null { |
| 352 | if (!changeRequest) return null; |
| 353 | const find = sourceControlClients.get(changeRequest.provider).referenceAutolinks; |
| 354 | const repositoryUrl = sourceControlClients.parseChangeRequestUrl(changeRequest.url)?.link |
| 355 | .repositoryUrl; |
| 356 | return find === undefined || repositoryUrl === undefined |
| 357 | ? null |
| 358 | : (text) => find(text, repositoryUrl); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Forges link uploads root-relative, as GitCafe's `/api/attachments/<id>`. In a change request |
| 363 | * body that always means the forge's origin, never a path on the machine rendering it. |
| 364 | */ |
| 365 | export function resolveChangeRequestMediaUrl(url: string, changeRequestUrl: string): string { |
| 366 | if (!/^\/(?![/\\])/u.test(url)) return url; |
| 367 | try { |
| 368 | return new URL(url, changeRequestUrl).href; |
| 369 | } catch { |
| 370 | return url; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | let githubEmojiByAlias: ReadonlyMap<string, string> | undefined; |
| 375 | const githubEmojiAlias = /:([a-z\d_+-]+)(?=:)/gu; |
| 376 | |
| 377 | /** GitHub's `:alias:` emoji in `text`; a colon closes one alias only, so `:tada:tada:` has one. */ |
| 378 | export function githubEmojiAliases( |
| 379 | text: string, |
| 380 | ): ReadonlyArray<{ readonly index: number; readonly text: string; readonly emoji: string }> { |
| 381 | githubEmojiByAlias ??= new Map( |
| 382 | GITHUB_EMOJI_ALIASES.trim() |
| 383 | .split("\n") |
| 384 | .map((line) => { |
| 385 | const space = line.indexOf(" "); |
| 386 | return [line.slice(0, space), line.slice(space + 1)] as const; |
| 387 | }), |
| 388 | ); |
| 389 | const found: { index: number; text: string; emoji: string }[] = []; |
| 390 | let end = 0; |
| 391 | for (const match of text.matchAll(githubEmojiAlias)) { |
| 392 | const emoji = githubEmojiByAlias.get(match[1]!); |
| 393 | if (emoji === undefined || match.index < end) continue; |
| 394 | end = match.index + match[0].length + 1; |
| 395 | found.push({ index: match.index, text: `${match[0]}:`, emoji }); |
| 396 | } |
| 397 | return found; |
| 398 | } |
| 399 | |
| 400 | export function expandGithubEmojiAliases(text: string): string { |
| 401 | let expanded = ""; |
| 402 | let start = 0; |
| 403 | for (const alias of githubEmojiAliases(text)) { |
| 404 | expanded += text.slice(start, alias.index) + alias.emoji; |
| 405 | start = alias.index + alias.text.length; |
| 406 | } |
| 407 | return start === 0 ? text : expanded + text.slice(start); |
| 408 | } |