From 8f19cb6a208f2be0633a2e626475775515f70089 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 20 Jul 2026 17:14:41 +0200 Subject: [PATCH] Consolidate external link parsing for PR #4161 Use one HTTP(S) host resolver for chat-link favicon rendering and context-menu eligibility. Co-authored-by: codex --- apps/web/src/components/ChatMarkdown.tsx | 20 ++++++------------ .../chat/externalLinkContextMenu.test.ts | 21 ++++++++++--------- .../chat/externalLinkContextMenu.ts | 8 ++++--- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 36fab2d5da..fac3bf7d24 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -42,7 +42,10 @@ import remarkGfm from "remark-gfm"; import { renderSkillInlineMarkdownChildren } from "./chat/SkillInlineText"; import { CHAT_FILE_TAG_CHIP_CLASS_NAME, FileTagChipContent } from "./chat/FileTagChip"; import { PierreEntryIcon } from "./chat/PierreEntryIcon"; -import { isExternalWebLink, showExternalLinkContextMenu } from "./chat/externalLinkContextMenu"; +import { + resolveExternalWebLinkHost, + showExternalLinkContextMenu, +} from "./chat/externalLinkContextMenu"; import { hasSpecificPierreIconForFileName, syntheticFileNameForLanguageId } from "../pierre-icons"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; import { Button } from "./ui/button"; @@ -833,17 +836,6 @@ const MARKDOWN_LINK_FAVICON_CLASS_NAME = "block size-full shrink-0 select-none"; /** Hosts whose favicon request already failed this session — skip straight to the globe. */ const failedFaviconHosts = new Set(); -function resolveExternalLinkHost(href: string | undefined): string | null { - if (!href) return null; - try { - const url = new URL(href); - if (url.protocol !== "http:" && url.protocol !== "https:") return null; - return url.hostname || null; - } catch { - return null; - } -} - const MarkdownLinkFavicon = memo(function MarkdownLinkFavicon({ host }: { host: string }) { const [failedHost, setFailedHost] = useState(null); return ( @@ -1395,7 +1387,7 @@ function ChatMarkdown({ const normalizedHref = href ? normalizeMarkdownLinkHrefKey(href) : ""; const fileLinkMeta = normalizedHref ? markdownFileLinkMetaByHref.get(normalizedHref) : null; if (!fileLinkMeta) { - const faviconHost = resolveExternalLinkHost(href); + const faviconHost = resolveExternalWebLinkHost(href); const isSameDocumentLink = href?.startsWith("#") ?? false; const onClick = props.onClick; const canOpenInPreview = Boolean(threadRef) && isPreviewSupportedInRuntime(); @@ -1412,7 +1404,7 @@ function ChatMarkdown({ } }} onContextMenu={(event) => { - if (!canOpenInPreview || !href || !isExternalWebLink(href)) return; + if (!canOpenInPreview || !href || !faviconHost) return; event.preventDefault(); event.stopPropagation(); const api = readLocalApi(); diff --git a/apps/web/src/components/chat/externalLinkContextMenu.test.ts b/apps/web/src/components/chat/externalLinkContextMenu.test.ts index 1f2b6b5ae0..64935d53e4 100644 --- a/apps/web/src/components/chat/externalLinkContextMenu.test.ts +++ b/apps/web/src/components/chat/externalLinkContextMenu.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from "vite-plus/test"; -import { isExternalWebLink, showExternalLinkContextMenu } from "./externalLinkContextMenu"; +import { resolveExternalWebLinkHost, showExternalLinkContextMenu } from "./externalLinkContextMenu"; function createHarness(selection: "open-in-preview" | "open-external" | "copy-link" | null) { const showContextMenu = vi.fn().mockResolvedValue(selection); @@ -114,14 +114,15 @@ describe("external chat link context menu", () => { }); it.each([ - ["https://example.com", true], - ["http://localhost:3000/path", true], - ["#details", false], - ["mailto:hello@example.com", false], - ["file:///tmp/example.txt", false], - ["javascript:void(0)", false], - ["not a URL", false], - ])("classifies %s as an external web link: %s", (href, expected) => { - expect(isExternalWebLink(href)).toBe(expected); + ["https://example.com", "example.com"], + ["http://localhost:3000/path", "localhost"], + ["#details", null], + ["mailto:hello@example.com", null], + ["file:///tmp/example.txt", null], + ["javascript:void(0)", null], + ["not a URL", null], + [undefined, null], + ])("resolves the external web-link host for %s as %s", (href, expected) => { + expect(resolveExternalWebLinkHost(href)).toBe(expected); }); }); diff --git a/apps/web/src/components/chat/externalLinkContextMenu.ts b/apps/web/src/components/chat/externalLinkContextMenu.ts index 020c6169ff..398ca40da5 100644 --- a/apps/web/src/components/chat/externalLinkContextMenu.ts +++ b/apps/web/src/components/chat/externalLinkContextMenu.ts @@ -36,12 +36,14 @@ interface ShowExternalLinkContextMenuOptions { ) => void; } -export function isExternalWebLink(href: string): boolean { +export function resolveExternalWebLinkHost(href: string | undefined): string | null { + if (!href) return null; try { const url = new URL(href); - return (url.protocol === "http:" || url.protocol === "https:") && url.hostname.length > 0; + if (url.protocol !== "http:" && url.protocol !== "https:") return null; + return url.hostname || null; } catch { - return false; + return null; } }