Skip to content

Commit f53b6e0

Browse files
fix(web): show readable text in collapsed PR comments
1 parent 240446e commit f53b6e0

3 files changed

Lines changed: 88 additions & 10 deletions

File tree

‎apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { pullRequestCommentPreview } from "./pullRequestMarkdown.logic";
12
import { useAtomValue } from "@effect/atom-react";
23
import {
34
capitalizeTerm,
@@ -305,14 +306,7 @@ function CollapsedComment({
305306
className="mt-2 block w-full truncate text-left text-xs text-muted-foreground hover:text-foreground"
306307
onClick={() => statusTriggerRef.current?.focus({ preventScroll: true })}
307308
>
308-
{body
309-
.replace(/<!--[\s\S]*?-->/gu, "")
310-
.replace(/^\s*>?\s*\[!\w+\]\s*$/gmu, "")
311-
.replace(/!?(\[([^\]]+)\])\([^)]*\)/gu, "$2")
312-
.replace(/^[\s>#*-]+/gmu, "")
313-
.replace(/[*`]/gu, "")
314-
.replace(/\s+/g, " ")
315-
.trim()}
309+
{pullRequestCommentPreview(body)}
316310
</CollapsibleTrigger>
317311
) : null}
318312
</div>

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import remarkParse from "remark-parse";
33
import { unified } from "unified";
44
import { describe, expect, it } from "vite-plus/test";
55

6-
import { remarkPullRequestAutolinks } from "./pullRequestMarkdown.logic";
6+
import { pullRequestCommentPreview, remarkPullRequestAutolinks } from "./pullRequestMarkdown.logic";
77

88
type Node = {
99
type: string;
@@ -44,3 +44,30 @@ describe("remarkPullRequestAutolinks", () => {
4444
]);
4545
});
4646
});
47+
48+
describe("pullRequestCommentPreview", () => {
49+
it.each([
50+
["<!-- hidden -->\n> [!WARNING]\n**Guard** [this](https://example.com).", "Guard this."],
51+
[
52+
'<a href="https://example.com"><img src="badge.svg" /></a>\n<strong>Tasks keep the wrong computer</strong>',
53+
"Tasks keep the wrong computer",
54+
],
55+
[
56+
"<details><summary>Review &amp; fixes</summary><p>First<br>Second</p></details>",
57+
"Review & fixes First Second",
58+
],
59+
['<p title="value > limit">Keep <strong>this</strong> readable.</p>', "Keep this readable."],
60+
[
61+
"Use `<div>` and `Promise<void>`.\n\n```html\n<span>example</span>\n```",
62+
"Use <div> and Promise<void>. <span>example</span>",
63+
],
64+
["![badge](https://example.com/badge.svg) **Review**", "badge Review"],
65+
[
66+
"Review :codex-file-citation{purpose='output'} safely.",
67+
"Review :codex-file-citation{purpose='output'} safely.",
68+
],
69+
[":codex-followup[Prepare **print** version]", ":codex-followup[Prepare **print** version]"],
70+
])("extracts readable text from %s", (body, expected) => {
71+
expect(pullRequestCommentPreview(body)).toBe(expected);
72+
});
73+
});

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import {
66
sourceControlClients,
77
type ReferenceAutolink,
88
} from "@t3tools/client-runtime/source-control-clients";
9-
import type { PluggableList } from "unified";
9+
import {
10+
CHAT_MARKDOWN_REHYPE_PLUGINS,
11+
CHAT_MARKDOWN_REMARK_PLUGINS,
12+
} from "@t3tools/shared/markdownPipeline";
13+
import type { Nodes } from "hast";
14+
import remarkParse from "remark-parse";
15+
import remarkRehype from "remark-rehype";
16+
import { unified, type PluggableList } from "unified";
1017

1118
export {
1219
splitPullRequestBody,
@@ -95,3 +102,53 @@ export function pullRequestMarkdownPlugins(
95102
}
96103
return plugins;
97104
}
105+
106+
const commentPreviewProcessor = unified()
107+
.use(remarkParse)
108+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
109+
.use(remarkRehype, { allowDangerousHtml: true })
110+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS)
111+
.freeze();
112+
113+
const PREVIEW_BLOCK_TAGS = new Set([
114+
"blockquote",
115+
"br",
116+
"dd",
117+
"details",
118+
"div",
119+
"dl",
120+
"dt",
121+
"h1",
122+
"h2",
123+
"h3",
124+
"h4",
125+
"h5",
126+
"h6",
127+
"hr",
128+
"li",
129+
"ol",
130+
"p",
131+
"pre",
132+
"summary",
133+
"table",
134+
"td",
135+
"th",
136+
"tr",
137+
"ul",
138+
]);
139+
140+
function commentPreviewText(node: Nodes): string {
141+
if (node.type === "text") return node.value;
142+
if (node.type === "element" && node.tagName === "img") {
143+
return typeof node.properties.alt === "string" ? node.properties.alt : "";
144+
}
145+
if (!("children" in node)) return "";
146+
const text = node.children.map(commentPreviewText).join("");
147+
return node.type === "element" && PREVIEW_BLOCK_TAGS.has(node.tagName) ? ` ${text} ` : text;
148+
}
149+
150+
export function pullRequestCommentPreview(body: string): string {
151+
const source = body.replace(/^\s*>?\s*\[!\w+\]\s*$/gmu, "");
152+
const tree = commentPreviewProcessor.runSync(commentPreviewProcessor.parse(source), source);
153+
return commentPreviewText(tree).replace(/\s+/gu, " ").trim();
154+
}

0 commit comments

Comments
 (0)