fix: math code format
This commit is contained in:
parent
a8f3e2be6b
commit
8a0e2890dd
@ -1,8 +1,9 @@
|
||||
import 'katex/dist/katex.min.css'
|
||||
|
||||
import { Message } from '@renderer/types'
|
||||
import { convertMathFormula } from '@renderer/utils'
|
||||
import { isEmpty } from 'lodash'
|
||||
import { FC, useCallback, useMemo } from 'react'
|
||||
import { FC, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import ReactMarkdown from 'react-markdown'
|
||||
import rehypeKatex from 'rehype-katex'
|
||||
@ -19,31 +20,29 @@ interface Props {
|
||||
const Markdown: FC<Props> = ({ message }) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const getMessageContent = useCallback(
|
||||
(message: Message) => {
|
||||
const messageContent = useMemo(() => {
|
||||
const empty = isEmpty(message.content)
|
||||
const paused = message.status === 'paused'
|
||||
return empty && paused ? t('message.chat.completion.paused') : message.content
|
||||
},
|
||||
[t]
|
||||
)
|
||||
const content = empty && paused ? t('message.chat.completion.paused') : message.content
|
||||
return convertMathFormula(content)
|
||||
}, [message.content, message.status, t])
|
||||
|
||||
return useMemo(() => {
|
||||
return (
|
||||
<ReactMarkdown
|
||||
className="markdown"
|
||||
rehypePlugins={[rehypeKatex]}
|
||||
remarkPlugins={[[remarkMath, { singleDollarTextMath: false }], remarkGfm]}
|
||||
remarkRehypeOptions={{
|
||||
footnoteLabel: t('common.footnotes'),
|
||||
footnoteLabelTagName: 'h4',
|
||||
footnoteBackContent: ' '
|
||||
}}
|
||||
rehypePlugins={[rehypeKatex]}
|
||||
components={{ code: CodeBlock as any, a: Link as any }}>
|
||||
{getMessageContent(message)}
|
||||
{messageContent}
|
||||
</ReactMarkdown>
|
||||
)
|
||||
}, [getMessageContent, message, t])
|
||||
}, [messageContent, t])
|
||||
}
|
||||
|
||||
export default Markdown
|
||||
|
||||
@ -225,3 +225,8 @@ export function loadScript(url: string) {
|
||||
document.head.appendChild(script)
|
||||
})
|
||||
}
|
||||
|
||||
export function convertMathFormula(input) {
|
||||
// 使用正则表达式匹配并替换公式格式
|
||||
return input.replaceAll(/\\\[/g, '$$$$').replaceAll(/\\\]/g, '$$$$')
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user