Skip to content

Commit cdf4270

Browse files
committed
fix children as props
1 parent b5dc6cf commit cdf4270

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

components/ChatLine.tsx

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import ReactMarkdown from 'react-markdown'
33
import remarkGfm from 'remark-gfm'
44
import {Prism as SyntaxHighlighter, SyntaxHighlighterProps} from 'react-syntax-highlighter'
55
import 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

711
type 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 = /language-(\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

3758
export 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 = /language-(\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

Comments
 (0)