import { createContext, useContext } from 'react' import type { ComponentPropsWithoutRef } from 'react' import type { ExtraProps } from 'react-markdown' import { getLocalLinkPath, isExternalLink } from '@/lib/markdownLinks' const MarkdownLinkContext = createContext(false) /** Returns whether the current subtree is rendered inside a markdown link. */ export function useIsInsideMarkdownLink(): boolean { return useContext(MarkdownLinkContext) } type MarkdownLinkProps = ComponentPropsWithoutRef<'a'> & ExtraProps & { onOpenLocalPath?: (linkPath: string) => void } /** * Renders a markdown link. Local file links call `onOpenLocalPath` instead of navigating the app, * and external http(s) links open in a new tab. */ export function MarkdownLink({ href, children, onOpenLocalPath, ...rest }: MarkdownLinkProps) { delete (rest as Record).node const localPath = onOpenLocalPath ? getLocalLinkPath(href) : null const wrappedChildren = {children} if (localPath && onOpenLocalPath) { return ( { event.preventDefault() onOpenLocalPath(localPath) }} > {wrappedChildren} ) } if (isExternalLink(href)) { return {wrappedChildren} } return {wrappedChildren} }