import URL from "url"; import React from "react"; import { Link as GatsbyLink } from "gatsby"; import { OutboundLink } from "gatsby-plugin-google-gtag"; import type { Maybe } from "../types"; import { notEmpty, type RequireOnlyOne } from "../utils"; import "../styles/Link.css"; export type ISiteLinkProps = React.HTMLProps; const ensureTrailingSlash = (url: string): string => { // @ts-ignore const _url = new URL.parse(url); // eslint-disable-next-line no-useless-escape _url.pathname = _url.pathname.replace(/^\/?([^\\/]+(?:\/[^\/]+)*)\/?$/, "/$1/") || "/"; return `${_url.pathname ?? ""}${_url.search ?? ""}${_url.hash ?? ""}`; }; export const SiteLink = React.forwardRef( ({ ...props }, ref) => { const internal = /^\.?\/(?!\/)/.test(props.href || ""); if ((props.href || "").startsWith("#")) { return ; } if (internal) { // @ts-ignore props.to = ensureTrailingSlash(props.href as string); props.ref = ref; } const LinkComponent: React.ElementType = internal ? GatsbyLink : OutboundLink; // @ts-expect-error return ; }, ); interface ILinkObject { readonly slug: string; readonly title: Maybe; readonly handle: Maybe; } type MyLinkObject = RequireOnlyOne; type ILinkListType = readonly MyLinkObject[]; interface ILinkListProps { links: ILinkListType; Component?: React.FC; componentProps?: React.ComponentProps; } export const LinkList: React.FC = ({ links, Component = SiteLink, componentProps, }) => { if (!links || links.length < 1) { return <>; } return ( <> {links.filter(notEmpty).map((obj: MyLinkObject, idx: number) => [ idx > 0 && ", ", {obj.title || obj.handle} , ])} ); }; interface ILinkBoxProps { href: string; linkProps?: React.ComponentProps; boxProps?: React.HTMLProps & { style?: any }; } export const LinkBox: React.FC = ({ children, href, linkProps = {}, boxProps = {}, }) => (
{children}
);