@devries-cameron20 / t3code-1 / commits / 1159aa2991
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(client-runtime): preserve PR reference links across embedded media (#36660)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 12:35 UTC 1159aa299119b6b6d76405891c9240ae37861578
4 files changed +166 -3
packages/client-runtime/package.json
+5 -1
@@ -590,11 +590,15 @@
590 "@t3tools/source-control-definitions": "workspace:*",
591 "effect": "catalog:",
592 "jszip": "3.10.1",
593 + "remark-gfm": "catalog:",
594 + "remark-parse": "catalog:",
595 "three": "0.180.0",
594 - "unbash": "^5.0.0"
596 + "unbash": "^5.0.0",
597 + "unified": "catalog:"
598 },
599 "devDependencies": {
600 "@effect/vitest": "catalog:",
601 + "@types/mdast": "^4.0.4",
602 "@types/three": "0.180.0",
603 "vite-plus": "catalog:"
604 }
packages/client-runtime/src/pullRequestMarkdown.test.ts
+83
@@ -1,3 +1,7 @@
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 import { describe, expect, it } from "vite-plus/test";
6
7 import {
@@ -156,6 +160,85 @@ describe("pull request body segmentation", () => {
160 });
161 });
162
163 +describe("reference links around lifted media", () => {
164 + const recording =
165 + "https://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456";
166 + const parser = unified().use(remarkParse).use(remarkGfm);
167 + const prose = (segments: ReadonlyArray<PullRequestBodySegment>): string[] =>
168 + segments.flatMap((segment) =>
169 + segment.kind === "markdown"
170 + ? [segment.text]
171 + : segment.kind === "table"
172 + ? segment.rows.flat().flatMap((cell) => prose(cell.segments))
173 + : [],
174 + );
175 + // Each segment renders as its own document; the first definition of a label wins, as in mdast-util-to-hast.
176 + const referenceLinks = (body: string) =>
177 + prose(splitPullRequestBody(body)).flatMap((text) => {
178 + const definitions = new Map<string, string>();
179 + const references: Array<{ identifier: string; text: string }> = [];
180 + const visit = (node: Root | RootContent) => {
181 + if (node.type === "definition" && !definitions.has(node.identifier)) {
182 + definitions.set(node.identifier, node.url);
183 + }
184 + if (node.type === "linkReference") {
185 + references.push({
186 + identifier: node.identifier,
187 + text: node.children.map((child) => ("value" in child ? child.value : "")).join(""),
188 + });
189 + }
190 + if ("children" in node) node.children.forEach(visit);
191 + };
192 + visit(parser.parse(text));
193 + return references.map((reference) => [reference.text, definitions.get(reference.identifier)]);
194 + });
195 +
196 + it.each([
197 + [
198 + "use, then media, then definition",
199 + `See [the docs][guide].\n\n${recording}\n\n[guide]: https://example.com/docs`,
200 + ],
201 + [
202 + "definition, then media, then use",
203 + `[guide]: https://example.com/docs\n\n${recording}\n\nSee [the docs][guide].`,
204 + ],
205 + ...["\n", "\r\n", "\r"].map((lineEnding) => [
206 + `a definition wrapped over quoted lines with ${JSON.stringify(lineEnding)}`,
207 + `> [guide]:${lineEnding}> https://example.com/docs${lineEnding}> "The guide"\n\n${recording}\n\nSee [the docs][guide].`,
208 + ]),
209 + [
210 + "a use inside a media table cell",
211 + `<table><tr><td>\n\nSee [the docs][guide].\n\n</td><td>\n\n${recording}\n\n</td></tr></table>\n\n[guide]: https://example.com/docs`,
212 + ],
213 + ])("resolves a reference defined across a video: %s", (_case, body) => {
214 + expect(referenceLinks(body)).toEqual([["the docs", "https://example.com/docs"]]);
215 + });
216 +
217 + it("keeps the body's first definition of a label, wherever the media falls", () => {
218 + expect(
219 + referenceLinks(
220 + `[guide]: https://example.com/first\n\n${recording}\n\nSee [the docs][guide].\n\n[GUIDE]: https://example.com/second`,
221 + ),
222 + ).toEqual([["the docs", "https://example.com/first"]]);
223 + });
224 +
225 + it("leaves a definition shown in code as code", () => {
226 + expect(
227 + referenceLinks(
228 + `\`\`\`md\n[guide]: https://example.com/docs\n\`\`\`\n\n${recording}\n\nSee [the docs][guide].`,
229 + ),
230 + ).toEqual([]);
231 + });
232 +
233 + it("still writes a task tick back to its place in the body", () => {
234 + const body = `[guide]: https://example.com/docs\n\n${recording}\n\n- [ ] Read [the docs][guide]`;
235 + const segment = splitPullRequestBody(body).at(-1)!;
236 + if (segment.kind !== "markdown") throw new Error("expected prose last");
237 + const marker = segment.text.indexOf("[ ]");
238 + expect(body.slice(segment.sourceOffset + marker)).toBe("[ ] Read [the docs][guide]");
239 + });
240 +});
241 +
242 describe("media on custom hosts", () => {
243 it.each([
244 ["https://drop.example.com/capture.PNG?token=abc#preview", "image"],
packages/client-runtime/src/pullRequestMarkdown.ts
+66 -2
@@ -1,3 +1,8 @@
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 { sourceControlClients, type ReferenceAutolink } from "./sourceControlClients.ts";
7
8 /** `id` is positional on purpose: the same attachment can be embedded twice in one body. */
@@ -6,7 +11,10 @@ export type PullRequestBodySegment =
11 readonly id: string;
12 readonly kind: "markdown";
13 readonly text: string;
9 - /** Where `text` starts in the body, so an edit made to it can be written back. */
14 + /**
15 + * `sourceOffset + i` is where `text[i]` sits in the body, so an edit can be written back.
16 + * Holds past the body's reference definitions, which `text` may lead with.
17 + */
18 readonly sourceOffset: number;
19 }
20 | {
@@ -162,8 +170,64 @@ function mediaTableAt(
170 return { end, rows: hasMedia ? rows : null };
171 }
172
173 +const definitionParser = unified().use(remarkParse).use(remarkGfm);
174 +
175 +/**
176 + * The first definition of each label in the body, as authored, each joined onto one line so it
177 + * stands outside the quote or list it was written in.
178 + */
179 +function referenceDefinitions(body: string): string[] {
180 + const definitions = new Map<string, string>();
181 + const visit = (node: Root | RootContent, quotes: number) => {
182 + if (node.type === "definition" && !definitions.has(node.identifier)) {
183 + const wrap = new RegExp(`(?:\\r\\n|\\r|\\n)(?:[ \\t]*>){0,${quotes}}[ \\t]*`, "gu");
184 + const source = body.slice(node.position?.start.offset, node.position?.end.offset);
185 + definitions.set(node.identifier, source.replace(wrap, " "));
186 + }
187 + if (!("children" in node)) return;
188 + for (const child of node.children) visit(child, quotes + (node.type === "blockquote" ? 1 : 0));
189 + };
190 + visit(definitionParser.parse(body), 0);
191 + return [...definitions.values()];
192 +}
193 +
194 +/**
195 + * Each markdown segment renders as its own document, so it leads with the body's definitions.
196 + * Leading, they win over a later redefinition in the segment, as the first one does in the body.
197 + */
198 +function withDefinitions(
199 + segment: PullRequestBodySegment,
200 + definitions: string,
201 +): PullRequestBodySegment {
202 + if (segment.kind === "attachment") return segment;
203 + if (segment.kind === "table") {
204 + return {
205 + ...segment,
206 + rows: segment.rows.map((cells) =>
207 + cells.map((cell) => ({
208 + ...cell,
209 + segments: cell.segments.map((inner) => withDefinitions(inner, definitions)),
210 + })),
211 + ),
212 + };
213 + }
214 + if (!segment.text.includes("[")) return segment;
215 + return {
216 + ...segment,
217 + text: definitions + segment.text,
218 + sourceOffset: segment.sourceOffset - definitions.length,
219 + };
220 +}
221 +
222 export function splitPullRequestBody(body: string): ReadonlyArray<PullRequestBodySegment> {
166 - return splitBodyAt(body, 0);
223 + const segments = splitBodyAt(body, 0);
224 + // One markdown segment is the whole body, which resolves its own references.
225 + const whole = segments.length === 1 && segments[0]!.kind === "markdown";
226 + if (whole || !body.includes("]:")) return segments;
227 + const definitions = referenceDefinitions(body);
228 + if (definitions.length === 0) return segments;
229 + const leading = `${definitions.join("\n")}\n\n`;
230 + return segments.map((segment) => withDefinitions(segment, leading));
231 }
232
233 /** `baseOffset` is where `body` starts in the whole body, so a table cell's edits write back. */
pnpm-lock.yaml
+12
@@ -1555,16 +1555,28 @@ importers:
1555 jszip:
1556 specifier: 3.10.1
1557 version: 3.10.1
1558 + remark-gfm:
1559 + specifier: 'catalog:'
1560 + version: 4.0.1(supports-color@8.1.1)
1561 + remark-parse:
1562 + specifier: 'catalog:'
1563 + version: 11.0.0(supports-color@8.1.1)
1564 three:
1565 specifier: 0.180.0
1566 version: 0.180.0
1567 unbash:
1568 specifier: ^5.0.0
1569 version: 5.0.0
1570 + unified:
1571 + specifier: 'catalog:'
1572 + version: 11.0.5
1573 devDependencies:
1574 '@effect/vitest':
1575 specifier: 4.1.2
1576 version: 4.1.2(patch_hash=299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7)(@astrojs/compiler-rs@0.5.1)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(typescript@6.0.3))(bufferutil@4.1.0)(effect@4.1.3)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.64.1)(terser@5.48.0)(typescript@6.0.3)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
1577 + '@types/mdast':
1578 + specifier: ^4.0.4
1579 + version: 4.0.4
1580 '@types/three':
1581 specifier: 0.180.0
1582 version: 0.180.0