Skip to content

Commit 92740ad

Browse files
authored
🎲 feat: support math katex highlighting (#27)
1 parent 7d73345 commit 92740ad

File tree

9 files changed

+64
-29
lines changed

9 files changed

+64
-29
lines changed

.stylelintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
'scss/double-slash-comment-whitespace-inside': 'always',
3131
'scss/dollar-variable-pattern': null,
3232

33+
'selector-type-no-unknown': null,
3334
'selector-pseudo-class-no-unknown': [
3435
true,
3536
{

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# chatgpt-vue3-light-mvp
22

3-
💭 一个可二次开发 Chat Bot 对话 Web 端原型模板, 基于 Vue3、Vite 6、TypeScript、Naive UI、Pinia、UnoCSS 等主流技术构建, 🧤简单集成大模型 API, 采用单轮 AI 问答对话模式, 每次提问独立响应, 无需上下文, 支持打字机效果流式输出, 集成 markdown-it, highlight.js 语法高亮预览, 💼 易于定制和快速搭建 Chat 类大语言模型产品
3+
💭 一个可二次开发 Chat Bot 对话 Web 端原型模板, 基于 Vue3、Vite 6、TypeScript、Naive UI、Pinia、UnoCSS 等主流技术构建, 🧤简单集成大模型 API, 采用单轮 AI 问答对话模式, 每次提问独立响应, 无需上下文, 支持打字机效果流式输出, 集成 markdown-it, highlight.js, 数学公式语法高亮预览, 💼 易于定制和快速搭建 Chat 类大语言模型产品
44

55

66
__[🌈 Live Demo 在线体验](https://pdsuwwz.github.io/chatgpt-vue3-light-mvp)__
@@ -220,9 +220,10 @@ export const isGithubDeployed = process.env.VITE_ROUTER_MODE === 'hash'
220220
<details>
221221
<summary>使用本地 Ollama 大模型</summary><br>
222222

223-
**Ollama3 大模型**
223+
**Ollama 大模型**
224224
- **安装**:Ollama3 不需要 API 密钥,只需要在本地安装并运行 Ollama 即可。请参考 Ollama 官方文档进行安装:[Ollama GitHub](https://github.com/ollama/ollama)
225-
- **运行**:运行 Ollama3 服务,直接执行 `ollama run llama3`, 运行后确保其在 `http://localhost:11434` 运行
225+
- **Ollama现已支持的大模型**:[模型列表](https://ollama.com/search)
226+
- **运行**:运行 Ollama3 服务,直接执行 `ollama run <模型名称>`, 如: `ollama run llama3`, 运行后确保其在 `http://localhost:11434` 运行
226227

227228
![image](https://github.com/user-attachments/assets/f3955060-a22d-4db8-b162-7393c10403f6)
228229

@@ -346,6 +347,8 @@ export const defaultMockModelName = 'standard'
346347
/**
347348
* 项目默认使用模型,按需修改此字段即可
348349
*/
350+
351+
// export const defaultModelName = 'spark'
349352
export const defaultModelName = defaultMockModelName
350353
```
351354

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@unocss/preset-rem-to-px": "^65.4.3",
6161
"@vitejs/plugin-vue": "^5.2.1",
6262
"@vitejs/plugin-vue-jsx": "^4.1.1",
63+
"@vscode/markdown-it-katex": "~1.1.1",
6364
"@vue/babel-plugin-jsx": "^1.2.5",
6465
"@vue/compiler-sfc": "^3.5.13",
6566
"cross-env": "^7.0.3",

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/MarkdownPreview/index.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts" setup>
2-
import MarkdownInstance from './plugins/markdown'
2+
import { renderMarkdownText } from './plugins/markdown'
33
44
import type { CrossTransformFunction, TransformFunction } from './models'
55
import { defaultMockModelName } from './models'
@@ -39,7 +39,7 @@ const refWrapperContent = ref<HTMLElement>()
3939
let typingAnimationFrame: number | null = null
4040
4141
const renderedMarkdown = computed(() => {
42-
return MarkdownInstance.render(displayText.value)
42+
return renderMarkdownText(displayText.value)
4343
})
4444
4545
@@ -477,5 +477,9 @@ const emptyPlaceholder = computed(() => {
477477
margin: 0;
478478
}
479479
}
480+
481+
.katex {
482+
--at-apply: c-primary;
483+
}
480484
}
481485
</style>

src/components/MarkdownPreview/models/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export const defaultMockModelName = 'standard'
8080
/**
8181
* 项目默认使用模型,按需修改此字段即可
8282
*/
83+
84+
// export const defaultModelName = 'spark'
8385
export const defaultModelName = defaultMockModelName
8486

8587
export const modelMappingList: TypesModelLLM[] = [
@@ -205,7 +207,7 @@ export const modelMappingList: TypesModelLLM[] = [
205207
messages: [
206208
{
207209
role: 'system',
208-
content: '你叫小明同学,喜欢探索新的前端知识,目前正在学习 AI 大模型。我可以帮你解决任何前端方面的问题。'
210+
content: '你叫小明同学,喜欢探索新的前端知识,目前正在学习 AI 大模型。你可以解决任何前端方面的问题。'
209211
},
210212
{
211213
'role': 'user',

src/components/MarkdownPreview/plugins/markdown.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,52 @@
11
import MarkdownIt from 'markdown-it'
22
import hljs from './highlight'
3-
43
import markdownItHighlight from 'markdown-it-highlightjs'
54
import { preWrapperPlugin } from './preWrapper'
65

6+
import markdownItKatex from '@vscode/markdown-it-katex'
7+
import splitAtDelimiters from 'katex/contrib/auto-render/splitAtDelimiters'
8+
9+
import 'katex/dist/katex.min.css'
10+
import 'katex/dist/contrib/mhchem.min.js'
11+
712
const md = new MarkdownIt({
813
html: true,
914
linkify: true,
1015
typographer: true
1116
})
1217

18+
const transformMathMarkdown = (markdownText: string) => {
19+
const data = splitAtDelimiters(markdownText, [
20+
{
21+
left: '\\[',
22+
right: '\\]',
23+
display: true
24+
},
25+
{
26+
left: '\\(',
27+
right: '\\)',
28+
display: false
29+
}
30+
])
31+
32+
return data.reduce((result, segment: any) => {
33+
if (segment.type === 'text') {
34+
return result + segment.data
35+
}
36+
const math = segment.display ? `$$${ segment.data }$$` : `$${ segment.data }$`
37+
return result + math
38+
}, '')
39+
}
40+
41+
1342
md.use(markdownItHighlight, {
1443
hljs
1544
}).use(preWrapperPlugin, {
1645
hasSingleTheme: true
17-
})
46+
}).use(markdownItKatex)
47+
48+
export const renderMarkdownText = (content: string) => {
49+
const transformedContent = transformMathMarkdown(content)
50+
return md.render(transformedContent)
51+
}
1852

19-
export default md

src/components/index.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import { setupStore } from '@/store'
55

66
import App from '@/App.vue'
77

8-
import InstallGlobalComponents from '@/components'
98

109
const app = createApp(App)
1110

1211
function setupPlugins() {
13-
app
14-
.use(InstallGlobalComponents)
12+
// ...
1513
}
1614

1715
async function setupApp() {

0 commit comments

Comments
 (0)