TypeScript
fix(mobile): keep PRs readable when image URLs are malformed (#23466)
Victor Vogelc17a469bb7f2
diff --git a/apps/mobile/src/features/pull-requests/pullRequestDescription.test.ts b/apps/mobile/src/features/pull-requests/pullRequestDescription.test.tsindex 2936feb2366..6bad7d6bcfb 100644--- a/apps/mobile/src/features/pull-requests/pullRequestDescription.test.ts+++ b/apps/mobile/src/features/pull-requests/pullRequestDescription.test.ts@@ -3,6 +3,26 @@ import { parseMarkdownDocument } from "../../lib/markdownDocument";import { pullRequestDescriptionContent } from "./pullRequestDescriptionContent";describe("pull request description content", () => {+ it("ignores malformed image URLs while keeping the description and valid attachments", () => {+ const result = pullRequestDescriptionContent(+ parseMarkdownDocument(+ [+ "The pull request remains readable.",+ "",+ "",+ '<img src="https://:80" alt="Invalid port">',+ "",+ "",+ ].join("\n\n"),+ ),+ );++ expect(result.attachments).toEqual([+ { uri: "http://example.com/screenshot.png", caption: "Screenshot", kind: "image" },+ ]);+ expect(result.preview).toContain("The pull request remains readable.");+ });+it("keeps screenshot attachments and does not turn code examples or local paths into media", () => {const result = pullRequestDescriptionContent(parseMarkdownDocument(diff --git a/apps/mobile/src/features/pull-requests/pullRequestDescriptionContent.ts b/apps/mobile/src/features/pull-requests/pullRequestDescriptionContent.tsindex 2e2894f1404..8d0d3c93edd 100644--- a/apps/mobile/src/features/pull-requests/pullRequestDescriptionContent.ts+++ b/apps/mobile/src/features/pull-requests/pullRequestDescriptionContent.ts@@ -1,6 +1,15 @@import type { PullRequestBodySegment } from "@t3tools/client-runtime/pull-request-markdown";import type { MarkdownNode } from "../../lib/markdownDocument";+function isWebUrl(url: string): boolean {+ try {+ const parsed = new URL(url);+ return parsed.protocol === "https:" || parsed.protocol === "http:";+ } catch {+ return false;+ }+}+export function pullRequestDescriptionContent(document: MarkdownNode,media: readonly PullRequestBodySegment[] = [],@@ -12,7 +21,7 @@ export function pullRequestDescriptionContent(const text: string[] = [];const visit = (node: MarkdownNode) => {if (node.type === "image") {- if (node.url && /^https?:\/\//i.test(node.url) && !attachments.has(node.url)) {+ if (node.url && isWebUrl(node.url) && !attachments.has(node.url)) {attachments.set(node.url, {uri: node.url,caption: node.alt || node.title || "Image",