fix(web): extract single tryGetHostname helper (DRY, unified fallback)

Fixes #1368

Previously,  was duplicated in 4 places with 3 subtly different fallback behaviors:
1. inline-citation.tsx: returned  on error
2. markdown-text.tsx: returned  on error
3. assistant-message.tsx: returned  on error
4. citation.tsx: returned  on error

Created canonical  in  that:
- Returns
- Strips  prefix from hostname
- Returns  on invalid URL (safest contract)

Updated all 4 call sites:
- inline-citation.tsx:  (preserves original fallback)
- markdown-text.tsx:  (preserves original fallback)
- assistant-message.tsx:  (drop-in, both return )
- citation.tsx:  (drop-in, both return )

Co-authored-by: guangyang1206 <guangyang1206@users.noreply.github.com>
This commit is contained in:
guangyang1206 2026-05-16 12:15:16 +08:00
parent 1119f557df
commit f096548a16
5 changed files with 24 additions and 38 deletions

View file

@ -23,6 +23,8 @@ import "katex/dist/katex.min.css";
import { toast } from "sonner";
import { processChildrenWithCitations } from "@/components/citations/citation-renderer";
import { Skeleton } from "@/components/ui/skeleton";
import { tryGetHostname } from "@/lib/url";
import {
Table,
TableBody,
@ -139,15 +141,6 @@ const MarkdownTextImpl = () => {
export const MarkdownText = memo(MarkdownTextImpl);
function extractDomain(url: string): string {
try {
const parsed = new URL(url);
return parsed.hostname.replace(/^www\./, "");
} catch {
return "";
}
}
// Canonical local-file virtual paths are mount-prefixed: /<mount>/<relative/path>
const LOCAL_FILE_PATH_REGEX = /^\/[a-z0-9_-]+\/[^\s`]+(?:\/[^\s`]+)*$/;
@ -288,7 +281,7 @@ function FilePathLink({ path, className }: { path: string; className?: string })
function MarkdownImage({ src, alt }: { src?: string; alt?: string }) {
if (!src) return null;
const domain = extractDomain(src);
const domain = tryGetHostname(src) ?? "";
return (
<div className="my-4 w-fit max-w-lg overflow-hidden rounded-2xl border bg-muted/30 select-none">