feat: improved formula rendering with new escaping functions
- Improved formula rendering by removing unnecessary escaping of dollar numbers. - Implemented two new string escaping functions to prevent incorrect LaTeX formula rendering. #101
This commit is contained in:
parent
c8b98681ef
commit
6845ee1664
@ -1,6 +1,7 @@
|
|||||||
import 'katex/dist/katex.min.css'
|
import 'katex/dist/katex.min.css'
|
||||||
|
|
||||||
import { Message } from '@renderer/types'
|
import { Message } from '@renderer/types'
|
||||||
|
import { escapeBrackets } from '@renderer/utils/formula'
|
||||||
import { isEmpty } from 'lodash'
|
import { isEmpty } from 'lodash'
|
||||||
import { FC, useMemo } from 'react'
|
import { FC, useMemo } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
@ -31,7 +32,7 @@ const Markdown: FC<Props> = ({ 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
|
const content = empty && paused ? t('message.chat.completion.paused') : message.content
|
||||||
return escapeBrackets(escapeDollarNumber(content))
|
return escapeBrackets(content)
|
||||||
}, [message.content, message.status, t])
|
}, [message.content, message.status, t])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -50,35 +51,4 @@ const Markdown: FC<Props> = ({ message }) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeDollarNumber(text: string) {
|
|
||||||
let escapedText = ''
|
|
||||||
|
|
||||||
for (let i = 0; i < text.length; i += 1) {
|
|
||||||
let char = text[i]
|
|
||||||
const nextChar = text[i + 1] || ' '
|
|
||||||
|
|
||||||
if (char === '$' && nextChar >= '0' && nextChar <= '9') {
|
|
||||||
char = '\\$'
|
|
||||||
}
|
|
||||||
|
|
||||||
escapedText += char
|
|
||||||
}
|
|
||||||
|
|
||||||
return escapedText
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeBrackets(text: string) {
|
|
||||||
const pattern = /(```[\s\S]*?```|`.*?`)|\\\[([\s\S]*?[^\\])\\\]|\\\((.*?)\\\)/g
|
|
||||||
return text.replace(pattern, (match, codeBlock, squareBracket, roundBracket) => {
|
|
||||||
if (codeBlock) {
|
|
||||||
return codeBlock
|
|
||||||
} else if (squareBracket) {
|
|
||||||
return `$$${squareBracket}$$`
|
|
||||||
} else if (roundBracket) {
|
|
||||||
return `$${roundBracket}$`
|
|
||||||
}
|
|
||||||
return match
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Markdown
|
export default Markdown
|
||||||
|
|||||||
30
src/renderer/src/utils/formula.ts
Normal file
30
src/renderer/src/utils/formula.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
export function escapeDollarNumber(text: string) {
|
||||||
|
let escapedText = ''
|
||||||
|
|
||||||
|
for (let i = 0; i < text.length; i += 1) {
|
||||||
|
let char = text[i]
|
||||||
|
const nextChar = text[i + 1] || ' '
|
||||||
|
|
||||||
|
if (char === '$' && nextChar >= '0' && nextChar <= '9') {
|
||||||
|
char = '\\$'
|
||||||
|
}
|
||||||
|
|
||||||
|
escapedText += char
|
||||||
|
}
|
||||||
|
|
||||||
|
return escapedText
|
||||||
|
}
|
||||||
|
|
||||||
|
export function escapeBrackets(text: string) {
|
||||||
|
const pattern = /(```[\s\S]*?```|`.*?`)|\\\[([\s\S]*?[^\\])\\\]|\\\((.*?)\\\)/g
|
||||||
|
return text.replace(pattern, (match, codeBlock, squareBracket, roundBracket) => {
|
||||||
|
if (codeBlock) {
|
||||||
|
return codeBlock
|
||||||
|
} else if (squareBracket) {
|
||||||
|
return `$$${squareBracket}$$`
|
||||||
|
} else if (roundBracket) {
|
||||||
|
return `$${roundBracket}$`
|
||||||
|
}
|
||||||
|
return match
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user