import { memo, useMemo } from "react"; import type { Components } from "react-markdown"; import ReactMarkdown from "react-markdown"; import rehypeHighlight from "rehype-highlight"; import remarkGfm from "remark-gfm"; import { readNativeApi } from "../nativeApi"; import { resolveMarkdownFileLinkTarget } from "../markdown-links"; import { preferredTerminalEditor } from "../terminal-links"; interface ChatMarkdownProps { text: string; cwd: string | undefined; } function ChatMarkdown({ text, cwd }: ChatMarkdownProps) { const markdownComponents = useMemo( () => ({ a({ node: _node, href, ...props }) { const targetPath = resolveMarkdownFileLinkTarget(href, cwd); if (!targetPath) { return ; } return ( { event.preventDefault(); event.stopPropagation(); const api = readNativeApi(); if (api) { void api.shell.openInEditor(targetPath, preferredTerminalEditor()); } else { console.warn("Native API not found. Unable to open file in editor."); } }} /> ); }, }), [cwd], ); return (
{text}
); } export default memo(ChatMarkdown);