Open History to see the last commit
1import { unified } from "unified";
2import remarkParse from "remark-parse";
3import remarkGfm from "remark-gfm";
4
5type MarkdownNode = {
6 readonly type: string;
7 readonly position?:
8 | {
9 readonly start: { readonly offset?: number | undefined };
10 readonly end: { readonly offset?: number | undefined };
11 }
12 | undefined;
13 readonly children?: readonly MarkdownNode[] | undefined;
14};
15
16const parser = unified().use(remarkParse).use(remarkGfm);
17const reference =
18 /(?<![\w/\\])(?:([\w.-]+\/[\w.-]+|[\w-]+)?#([1-9]\d*)|(?:([\w.-]+\/[\w.-]+|[\w-]+)@)?([a-f\d]{7,40})(?![\w])|@([a-z\d](?:[a-z\d-]*[a-z\d])?(?:\/[a-z\d][a-z\d-]*)?))(?![\w])/gi;
19
20export function githubMarkdownLinks(markdown: string, pullRequestUrl: string | null): string {
21 if (!pullRequestUrl || !/[#@]|\b[a-f\d]{7,40}\b/i.test(markdown)) return markdown;
22 let url: URL;
23 try {
24 url = new URL(pullRequestUrl);
25 } catch {
26 return markdown;
27 }
28 const [owner, repo] = url.pathname.split("/").filter(Boolean);
29 if (!owner || !repo || !/^https?:$/.test(url.protocol)) return markdown;
30 const edits: { start: number; end: number; value: string }[] = [];
31 const repository = (qualifier: string | undefined) =>
32 qualifier?.includes("/") ? qualifier : qualifier ? `${qualifier}/${repo}` : `${owner}/${repo}`;
33 const visit = (node: MarkdownNode) => {
34 if (
35 [
36 "link",
37 "linkReference",
38 "image",
39 "imageReference",
40 "code",
41 "inlineCode",
42 "html",
43 "definition",
44 ].includes(node.type)
45 )
46 return;
47 if (node.type === "text") {
48 const start = node.position?.start.offset;
49 const end = node.position?.end.offset;
50 if (start === undefined || end === undefined) return;
51 const source = markdown.slice(start, end);
52 for (const match of source.matchAll(reference)) {
53 const [label, issueRepo, number, commitRepo, sha, mention] = match;
54 if (sha && !/[a-f]/i.test(sha)) continue;
55 const path = number
56 ? `${repository(issueRepo)}/issues/${number}`
57 : sha
58 ? `${repository(commitRepo)}/commit/${sha}`
59 : mention?.includes("/")
60 ? `orgs/${mention.split("/")[0]}/teams/${mention.split("/")[1]}`
61 : mention;
62 if (!path) continue;
63 edits.push({
64 start: start + match.index,
65 end: start + match.index + label.length,
66 value: `[${label}](${url.origin}/${path})`,
67 });
68 }
69 } else if ("children" in node) {
70 let protectedHtmlDepth = 0;
71 for (const child of node.children ?? []) {
72 if (child.type === "html") {
73 const start = child.position?.start.offset;
74 const end = child.position?.end.offset;
75 const tag = start === undefined || end === undefined ? "" : markdown.slice(start, end);
76 for (const match of tag.matchAll(/<(\/)?(?:a|code|pre|script|style)(?=[\s>])[^>]*>/gi)) {
77 protectedHtmlDepth = Math.max(0, protectedHtmlDepth + (match[1] ? -1 : 1));
78 }
79 } else if (protectedHtmlDepth === 0) visit(child);
80 }
81 }
82 };
83 visit(parser.parse(markdown));
84 for (let index = edits.length - 1; index >= 0; index--) {
85 const edit = edits[index]!;
86 markdown = markdown.slice(0, edit.start) + edit.value + markdown.slice(edit.end);
87 }
88 return markdown;
89}