Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit ac9536a

Browse files
authored
feat: 支持 markdown 格式 (#77)
* feat: 支持 markdown 格式和图片 * perf: 重载的时候滚动条保持 * chore: version 2.5.2 * feat: 添加文字换行 * chore: 添加新封面 * chore: 更新 cover
1 parent 938c91f commit ac9536a

File tree

11 files changed

+85
-24
lines changed

11 files changed

+85
-24
lines changed

CHANGELOG.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
## v2.5.2
2+
3+
`2023-02-21`
4+
### Feature
5+
- 增加对 `markdown` 格式的支持 [Demo](https://github.com/Chanzhaoyu/chatgpt-web/pull/77)
6+
### BugFix
7+
- 重载会话时滚动条保持
8+
19
## v2.5.1
210

311
`2023-02-21`
12+
413
### Enhancement
514
- 调整路由模式为 `hash`
6-
- 调整新增会话添加到列表最前
15+
- 调整新增会话添加到
716
- 调整移动端样式
817

18+
919
## v2.5.0
1020

1121
`2023-02-20`

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
使用 express 和 vue3 搭建的 ChartGPT 演示网页
44

5-
![PC](./docs/cover.png)
5+
![cover](./docs/cover.png)
6+
![cover2](./docs/cover2.png)
67

78
> 提示:目前 `OpenAI` 开放的模型最高只有 `GPT-3`,和现在网页所使用的 `GPT-3.5``GPT-4` 有很大差距,需要等官方开放最新的模型接口。
89

docs/cover.png

33.7 KB
Loading

docs/cover2.png

518 KB
Loading

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chatgpt-web",
3-
"version": "2.5.1",
3+
"version": "2.5.2",
44
"private": false,
55
"description": "ChatGPT Web",
66
"author": "ChenZhaoYu <[email protected]>",
@@ -25,6 +25,7 @@
2525
"dependencies": {
2626
"@vueuse/core": "^9.13.0",
2727
"highlight.js": "^11.7.0",
28+
"marked": "^4.2.12",
2829
"naive-ui": "^2.34.3",
2930
"pinia": "^2.0.30",
3031
"vue": "^3.2.47",
@@ -36,6 +37,7 @@
3637
"@commitlint/config-conventional": "^17.4.4",
3738
"@iconify/vue": "^4.1.0",
3839
"@types/crypto-js": "^4.1.1",
40+
"@types/marked": "^4.0.8",
3941
"@types/node": "^18.14.0",
4042
"@types/web-bluetooth": "^0.0.16",
4143
"@vitejs/plugin-vue": "^4.0.0",

pnpm-lock.yaml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/highlight.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { App, Directive } from 'vue'
22
import hljs from 'highlight.js'
3+
import includeCode from '@/utils/functions/includeCode'
34

45
function highlightCode(el: HTMLElement) {
5-
const regexp = /^(?:\s{4}|\t).+/gm
6-
if (el.textContent?.indexOf(' = ') !== -1 || el.textContent.match(regexp))
6+
if (includeCode(el.textContent))
77
hljs.highlightBlock(el)
88
}
99

src/utils/functions/includeCode.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function includeCode(text: string | null | undefined) {
2+
const regexp = /^(?:\s{4}|\t).+/gm
3+
if (text?.includes(' = ') || text?.match(regexp))
4+
return true
5+
return false
6+
}
7+
8+
export default includeCode
+44-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,60 @@
11
<script lang="ts" setup>
2+
import { computed } from 'vue'
3+
import { marked } from 'marked'
4+
import includeCode from '@/utils/functions/includeCode'
5+
26
interface Props {
37
inversion?: boolean
48
error?: boolean
9+
text?: string
10+
loading?: boolean
511
}
612
7-
defineProps<Props>()
13+
const props = defineProps<Props>()
14+
15+
const wrapClass = computed(() => {
16+
return [
17+
'text-wrap',
18+
'p-2',
19+
'min-w-[20px]',
20+
'rounded-md',
21+
props.inversion ? 'bg-[#d2f9d1]' : 'bg-[#f4f6f8]',
22+
{ 'text-red-500': props.error },
23+
]
24+
})
25+
26+
const text = computed(() => {
27+
if (props.text) {
28+
if (!includeCode(props.text))
29+
return marked.parse(props.text)
30+
return props.text
31+
}
32+
return ''
33+
})
834
</script>
935

1036
<template>
11-
<div
12-
class="min-w-[20px] p-2 rounded-md"
13-
:class="[inversion ? 'bg-[#d2f9d1]' : 'bg-[#f4f6f8]', { 'text-red-500': error }]"
14-
>
15-
<span
16-
v-highlight
17-
class="leading-relaxed whitespace-pre-wrap"
18-
>
19-
<slot />
20-
</span>
37+
<div :class="wrapClass">
38+
<template v-if="loading">
39+
<span class="w-[3px] h-[20px] block animate-blink" />
40+
</template>
41+
<template v-else>
42+
<code v-if="includeCode(text)" v-highlight class="leading-relaxed" v-text="text" />
43+
<div v-else class="leading-relaxed break-all" v-html="text" />
44+
</template>
2145
</div>
2246
</template>
2347

24-
<style>
48+
<style lang="less">
49+
.text-wrap{
50+
img{
51+
max-width: 100%;
52+
vertical-align: middle;
53+
}
54+
}
55+
2556
.hljs {
2657
background-color: #fff0 !important;
58+
white-space: break-spaces;
2759
}
2860
</style>

src/views/chat/components/Message/index.vue

+1-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ function handleRegenerate() {
3737
{{ dateTime }}
3838
</span>
3939
<div class="flex items-end mt-2">
40-
<Text :inversion="inversion" :error="error">
41-
<span v-if="loading" class="w-[3px] h-[20px] block animate-blink" />
42-
<span v-else>{{ text }}</span>
43-
</Text>
40+
<Text :inversion="inversion" :error="error" :text="text" :loading="loading" />
4441
<button
4542
v-if="!inversion && !loading"
4643
class="mb-2 ml-2 transition text-neutral-400 hover:text-neutral-800"

src/views/chat/index.vue

-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ async function onRegenerate(index: number) {
153153
requestOptions: { prompt: message, ...options },
154154
},
155155
)
156-
scrollToBottom()
157156
158157
try {
159158
const { data } = await fetchChatAPI<Chat.ConversationResponse>(message, options, controller.signal)
@@ -170,7 +169,6 @@ async function onRegenerate(index: number) {
170169
requestOptions: { prompt: message, ...options },
171170
},
172171
)
173-
scrollToBottom()
174172
}
175173
catch (error: any) {
176174
let errorMessage = 'Something went wrong, please try again later.'
@@ -191,7 +189,6 @@ async function onRegenerate(index: number) {
191189
requestOptions: { prompt: message, ...options },
192190
},
193191
)
194-
scrollToBottom()
195192
}
196193
finally {
197194
loading.value = false

0 commit comments

Comments
 (0)