import * as React from 'react' ; import { Fn } from '@cometjs/core' ; import { useSiteMetadata } from './siteMetadata' ; type InternalLink = { t : 'Internal' ; pathname : string ; } ; type ExternalLink = { t : 'External' ; url : URL ; } ; export type LinkType = InternalLink | ExternalLink ; export function isInternalLink ( link ) { return link . t === 'Internal' ; } export function isExternalLink ( link ) { return link . t === 'External' ; } export function mapLink < RInternal , RExternal > ( link , fn ) { if ( isInternalLink ( link ) ) { return Fn . range ( fn . Internal , link ) ; } else { return Fn . range ( fn . External , link ) ; } } export function parseLink ( urlString , baseOrigin ) { const baseUrl = new URL ( baseOrigin ) ; const url = new URL ( urlString , baseOrigin ) ; if ( baseUrl . origin === url . origin ) { return { t : 'Internal' , pathname : url . pathname } ; } else { return { t : 'External' , url } ; } } export function useLinkParser ( ) { const siteMetadata = useSiteMetadata ( ) ; return React . useMemo ( ( ) => { return ( urlString ) => parseLink ( urlString , siteMetadata . siteUrl ) ; } , [ siteMetadata ] ) ; } export function useLink ( urlString ) { const parseLink = useLinkParser ( ) ; return parseLink ( urlString ) ; }