Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestDescriptionContent.ts

42 lines1.5 KB

1import type { PullRequestBodySegment } from "@t3tools/client-runtime/pull-request-markdown";
2import type { MarkdownNode } from "../../lib/markdownDocument";
3​
4export function pullRequestDescriptionContent(
5 document: MarkdownNode,
6 media: readonly PullRequestBodySegment[] = [],
7) {
8 const attachments = new Map<
9 string,
10 { readonly uri: string; readonly caption: string; readonly kind: "image" | "video" | "unknown" }
11 >();
12 const text: string[] = [];
13 const visit = (node: MarkdownNode) => {
14 if (node.type === "image") {
15 if (node.url && /^https?:\/\//i.test(node.url) && !attachments.has(node.url)) {
16 attachments.set(node.url, {
17 uri: node.url,
18 caption: node.alt || node.title || "Image",
19 kind: "image",
20 });
21 }
22 return;
23 }
24 if (node.type === "html") return;
25 if ("value" in node) text.push(node.value);
26 if ("children" in node) node.children.forEach(visit);
27 if (["paragraph", "heading", "listItem", "code"].includes(node.type)) text.push("\n");
28 if (node.type === "break") text.push(" ");
29 };
30 visit(document);
31 for (const segment of media) {
32 if (segment.kind === "attachment" && !attachments.has(segment.url)) {
33 const name = new URL(segment.url).pathname.split("/").pop();
34 attachments.set(segment.url, {
35 uri: segment.url,
36 caption: segment.media === "video" ? "Video" : name || "Image",
37 kind: segment.media,
38 });
39 }
40 }
41 return { preview: text.join("").trim(), attachments: [...attachments.values()] };