troylusty.com/src/components/Link.astro
Troy 237eeb4cac
All checks were successful
Docker / build-and-push-image (push) Successful in 1m16s
fix: stop link component opening internal page in new tab
2025-05-29 12:19:16 +01:00

23 lines
423 B
Text

---
import type { HTMLAttributes } from "astro/types";
interface Props extends HTMLAttributes<"a"> {
href: string;
}
const { href, ...rest } = Astro.props;
const isExternal =
/^(https?:)?\/\//.test(href) ||
href.startsWith("mailto:") ||
href.startsWith("tel:");
---
<a
href={href}
target={isExternal ? "_blank" : "_self"}
rel={isExternal ? "noopener noreferrer" : undefined}
{...rest}
>
<slot />
</a>