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 <[email protected]>
This commit is contained in:
1 parent
061ad09329
commit
8f19cb6a20
3 files changed
+22
-27
No files matched your search
@@ -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<string>();
|
||||
|
||||
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<string | null>(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();
|
||||
|
||||
@@ -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:[email protected]", 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:[email protected]", 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);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user