feat: 支持 markdown 格式 (#77)

* feat: 支持 markdown 格式和图片

* perf: 重载的时候滚动条保持

* chore: version 2.5.2

* feat: 添加文字换行

* chore: 添加新封面

* chore: 更新 cover
This commit is contained in:
Redon
2023-02-21 11:57:29 +08:00
committed by GitHub
parent 938c91f635
commit ac9536ab87
11 changed files with 85 additions and 24 deletions

View File

@@ -1,9 +1,9 @@
import type { App, Directive } from 'vue'
import hljs from 'highlight.js'
import includeCode from '@/utils/functions/includeCode'
function highlightCode(el: HTMLElement) {
const regexp = /^(?:\s{4}|\t).+/gm
if (el.textContent?.indexOf(' = ') !== -1 || el.textContent.match(regexp))
if (includeCode(el.textContent))
hljs.highlightBlock(el)
}

View File

@@ -0,0 +1,8 @@
function includeCode(text: string | null | undefined) {
const regexp = /^(?:\s{4}|\t).+/gm
if (text?.includes(' = ') || text?.match(regexp))
return true
return false
}
export default includeCode

View File

@@ -1,28 +1,60 @@
<script lang="ts" setup>
import { computed } from 'vue'
import { marked } from 'marked'
import includeCode from '@/utils/functions/includeCode'
interface Props {
inversion?: boolean
error?: boolean
text?: string
loading?: boolean
}
defineProps<Props>()
const props = defineProps<Props>()
const wrapClass = computed(() => {
return [
'text-wrap',
'p-2',
'min-w-[20px]',
'rounded-md',
props.inversion ? 'bg-[#d2f9d1]' : 'bg-[#f4f6f8]',
{ 'text-red-500': props.error },
]
})
const text = computed(() => {
if (props.text) {
if (!includeCode(props.text))
return marked.parse(props.text)
return props.text
}
return ''
})
</script>
<template>
<div
class="min-w-[20px] p-2 rounded-md"
:class="[inversion ? 'bg-[#d2f9d1]' : 'bg-[#f4f6f8]', { 'text-red-500': error }]"
>
<span
v-highlight
class="leading-relaxed whitespace-pre-wrap"
>
<slot />
</span>
<div :class="wrapClass">
<template v-if="loading">
<span class="w-[3px] h-[20px] block animate-blink" />
</template>
<template v-else>
<code v-if="includeCode(text)" v-highlight class="leading-relaxed" v-text="text" />
<div v-else class="leading-relaxed break-all" v-html="text" />
</template>
</div>
</template>
<style>
<style lang="less">
.text-wrap{
img{
max-width: 100%;
vertical-align: middle;
}
}
.hljs {
background-color: #fff0 !important;
white-space: break-spaces;
}
</style>

View File

@@ -37,10 +37,7 @@ function handleRegenerate() {
{{ dateTime }}
</span>
<div class="flex items-end mt-2">
<Text :inversion="inversion" :error="error">
<span v-if="loading" class="w-[3px] h-[20px] block animate-blink" />
<span v-else>{{ text }}</span>
</Text>
<Text :inversion="inversion" :error="error" :text="text" :loading="loading" />
<button
v-if="!inversion && !loading"
class="mb-2 ml-2 transition text-neutral-400 hover:text-neutral-800"

View File

@@ -153,7 +153,6 @@ async function onRegenerate(index: number) {
requestOptions: { prompt: message, ...options },
},
)
scrollToBottom()
try {
const { data } = await fetchChatAPI<Chat.ConversationResponse>(message, options, controller.signal)
@@ -170,7 +169,6 @@ async function onRegenerate(index: number) {
requestOptions: { prompt: message, ...options },
},
)
scrollToBottom()
}
catch (error: any) {
let errorMessage = 'Something went wrong, please try again later.'
@@ -191,7 +189,6 @@ async function onRegenerate(index: number) {
requestOptions: { prompt: message, ...options },
},
)
scrollToBottom()
}
finally {
loading.value = false