Files
nanobot/webui/src/components/WebLink.tsx
T
chengyongru 08ca3b4525 refactor(webui): use native link actions and remove website previews (#6391)
* refactor(webui): remove website previews and custom link menus

* docs(webui): focus guide on product-specific usage
2026-10-22 16:34:54 +08:00

22 lines
947 B
TypeScript

import type { ComponentPropsWithoutRef } from "react";
function parseWebLink(value: string): URL | null {
if (value.length > 8192 || value.includes("\\")
|| Array.from(value).some((char) => char.charCodeAt(0) <= 32 || char.charCodeAt(0) === 127)) return null;
try {
const url = new URL(value);
return /^https?:$/.test(url.protocol) && !url.username && !url.password ? url : null;
} catch {
return null;
}
}
export function WebLink({ href = "", children, ...props }: ComponentPropsWithoutRef<"a">) {
const url = parseWebLink(href);
// Keep the renderer's relative media/document and mail links unchanged.
const relative = !/^[a-z][a-z\d+.-]*:/i.test(href)
&& !href.includes("\\") && !Array.from(href).some((char) => char.charCodeAt(0) < 32);
return url || relative || /^(mailto:|tel:|#)/i.test(href)
? <a {...props} href={href} target="_blank" rel="noreferrer noopener">{children}</a> : <>{children}</>;
}