Files

41 lines
1.1 KiB
TypeScript

"use client";
import type { AnchorHTMLAttributes } from "react";
import { usePostLinkNewTab } from "@/components/PostLinkProvider";
type MdLinkProps = AnchorHTMLAttributes<HTMLAnchorElement>;
function isExternalHref(href?: string): boolean {
if (!href) return false;
if (href.startsWith("/") || href.startsWith("#") || href.startsWith("?")) {
return false;
}
if (href.startsWith("mailto:") || href.startsWith("tel:")) return false;
try {
if (typeof window !== "undefined") {
const url = new URL(href, window.location.origin);
return url.origin !== window.location.origin;
}
} catch {
/* 相对或非法 URL 当站内 */
}
return /^https?:\/\//i.test(href);
}
/** Markdown 链接:外链是否新开由站点设置 post_link_new_tab 控制 */
export default function MdLink({ href, children, ...rest }: MdLinkProps) {
const openInNewTab = usePostLinkNewTab();
const external = isExternalHref(href);
return (
<a
{...rest}
href={href}
{...(external && openInNewTab
? { target: "_blank", rel: "noopener noreferrer" }
: {})}
>
{children}
</a>
);
}