Open History to see the last commit
1/** `id` is positional on purpose: the same attachment can be embedded twice in one body. */
2export type PullRequestBodySegment =
3 | { readonly id: string; readonly kind: "markdown"; readonly text: string }
4 | {
5 readonly id: string;
6 readonly kind: "attachment";
7 readonly url: string;
8 readonly media: "video" | "image" | "unknown";
9 };
10
11const FENCE_PATTERN = /^\s{0,3}((?:`{3,})|(?:~{3,}))(.*)$/u;
12/**
13 * How far a `<video>` tag may reach for its closing tag. Real embeds are one to three lines,
14 * and the bound keeps an unclosed tag from making each one rescan the rest of the body.
15 */
16const VIDEO_TAG_MAX_LINES = 8;
17
18const INDENTED_CODE_PATTERN = /^(?: {4}|\t)/u;
19const BARE_URL_PATTERN = /^<?(https?:\/\/\S+?)>?$/u;
20const IMAGE_EXTENSION_PATTERN = /\.(?:png|jpe?g|gif|webp|avif|heic|heif|svg)(?:$|[?#])/iu;
21const MARKDOWN_LINK_PATTERN = /^\[[^\]]*\]\((https?:\/\/[^\s)]+)\)$/u;
22const VIDEO_EXTENSION_PATTERN = /\.(?:mp4|webm|mov|m4v|ogv)(?:$|[?#])/iu;
23
24const GITHUB_ASSET_PATTERN = /^https:\/\/github\.com\/user-attachments\/assets\/[\w-]+$/iu;
25const VIDEO_TAG_SRC_PATTERN = /<(?:video|source)\b[^>]*\bsrc\s*=\s*["']([^"']+)["']/iu;
26
27const STANDALONE_VIDEO_TAG_PATTERN = /^\s*<video\b/iu;
28const VIDEO_TAG_END_PATTERN = /<\/video>\s*$/iu;
29
30function isWebUrl(url: string): boolean {
31 try {
32 const parsed = new URL(url);
33 return parsed.protocol === "https:" || parsed.protocol === "http:";
34 } catch {
35 return false;
36 }
37}
38
39function attachmentFromLine(
40 line: string,
41): { url: string; media: "video" | "image" | "unknown" } | null {
42 const url =
43 BARE_URL_PATTERN.exec(line.trim())?.[1] ?? MARKDOWN_LINK_PATTERN.exec(line.trim())?.[1];
44 if (url === undefined || !isWebUrl(url)) return null;
45 if (VIDEO_EXTENSION_PATTERN.test(url) || GITHUB_ASSET_PATTERN.test(url)) {
46 return { url, media: "video" };
47 }
48 if (IMAGE_EXTENSION_PATTERN.test(url)) return { url, media: "image" };
49 return null;
50}
51
52export function splitPullRequestBody(body: string): ReadonlyArray<PullRequestBodySegment> {
53 const segments: PullRequestBodySegment[] = [];
54 const markdown: string[] = [];
55 let openFence: string | null = null;
56
57 // Blank lines around a run are dropped, but never leading spaces: four of them open an
58 // indented code block, so trimming a run would silently turn code into prose.
59 const flushMarkdown = () => {
60 const text = markdown.join("\n").replace(/^\n+/u, "").replace(/\s+$/u, "");
61 markdown.length = 0;
62 if (text.trim().length > 0) {
63 segments.push({ id: `markdown:${segments.length}`, kind: "markdown", text });
64 }
65 };
66
67 const lines = body.split("\n");
68 for (let index = 0; index < lines.length; index += 1) {
69 const line = lines[index]!;
70 const fenceMatch = FENCE_PATTERN.exec(line);
71 if (fenceMatch !== null) {
72 const fence = fenceMatch[1]!;
73 // A fence closes only on the same marker, at least as long as the one that opened it,
74 // and with nothing after it: `~~~` cannot end a ``` block, and ```` ```ts ```` is an
75 // info string opening a nested fence, not a close.
76 const closes =
77 openFence !== null &&
78 fence[0] === openFence[0] &&
79 fence.length >= openFence.length &&
80 fenceMatch[2]!.trim().length === 0;
81 if (openFence === null) {
82 openFence = fence;
83 } else if (closes) {
84 openFence = null;
85 }
86 markdown.push(line);
87 continue;
88 }
89 if (openFence !== null || INDENTED_CODE_PATTERN.test(line)) {
90 markdown.push(line);
91 continue;
92 }
93
94 const bareAttachment = attachmentFromLine(line);
95 if (bareAttachment !== null) {
96 flushMarkdown();
97 segments.push({ id: `attachment:${segments.length}`, kind: "attachment", ...bareAttachment });
98 continue;
99 }
100
101 if (!STANDALONE_VIDEO_TAG_PATTERN.test(line)) {
102 markdown.push(line);
103 continue;
104 }
105 const lastCandidate = Math.min(index + VIDEO_TAG_MAX_LINES, lines.length) - 1;
106 let cursor = index;
107 while (cursor < lastCandidate && !VIDEO_TAG_END_PATTERN.test(lines[cursor]!)) {
108 cursor += 1;
109 }
110 const source = VIDEO_TAG_END_PATTERN.test(lines[cursor]!)
111 ? VIDEO_TAG_SRC_PATTERN.exec(lines.slice(index, cursor + 1).join("\n"))?.[1]
112 : undefined;
113 if (source !== undefined && isWebUrl(source)) {
114 flushMarkdown();
115 segments.push({
116 id: `attachment:${segments.length}`,
117 kind: "attachment",
118 url: source,
119
120 media: "video",
121 });
122 index = cursor;
123 } else {
124 // Unclosed, or nothing linkable in it: prose. Only this line is consumed, so the
125 // lines that were looked at are still parsed on their own terms.
126 markdown.push(line);
127 }
128 }
129
130 flushMarkdown();
131 return segments;
132}