import type { ComponentPropsWithoutRef } from 'react' import type { ExtraProps } from 'react-markdown' import { getLocalLinkPath, isExternalLink } from '@/lib/markdownLinks' 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 if (localPath && onOpenLocalPath) { return ( { event.preventDefault() onOpenLocalPath(localPath) }} > {children} ) } if (isExternalLink(href)) { return {children} } return {children} }