fix: stop link component opening internal page in new tab
All checks were successful
Docker / build-and-push-image (push) Successful in 1m16s

This commit is contained in:
Troy 2025-05-29 12:19:16 +01:00
parent 9346a78c27
commit 237eeb4cac
Signed by: troy
GPG key ID: DFC06C02ED3B4711
5 changed files with 166 additions and 384 deletions

View file

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