44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
import type { PullRequestBodySegment } from "@t3tools/client-runtime/pull-request-markdown";
|
|
import type { MarkdownNode } from "../../lib/markdownDocument";
|
|
|
|
/** Read media from the parsed document so code examples never become attachments. */
|
|
export function pullRequestDescriptionContent(
|
|
document: MarkdownNode,
|
|
media: readonly PullRequestBodySegment[] = [],
|
|
) {
|
|
const attachments = new Map<
|
|
string,
|
|
{ readonly uri: string; readonly caption: string; readonly kind: "image" | "video" | "unknown" }
|
|
>();
|
|
const text: string[] = [];
|
|
const visit = (node: MarkdownNode) => {
|
|
if (node.type === "image") {
|
|
if (node.url && /^https?:\/\//i.test(node.url) && !attachments.has(node.url)) {
|
|
attachments.set(node.url, {
|
|
uri: node.url,
|
|
caption: node.alt || node.title || "Image",
|
|
kind: "image",
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
if (node.type === "html") return;
|
|
if ("value" in node) text.push(node.value);
|
|
if ("children" in node) node.children.forEach(visit);
|
|
if (["paragraph", "heading", "listItem", "code"].includes(node.type)) text.push("\n");
|
|
if (node.type === "break") text.push(" ");
|
|
};
|
|
visit(document);
|
|
for (const segment of media) {
|
|
if (segment.kind === "attachment" && !attachments.has(segment.url)) {
|
|
const name = new URL(segment.url).pathname.split("/").pop();
|
|
attachments.set(segment.url, {
|
|
uri: segment.url,
|
|
caption: segment.media === "video" ? "Video" : name || "Image",
|
|
kind: segment.media,
|
|
});
|
|
}
|
|
}
|
|
return { preview: text.join("").trim(), attachments: [...attachments.values()] };
|
|
}
|