fix: open external links in new tab with rel=noopener noreferrer

Markdown links pointing to external URLs (http/https) now open in a
new tab with target=_blank and rel='noopener noreferrer' for security.
Internal/relative links are unaffected.
This commit is contained in:
Nicolas Varrot
2026-02-13 04:42:39 +00:00
parent ad6f81d4d7
commit dfbfc375da

View File

@@ -150,7 +150,21 @@ function MarkdownImage(props: React.ImgHTMLAttributes<HTMLImageElement>) {
return <ImageBlock src={props.src || ''} alt={props.alt} />;
}
const markdownComponents = { pre: CodeBlock, img: MarkdownImage };
function MarkdownLink(props: React.AnchorHTMLAttributes<HTMLAnchorElement>) {
const { href, children, ...rest } = props;
const isExternal = href && /^https?:\/\//.test(href);
return (
<a
href={href}
{...(isExternal ? { target: '_blank', rel: 'noopener noreferrer' } : {})}
{...rest}
>
{children}
</a>
);
}
const markdownComponents = { pre: CodeBlock, img: MarkdownImage, a: MarkdownLink };
function renderTextBlocks(blocks: MessageBlock[]) {
return getTextBlocks(blocks).map((block, i) => (