|
1 | | -import React, { useState } from 'react' |
| 1 | +import React, { useMemo, useState } from 'react' |
2 | 2 | import cn from "classnames"; |
3 | | -import ReactMarkdown from "react-markdown"; |
| 3 | +import ReactMarkdown, { Components } from "react-markdown"; |
4 | 4 | import rehypeRaw from "rehype-raw"; |
5 | 5 | import remarkGfm from "remark-gfm"; |
6 | 6 | import { makeStyles } from "@material-ui/core"; |
@@ -241,7 +241,20 @@ export const Message = React.memo((props: MessageProps) => { |
241 | 241 | setDebugVisible(prevState => !prevState) |
242 | 242 | } |
243 | 243 |
|
244 | | - const contentToRender: string = content?.replace(/\n/g, ' \n') || '' |
| 244 | + const contentToRender = useMemo(() => content, [content]); |
| 245 | + |
| 246 | + const renderers = useMemo<Components>(() => ({ |
| 247 | + p: ({ node, ...props }) => <div {...props} />, |
| 248 | + img: ({ node, ...props }) => <img style={{ maxWidth: '60%' }} {...props} />, |
| 249 | + code: ({ node, inline, className, children, ...props }) => { |
| 250 | + const match = /language-(\w+)/.exec(className || ''); |
| 251 | + return !inline ? ( |
| 252 | + <CodeBlock value={String(children).replace(/\n$/, '')} language={match?.[1]} /> |
| 253 | + ) : ( |
| 254 | + <code {...props}>{children}</code> |
| 255 | + ); |
| 256 | + }, |
| 257 | + }), []); |
245 | 258 |
|
246 | 259 | return ( |
247 | 260 | <> |
@@ -303,22 +316,11 @@ export const Message = React.memo((props: MessageProps) => { |
303 | 316 | </div> |
304 | 317 | : <ReactMarkdown |
305 | 318 | className={classes.markdown} |
306 | | - children={contentToRender} |
| 319 | + children={contentToRender || ''} |
307 | 320 | rehypePlugins={[rehypeRaw]} |
308 | 321 | remarkPlugins={[remarkGfm]} |
309 | 322 | linkTarget='_blank' |
310 | | - components={{ |
311 | | - p: 'div', |
312 | | - img: ({node, ...props}) => <img style={{maxWidth: '60%'}} {...props} />, |
313 | | - code: ({ node, inline, className, children, ...props }) => { |
314 | | - const match = /language-(\w+)/.exec(className || ''); |
315 | | - return !inline ? ( |
316 | | - <CodeBlock value={String(children).replace(/\n$/, '')} language={match?.[1]} /> |
317 | | - ) : ( |
318 | | - <code {...props}>{children}</code> |
319 | | - ); |
320 | | - }, |
321 | | - }} |
| 323 | + components={renderers} |
322 | 324 | /> |
323 | 325 | } |
324 | 326 | </div> |
|
0 commit comments