Skip to content

Commit 85d19fd

Browse files
committed
fix(web): preserve code and break spacing in PR previews
1 parent dc20ec0 commit 85d19fd

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎apps/web/src/components/pullRequest/pullRequestMarkdown.logic.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ describe("pull request body segmentation", () => {
146146
});
147147

148148
describe("resolved mention previews", () => {
149+
it.each(["```\n[!NOTE]\n```", " [!NOTE]", "`[!NOTE]`"])(
150+
"preserves literal alert markers in code: %s",
151+
(body) => expect(pullRequestMarkdownPreview(body)).toBe("[!NOTE]"),
152+
);
153+
it.each(["<br>", "<br/>", "<BR />", '<br title="break">', '<br title="a > b">'])(
154+
"separates words across HTML breaks: %s",
155+
(tag) => expect(pullRequestMarkdownPreview(`Review:${tag}@Alex`)).toBe("Review: @Alex"),
156+
);
149157
it("hides alert markers just like unresolved comment previews", () => {
150158
expect(pullRequestMarkdownPreview("> [!NOTE]\n> @Alex Smith done")).toBe("@Alex Smith done");
151159
});

‎apps/web/src/components/pullRequest/pullRequestMarkdown.logic.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
import remarkParse from "remark-parse";
2-
import { unified } from "unified";
2+
import { unified, type Plugin } from "unified";
33

4+
import { remarkGithubAlerts } from "~/markdown-github-alerts";
45
import {
56
findAndReplaceText,
67
type MarkdownNode,
78
type TextMatch,
89
} from "~/vendor/mdast-find-and-replace";
910

10-
const previewParser = unified().use(remarkParse).freeze();
11+
const previewParser = unified()
12+
.use(remarkParse)
13+
.use(remarkGithubAlerts as Plugin)
14+
.freeze();
1115

1216
/** A single-line preview that preserves literal characters in resolved mention names. */
1317
export function pullRequestMarkdownPreview(body: string) {
1418
const text = (node: MarkdownNode & { alt?: string | null }): string => {
1519
if (node.type === "break") return " ";
16-
if (node.type === "html") return "";
20+
if (node.type === "html") {
21+
return /^<br(?:\s|\/?>)/iu.test(node.value ?? "") ? " " : "";
22+
}
1723
if (node.type === "image" || node.type === "imageReference") return node.alt ?? "";
1824
if (node.value !== undefined) return node.value;
1925
const separator = ["root", "list", "listItem", "blockquote"].includes(node.type) ? " " : "";
2026
return node.children?.map(text).join(separator) ?? "";
2127
};
22-
const prose = body.replace(/^\s*>?\s*\[!\w+\]\s*$/gmu, "");
23-
return text(previewParser.parse(prose)).replace(/\s+/gu, " ").trim();
28+
return text(previewParser.runSync(previewParser.parse(body)))
29+
.replace(/\s+/gu, " ")
30+
.trim();
2431
}
2532

2633
/** `id` is positional on purpose: the same attachment can be embedded twice in one body. */

0 commit comments

Comments
 (0)