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