Open History to see the last commit
1import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-references";
2import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
3import type { Nodes } from "hast";
4
5import {
6 splitPullRequestBody as splitBody,
7 type PullRequestBodySegment,
8} from "@t3tools/client-runtime/pull-request-markdown";
9import {
10 markdownReferenceDefinitions,
11 wordsHast,
12 type MarkdownExtensions,
13} from "~/markdown/wordsHast";
14
15export type { PullRequestBodySegment };
16
17export function splitPullRequestBody(body: string): ReadonlyArray<PullRequestBodySegment> {
18 return splitBody(body, markdownReferenceDefinitions);
19}
20
21const NO_EXTENSIONS: MarkdownExtensions = {};
22const markdownExtensions = new Map<string, MarkdownExtensions>();
23
24/**
25 * One extension object per repository, so every comment of a pull request shares one markdown
26 * renderer and its parse cache instead of parsing again on each render.
27 */
28export function pullRequestMarkdownExtensions(
29 changeRequest: { readonly provider: string; readonly url: string } | null,
30): MarkdownExtensions {
31 if (changeRequest === null) return NO_EXTENSIONS;
32 const repositoryUrl = sourceControlClients.parseChangeRequestUrl(changeRequest.url)?.link
33 .repositoryUrl;
34 if (repositoryUrl === undefined) return NO_EXTENSIONS;
35 const key = `${changeRequest.provider}\u0000${repositoryUrl}`;
36 let extensions = markdownExtensions.get(key);
37 if (extensions === undefined) {
38 extensions = {
39 changeRequest: {
40 repositoryUrl,
41 autolinks: changeRequestReferenceAutolinker(changeRequest),
42 },
43 githubEmoji: changeRequest.provider === "github",
44 };
45 markdownExtensions.set(key, extensions);
46 }
47 return extensions;
48}
49
50const PREVIEW_MARKDOWN = {
51 math: false,
52 breaks: false,
53 escapeHtml: false,
54 insights: false,
55 extensions: NO_EXTENSIONS,
56};
57const GITHUB_PREVIEW_MARKDOWN = {
58 ...PREVIEW_MARKDOWN,
59 extensions: { githubEmoji: true },
60};
61
62const PREVIEW_BLOCK_TAGS = new Set([
63 "blockquote",
64 "br",
65 "dd",
66 "details",
67 "div",
68 "dl",
69 "dt",
70 "h1",
71 "h2",
72 "h3",
73 "h4",
74 "h5",
75 "h6",
76 "hr",
77 "li",
78 "ol",
79 "p",
80 "pre",
81 "summary",
82 "table",
83 "td",
84 "th",
85 "tr",
86 "ul",
87]);
88
89function commentPreviewText(node: Nodes): string {
90 if (node.type === "text") return node.value;
91 if (node.type === "element" && node.tagName === "img") {
92 return typeof node.properties.alt === "string" ? node.properties.alt : "";
93 }
94 if (!("children" in node)) return "";
95 const text = node.children.map(commentPreviewText).join("");
96 return node.type === "element" && PREVIEW_BLOCK_TAGS.has(node.tagName) ? ` ${text} ` : text;
97}
98
99export function pullRequestCommentPreview(body: string, provider: string): string {
100 const options = provider === "github" ? GITHUB_PREVIEW_MARKDOWN : PREVIEW_MARKDOWN;
101 return commentPreviewText(wordsHast(body, options)).replace(/\s+/gu, " ").trim();
102}