perf: 数学公式 $$ 处理

This commit is contained in:
ChenZhoYu 2024-06-07 10:18:56 +08:00
parent a546d856d7
commit 574aac2ff1
1 changed files with 34 additions and 2 deletions

View File

@ -53,8 +53,11 @@ const wrapClass = computed(() => {
const text = computed(() => { const text = computed(() => {
const value = props.text ?? '' const value = props.text ?? ''
if (!props.asRawText) if (!props.asRawText) {
return mdi.render(value) // $$
const escapedText = escapeBrackets(escapeDollarNumber(value))
return mdi.render(escapedText)
}
return value return value
}) })
@ -90,6 +93,35 @@ function removeCopyEvents() {
} }
} }
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
})
}
onMounted(() => { onMounted(() => {
addCopyEvents() addCopyEvents()
}) })