Skip to content

Commit

Permalink
feat: add latex rendering (#247)
Browse files Browse the repository at this point in the history
* feat: add latex rendering

* perf: 提升 css 引入路径

---------

Co-authored-by: ChenZhaoYu <[email protected]>
  • Loading branch information
hyln9 and Chanzhaoyu authored Mar 4, 2023
1 parent f7f87e2 commit 2293969
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"GPTAPI",
"hljs",
"iconify",
"katex",
"logprobs",
"nodata",
"OPENAI",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@vueuse/core": "^9.13.0",
"highlight.js": "^11.7.0",
"katex": "^0.16.4",
"marked": "^4.2.12",
"naive-ui": "^2.34.3",
"pinia": "^2.0.32",
Expand All @@ -38,6 +39,7 @@
"@commitlint/config-conventional": "^17.4.4",
"@iconify/vue": "^4.1.0",
"@types/crypto-js": "^4.1.1",
"@types/katex": "^0.16.0",
"@types/marked": "^4.0.8",
"@types/node": "^18.14.4",
"@vitejs/plugin-vue": "^4.0.0",
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/plugins/assets.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
49 changes: 49 additions & 0 deletions src/views/chat/components/Message/Text.vue
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 2293969

Please sign in to comment.