@@ -3,6 +3,10 @@ import ReactMarkdown from 'react-markdown'
33import remarkGfm from 'remark-gfm'
44import { Prism as SyntaxHighlighter , SyntaxHighlighterProps } from 'react-syntax-highlighter'
55import okaidia from 'react-syntax-highlighter/dist/cjs/styles/prism/okaidia' ;
6+ import {
7+ CodeComponent ,
8+ ReactMarkdownNames ,
9+ } from 'react-markdown/lib/ast-to-react' ;
610
711type ChatGPTAgent = 'user' | 'system' | 'assistant'
812
@@ -33,12 +37,33 @@ export const LoadingChatLine = () => (
3337 </ div >
3438)
3539
40+ const CodeBlock : CodeComponent | ReactMarkdownNames = ( {
41+ inline,
42+ className,
43+ children,
44+ ...props
45+ } ) => {
46+ const match = / l a n g u a g e - ( \w + ) / . exec ( className || '' ) ;
47+ return ! inline && match ? (
48+ < SyntaxHighlighter style = { okaidia } language = { match [ 1 ] } PreTag = "div" { ...props } >
49+ { String ( children ) . replace ( / \n $ / , '' ) }
50+ </ SyntaxHighlighter >
51+ ) : (
52+ < code className = { className } { ...props } >
53+ { children }
54+ </ code >
55+ ) ;
56+ } ;
3657
3758export function ChatLine ( { role = 'assistant' , content } : ChatGPTMessage ) {
3859 if ( ! content ) {
3960 return null
4061 }
4162
63+ const components = {
64+ code : CodeBlock ,
65+ } ;
66+
4267 return (
4368 < div
4469 className = {
@@ -64,24 +89,7 @@ export function ChatLine({ role = 'assistant', content }: ChatGPTMessage) {
6489 < ReactMarkdown
6590 linkTarget = { "_blank" }
6691 remarkPlugins = { [ remarkGfm ] }
67- components = { {
68- code ( { node, inline, className, children, ...props } ) {
69- const match = / l a n g u a g e - ( \w + ) / . exec ( className || '' )
70- return ! inline && match ? (
71- < SyntaxHighlighter
72- children = { String ( children ) . replace ( / \n $ / , '' ) }
73- style = { okaidia }
74- language = { match [ 1 ] }
75- PreTag = "div"
76- { ...props }
77- />
78- ) : (
79- < code className = { className } { ...props } >
80- { children }
81- </ code >
82- )
83- }
84- } }
92+ components = { components }
8593 >
8694 { content }
8795 </ ReactMarkdown >
0 commit comments