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