Skip to content

Commit d0d4750

Browse files
committed
fix(client-runtime): preserve table content outside media cells
1 parent 5e6f5b2 commit d0d4750

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

‎packages/client-runtime/src/pullRequestMarkdown.test.ts‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,28 @@ describe("media on custom hosts", () => {
167167
]);
168168
});
169169

170-
it("keeps the caption of a media table instead of dropping it", () => {
171-
const segments = splitPullRequestBody(
172-
"<table>\n<caption>Before upgrading</caption>\n<tr><td>\n\nhttps://example.com/a.mp4\n\n</td></tr>\n</table>",
173-
);
174-
expect(segments.some((segment) => segment.kind === "table")).toBe(false);
175-
expect(segments).toContainEqual(
176-
expect.objectContaining({
177-
kind: "markdown",
178-
text: expect.stringContaining("Before upgrading"),
179-
}),
180-
);
181-
expect(segments).toContainEqual(
182-
expect.objectContaining({
183-
kind: "attachment",
184-
url: "https://example.com/a.mp4",
185-
media: "video",
186-
}),
187-
);
188-
});
170+
it.each(["<caption>Before upgrading</caption>", "Before upgrading", "<tr>Before upgrading</tr>"])(
171+
"keeps table content outside cells: %s",
172+
(extra) => {
173+
const segments = splitPullRequestBody(
174+
`<table>\n${extra}\n<tr><td>\n\nhttps://example.com/a.mp4\n\n</td></tr>\n</table>`,
175+
);
176+
expect(segments.some((segment) => segment.kind === "table")).toBe(false);
177+
expect(segments).toContainEqual(
178+
expect.objectContaining({
179+
kind: "markdown",
180+
text: expect.stringContaining("Before upgrading"),
181+
}),
182+
);
183+
expect(segments).toContainEqual(
184+
expect.objectContaining({
185+
kind: "attachment",
186+
url: "https://example.com/a.mp4",
187+
media: "video",
188+
}),
189+
);
190+
},
191+
);
189192
});
190193

191194
describe("change request reference autolinks", () => {

‎packages/client-runtime/src/pullRequestMarkdown.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type PullRequestBodySegment =
1515
readonly rows: ReadonlyArray<ReadonlyArray<PullRequestBodyTableCell>>;
1616
};
1717

18-
export type PullRequestBodyTableCell = {
18+
type PullRequestBodyTableCell = {
1919
readonly header: boolean;
2020
readonly segments: ReadonlyArray<PullRequestBodySegment>;
2121
};
@@ -80,7 +80,13 @@ function mediaTableAt(
8080
}
8181
if (!TABLE_END_PATTERN.test(lines[end]!)) return null;
8282
const html = lines.slice(start, end + 1).join("\n");
83-
if (/<caption\b/iu.test(html) || (html.match(TABLE_TAG_PATTERN)?.length ?? 0) > 1) return null;
83+
if ((html.match(TABLE_TAG_PATTERN)?.length ?? 0) > 1) return null;
84+
// Only cell content is represented; leave other content in Markdown rather than dropping it.
85+
const outsideCells = html
86+
.replace(TABLE_ROW_PATTERN, (_match, row: string) => row.replace(TABLE_CELL_PATTERN, ""))
87+
.replace(/<\/?(?:table|thead|tbody|tfoot)\b[^>]*>/giu, "")
88+
.trim();
89+
if (outsideCells.length > 0) return null;
8490
const rows = Array.from(html.matchAll(TABLE_ROW_PATTERN), ([, row]) =>
8591
Array.from(row!.matchAll(TABLE_CELL_PATTERN), ([, tag, content]) => ({
8692
header: tag!.toLowerCase() === "th",

0 commit comments

Comments
 (0)