Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ChatMarkdown.tsx

58 lines1.7 KB

1import { memo, useMemo } from "react";
2import type { Components } from "react-markdown";
3import ReactMarkdown from "react-markdown";
4import rehypeHighlight from "rehype-highlight";
5import remarkGfm from "remark-gfm";
6import { readNativeApi } from "../nativeApi";
7import { resolveMarkdownFileLinkTarget } from "../markdown-links";
8import { preferredTerminalEditor } from "../terminal-links";
9​
10interface ChatMarkdownProps {
11 text: string;
12 cwd: string | undefined;
14​
15function ChatMarkdown({ text, cwd }: ChatMarkdownProps) {
16 const markdownComponents = useMemo<Components>(
17 () => ({
18 a({ node: _node, href, ...props }) {
19 const targetPath = resolveMarkdownFileLinkTarget(href, cwd);
20 if (!targetPath) {
21 return <a {...props} href={href} target="_blank" rel="noreferrer" />;
22 }
23​
24 return (
25 <a
26 {...props}
27 href={href}
28 onClick={(event) => {
29 event.preventDefault();
30 event.stopPropagation();
31 const api = readNativeApi();
32 if (api) {
33 void api.shell.openInEditor(targetPath, preferredTerminalEditor());
34 } else {
35 console.warn("Native API not found. Unable to open file in editor.");
36 }
37 }}
38 />
39 );
40 },
41 }),
42 [cwd],
43 );
44​
45 return (
46 <div className="chat-markdown w-full min-w-0 text-sm leading-relaxed text-foreground/80">
47 <ReactMarkdown
48 remarkPlugins={[remarkGfm]}
49 rehypePlugins={[[rehypeHighlight, { detect: true, ignoreMissing: true }]]}
50 components={markdownComponents}
51 >
52 {text}
53 </ReactMarkdown>
54 </div>
55 );
57​
58export default memo(ChatMarkdown);