mirror of
https://github.com/Chanzhaoyu/chatgpt-web.git
synced 2025-07-21 11:57:47 +00:00
feat: add latex rendering (#247)
* feat: add latex rendering * perf: 提升 css 引入路径 --------- Co-authored-by: ChenZhaoYu <790348264@qq.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'katex/dist/katex.min.css'
|
||||
import '@/styles/lib/tailwind.css'
|
||||
import '@/styles/lib/highlight.less'
|
||||
import '@/styles/lib/github-markdown.less'
|
||||
|
@@ -1,5 +1,6 @@
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref } from 'vue'
|
||||
import katex from 'katex'
|
||||
import { marked } from 'marked'
|
||||
import hljs from 'highlight.js'
|
||||
import { useBasicLayout } from '@/hooks/useBasicLayout'
|
||||
@@ -41,6 +42,54 @@ marked.setOptions({
|
||||
},
|
||||
})
|
||||
|
||||
const katexOptions = {
|
||||
throwOnError: false,
|
||||
}
|
||||
|
||||
const katexInline = {
|
||||
name: 'katexInline',
|
||||
level: 'inline',
|
||||
start(src: string) {
|
||||
return src.indexOf('$')
|
||||
},
|
||||
tokenizer(src: string) {
|
||||
const match = src.match(/^\$+([^$\n]+?)\$+/)
|
||||
if (match) {
|
||||
return {
|
||||
type: 'katexInline',
|
||||
raw: match[0],
|
||||
text: match[1].trim(),
|
||||
}
|
||||
}
|
||||
},
|
||||
renderer(token: marked.Tokens.Generic) {
|
||||
return katex.renderToString(token.text, katexOptions)
|
||||
},
|
||||
}
|
||||
|
||||
const katexBlock = {
|
||||
name: 'katexBlock',
|
||||
level: 'block',
|
||||
start(src: string) {
|
||||
return src.indexOf('\n$$')
|
||||
},
|
||||
tokenizer(src: string) {
|
||||
const match = src.match(/^\$\$+\n([^$]+?)\n\$\$+\n/)
|
||||
if (match) {
|
||||
return {
|
||||
type: 'katexBlock',
|
||||
raw: match[0],
|
||||
text: match[1].trim(),
|
||||
}
|
||||
}
|
||||
},
|
||||
renderer(token: marked.Tokens.Generic) {
|
||||
return `<p>${katex.renderToString(token.text, katexOptions)}</p>`
|
||||
},
|
||||
}
|
||||
|
||||
marked.use({ extensions: [katexInline, katexBlock] })
|
||||
|
||||
const wrapClass = computed(() => {
|
||||
return [
|
||||
'text-wrap',
|
||||
|
Reference in New Issue
Block a user