import React from 'react'; import type { Element } from 'hast'; import { useResolvedUrl, parseUrlScheme, mapSchemeToRoute } from './useResolvedUrl'; import { CodeBlockPlaceholder } from './CodeBlockPlaceholder'; export interface MarkdownLinkProps { node?: Element; href?: string; children?: React.ReactNode; className?: string; artifactRunId?: string; /** Existing link component to delegate to for standard URLs */ ExistingLink?: React.ComponentType; } /** * Markdown link component with support for custom URL schemes. * Handles artifact:, image:, store:, document://, and collection: URLs. */ export function MarkdownLink({ node, href, children, className, artifactRunId, ExistingLink, ...rest }: MarkdownLinkProps) { const rawHref = href || ''; const { scheme, path } = parseUrlScheme(rawHref); // For schemes that map directly to routes (store:, document://, collection:) const mappedRoute = mapSchemeToRoute(scheme, path); if (mappedRoute) { if (typeof ExistingLink === 'function') { return ( {children} ); } return ( {children} ); } // For standard URLs, delegate to existing component or render directly if (scheme === 'standard') { if (typeof ExistingLink === 'function') { return ( {children} ); } return ( {children} ); } // For artifact: and image: URLs, use the resolver return ( {children} ); } /** * Internal component for links that need async URL resolution */ function ResolvedLink({ rawHref, artifactRunId, className, children, rest, }: { rawHref: string; artifactRunId?: string; className?: string; children?: React.ReactNode; rest: Record; }) { const { url, isLoading, error, retry } = useResolvedUrl({ rawUrl: rawHref, artifactRunId, disposition: 'attachment', }); if (isLoading) { return ( ); } if (error) { return ( ); } return ( {children} ); }