|
1 | | -import { MarkdownRenderer } from "@features/editor/components/MarkdownRenderer"; |
2 | | -import { File } from "@phosphor-icons/react"; |
3 | | -import { Box, Code } from "@radix-ui/themes"; |
4 | | -import type { ReactNode } from "react"; |
| 1 | +import { FileIcon } from "@components/ui/FileIcon"; |
| 2 | +import { |
| 3 | + baseComponents, |
| 4 | + remarkPlugins, |
| 5 | +} from "@features/editor/components/MarkdownRenderer"; |
| 6 | +import { Box, Code, Link } from "@radix-ui/themes"; |
| 7 | +import { useMemo } from "react"; |
| 8 | +import type { Components } from "react-markdown"; |
| 9 | +import ReactMarkdown from "react-markdown"; |
5 | 10 |
|
6 | 11 | interface UserMessageProps { |
7 | 12 | content: string; |
8 | 13 | } |
9 | 14 |
|
10 | | -function parseFileMentions(content: string): ReactNode[] { |
11 | | - const fileTagRegex = /<file\s+path="([^"]+)"\s*\/>/g; |
12 | | - const parts: ReactNode[] = []; |
13 | | - let lastIndex = 0; |
| 15 | +/** |
| 16 | + * Convert `<file path="..." />` XML tags into markdown links with a custom |
| 17 | + * `twig-file:` protocol so they are parsed as inline elements within paragraphs. |
| 18 | + */ |
| 19 | +function preprocessFileMentions(content: string): string { |
| 20 | + return content.replace(/<file\s+path="([^"]+)"\s*\/>/g, (_match, path) => { |
| 21 | + const fileName = path.split("/").pop() ?? path; |
| 22 | + const safeFileName = fileName.replace(/[[\]]/g, "\\$&"); |
| 23 | + return `[${safeFileName}](twig-file:${encodeURIComponent(path)})`; |
| 24 | + }); |
| 25 | +} |
14 | 26 |
|
15 | | - for (const match of content.matchAll(fileTagRegex)) { |
16 | | - if (match.index !== undefined && match.index > lastIndex) { |
17 | | - const textBefore = content.slice(lastIndex, match.index); |
18 | | - parts.push( |
19 | | - <MarkdownRenderer key={`text-${lastIndex}`} content={textBefore} />, |
| 27 | +const userMessageComponents: Components = { |
| 28 | + ...baseComponents, |
| 29 | + a: ({ href, children }) => { |
| 30 | + if (href?.startsWith("twig-file:")) { |
| 31 | + const filePath = decodeURIComponent(href.slice("twig-file:".length)); |
| 32 | + const fileName = filePath.split("/").pop() ?? filePath; |
| 33 | + return ( |
| 34 | + <Code |
| 35 | + size="1" |
| 36 | + variant="soft" |
| 37 | + style={{ |
| 38 | + display: "inline-flex", |
| 39 | + alignItems: "center", |
| 40 | + gap: "4px", |
| 41 | + verticalAlign: "middle", |
| 42 | + }} |
| 43 | + > |
| 44 | + <FileIcon filename={fileName} size={12} /> |
| 45 | + {children} |
| 46 | + </Code> |
20 | 47 | ); |
21 | 48 | } |
22 | | - |
23 | | - const filePath = match[1]; |
24 | | - const fileName = filePath.split("/").pop() ?? filePath; |
25 | | - parts.push( |
26 | | - <Code |
27 | | - key={`file-${match.index}`} |
28 | | - size="1" |
29 | | - variant="soft" |
30 | | - style={{ |
31 | | - display: "inline-flex", |
32 | | - alignItems: "center", |
33 | | - gap: "4px", |
34 | | - verticalAlign: "middle", |
35 | | - }} |
36 | | - > |
37 | | - <File size={12} /> |
38 | | - {fileName} |
39 | | - </Code>, |
40 | | - ); |
41 | | - |
42 | | - lastIndex = (match.index ?? 0) + match[0].length; |
43 | | - } |
44 | | - |
45 | | - if (lastIndex < content.length) { |
46 | | - parts.push( |
47 | | - <MarkdownRenderer |
48 | | - key={`text-${lastIndex}`} |
49 | | - content={content.slice(lastIndex)} |
50 | | - />, |
| 49 | + return ( |
| 50 | + <Link href={href} target="_blank" rel="noopener noreferrer" size="1"> |
| 51 | + {children} |
| 52 | + </Link> |
51 | 53 | ); |
52 | | - } |
53 | | - |
54 | | - return parts; |
55 | | -} |
| 54 | + }, |
| 55 | +}; |
56 | 56 |
|
57 | 57 | export function UserMessage({ content }: UserMessageProps) { |
58 | | - const hasFileMentions = /<file\s+path="[^"]+"\s*\/>/.test(content); |
| 58 | + const processedContent = useMemo( |
| 59 | + () => preprocessFileMentions(content), |
| 60 | + [content], |
| 61 | + ); |
59 | 62 |
|
60 | 63 | return ( |
61 | 64 | <Box |
62 | 65 | className="border-l-2 bg-gray-2 py-2 pl-3" |
63 | 66 | style={{ borderColor: "var(--accent-9)" }} |
64 | 67 | > |
65 | 68 | <Box className="font-medium [&>*:last-child]:mb-0"> |
66 | | - {hasFileMentions ? ( |
67 | | - parseFileMentions(content) |
68 | | - ) : ( |
69 | | - <MarkdownRenderer content={content} /> |
70 | | - )} |
| 69 | + <ReactMarkdown |
| 70 | + remarkPlugins={remarkPlugins} |
| 71 | + components={userMessageComponents} |
| 72 | + > |
| 73 | + {processedContent} |
| 74 | + </ReactMarkdown> |
71 | 75 | </Box> |
72 | 76 | </Box> |
73 | 77 | ); |
|
0 commit comments