|
1 | 1 | import remarkParse from "remark-parse"; |
2 | | -import { unified } from "unified"; |
| 2 | +import { unified, type Plugin } from "unified"; |
3 | 3 |
|
| 4 | +import { remarkGithubAlerts } from "~/markdown-github-alerts"; |
4 | 5 | import { |
5 | 6 | findAndReplaceText, |
6 | 7 | type MarkdownNode, |
7 | 8 | type TextMatch, |
8 | 9 | } from "~/vendor/mdast-find-and-replace"; |
9 | 10 |
|
10 | | -const previewParser = unified().use(remarkParse).freeze(); |
| 11 | +const previewParser = unified() |
| 12 | + .use(remarkParse) |
| 13 | + .use(remarkGithubAlerts as Plugin) |
| 14 | + .freeze(); |
11 | 15 |
|
12 | 16 | /** A single-line preview that preserves literal characters in resolved mention names. */ |
13 | 17 | export function pullRequestMarkdownPreview(body: string) { |
14 | 18 | const text = (node: MarkdownNode & { alt?: string | null }): string => { |
15 | 19 | 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 | + } |
17 | 23 | if (node.type === "image" || node.type === "imageReference") return node.alt ?? ""; |
18 | 24 | if (node.value !== undefined) return node.value; |
19 | 25 | const separator = ["root", "list", "listItem", "blockquote"].includes(node.type) ? " " : ""; |
20 | 26 | return node.children?.map(text).join(separator) ?? ""; |
21 | 27 | }; |
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(); |
24 | 31 | } |
25 | 32 |
|
26 | 33 | /** `id` is positional on purpose: the same attachment can be embedded twice in one body. */ |
|
0 commit comments