Open History to see the last commit
1import {
2 changeRequestReferenceAutolinker,
3 resolveChangeRequestMediaUrl,
4} from "@t3tools/client-runtime/pull-request-markdown";
5import {
6 sourceControlClients,
7 type ReferenceAutolink,
8} from "@t3tools/client-runtime/source-control-clients";
9import {
10 CHAT_MARKDOWN_REHYPE_PLUGINS,
11 CHAT_MARKDOWN_REMARK_PLUGINS,
12} from "@t3tools/shared/markdownPipeline";
13import type { Nodes } from "hast";
14import remarkParse from "remark-parse";
15import remarkRehype from "remark-rehype";
16import { unified, type PluggableList } from "unified";
17
18export {
19 splitPullRequestBody,
20 type PullRequestBodySegment,
21} from "@t3tools/client-runtime/pull-request-markdown";
22
23type MarkdownNode = {
24 type: string;
25 value?: string;
26 children?: MarkdownNode[];
27 url?: string;
28 data?: unknown;
29};
30
31const AUTOLINK_IGNORED_TYPES = new Set(["link", "linkReference"]);
32
33export function remarkPullRequestAutolinks(options: {
34 readonly autolinks: (text: string) => ReadonlyArray<ReferenceAutolink>;
35}) {
36 const visit = (node: MarkdownNode): void => {
37 const children = node.children;
38 if (children === undefined) return;
39 for (let index = 0; index < children.length; index += 1) {
40 const child = children[index]!;
41 if (AUTOLINK_IGNORED_TYPES.has(child.type)) continue;
42 if (child.type !== "text" || child.value === undefined) {
43 visit(child);
44 continue;
45 }
46 const value = child.value;
47 const links = options.autolinks(value);
48 if (links.length === 0) continue;
49 const replacements: MarkdownNode[] = [];
50 let start = 0;
51 for (const link of links) {
52 if (start < link.index) {
53 replacements.push({ type: "text", value: value.slice(start, link.index) });
54 }
55 replacements.push({
56 type: "link",
57 url: link.url,
58 data: { hProperties: { dataPullRequestAutolink: link.kind } },
59 children: [{ type: "text", value: link.label }],
60 });
61 start = link.index + link.text.length;
62 }
63 if (start < value.length) replacements.push({ type: "text", value: value.slice(start) });
64 children.splice(index, 1, ...replacements);
65 index += replacements.length - 1;
66 }
67 };
68 return (tree: MarkdownNode) => visit(tree);
69}
70
71function remarkChangeRequestMediaUrls(options: { readonly changeRequestUrl: string }) {
72 const visit = (node: MarkdownNode): void => {
73 if ((node.type === "image" || node.type === "definition") && node.url !== undefined) {
74 node.url = resolveChangeRequestMediaUrl(node.url, options.changeRequestUrl);
75 }
76 node.children?.forEach(visit);
77 };
78 return (tree: MarkdownNode) => visit(tree);
79}
80
81const NO_PLUGINS: PluggableList = [];
82const markdownPlugins = new Map<string, PluggableList>();
83
84/**
85 * One plugin list per repository, so every comment of a pull request shares one markdown
86 * renderer and its parse cache instead of parsing again on each render.
87 */
88export function pullRequestMarkdownPlugins(
89 changeRequest: { readonly provider: string; readonly url: string } | null,
90): PluggableList {
91 if (changeRequest === null) return NO_PLUGINS;
92 const repositoryUrl = sourceControlClients.parseChangeRequestUrl(changeRequest.url)?.link
93 .repositoryUrl;
94 if (repositoryUrl === undefined) return NO_PLUGINS;
95 const key = `${changeRequest.provider}\u0000${repositoryUrl}`;
96 let plugins = markdownPlugins.get(key);
97 if (plugins === undefined) {
98 const autolinks = changeRequestReferenceAutolinker(changeRequest);
99 plugins = [[remarkChangeRequestMediaUrls, { changeRequestUrl: repositoryUrl }]];
100 if (autolinks) plugins.push([remarkPullRequestAutolinks, { autolinks }]);
101 markdownPlugins.set(key, plugins);
102 }
103 return plugins;
104}
105
106const 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
113const 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
140function 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
150export function pullRequestCommentPreview(body: string): string {
151 const tree = commentPreviewProcessor.runSync(commentPreviewProcessor.parse(body), body);
152 return commentPreviewText(tree).replace(/\s+/gu, " ").trim();
153}