@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 59 lines 2.05 KB
Raw
1 import type { PullRequestBodySegment } from "@t3tools/client-runtime/pull-request-markdown";
2 import { msg } from "gt-react-native";
3 import type { MarkdownNode } from "../../lib/markdownDocument";
4 import { m, type MessageResolver } from "../../i18n/messages";
5
6 function isWebUrl(url: string): boolean {
7 try {
8 const parsed = new URL(url);
9 return parsed.protocol === "https:" || parsed.protocol === "http:";
10 } catch {
11 return false;
12 }
13 }
14
15 export function pullRequestDescriptionContent(
16 document: MarkdownNode,
17 media: readonly PullRequestBodySegment[] = [],
18 resolve: MessageResolver = m,
19 ) {
20 const attachments = new Map<
21 string,
22 { readonly uri: string; readonly caption: string; readonly kind: "image" | "video" | "unknown" }
23 >();
24 const text: string[] = [];
25 const visit = (node: MarkdownNode) => {
26 if (node.type === "image") {
27 if (node.url && isWebUrl(node.url) && !attachments.has(node.url)) {
28 attachments.set(node.url, {
29 uri: node.url,
30 caption: node.alt || node.title || resolve(msg("Image")),
31 kind: "image",
32 });
33 }
34 return;
35 }
36 if (node.type === "html") return;
37 if ("value" in node) text.push(node.value);
38 if ("children" in node) node.children.forEach(visit);
39 if (["paragraph", "heading", "listItem", "code"].includes(node.type)) text.push("\n");
40 if (node.type === "break") text.push(" ");
41 };
42 visit(document);
43 const collect = (segment: PullRequestBodySegment) => {
44 if (segment.kind === "table") {
45 segment.rows.flat().forEach((cell) => cell.segments.forEach(collect));
46 return;
47 }
48 if (segment.kind === "attachment" && !attachments.has(segment.url)) {
49 const name = new URL(segment.url).pathname.split("/").pop();
50 attachments.set(segment.url, {
51 uri: segment.url,
52 caption: segment.media === "video" ? resolve(msg("Video")) : name || resolve(msg("Image")),
53 kind: segment.media,
54 });
55 }
56 };
57 media.forEach(collect);
58 return { preview: text.join("").trim(), attachments: [...attachments.values()] };
59 }