opt: optimize markdown image parser, identify image and blockquote tags

This commit is contained in:
RockYang 2024-01-16 10:13:00 +08:00
parent 39fdfae541
commit 861f11af66
4 changed files with 33 additions and 10 deletions

View File

@ -102,6 +102,13 @@ export default defineComponent({
border-radius: 5px; border-radius: 5px;
overflow: auto; overflow: auto;
img {
max-width: 600px;
border-radius: 10px;
margin 10px 0
}
a { a {
color #20a0ff color #20a0ff
} }

View File

@ -53,6 +53,7 @@ import {ref} from "vue";
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
import {httpGet, httpPost} from "@/utils/http"; import {httpGet, httpPost} from "@/utils/http";
import {PictureFilled, Plus} from "@element-plus/icons-vue"; import {PictureFilled, Plus} from "@element-plus/icons-vue";
import {isImage} from "@/utils/libs";
const props = defineProps({ const props = defineProps({
userId: String, userId: String,
@ -69,11 +70,6 @@ const fetchFiles = () => {
}) })
} }
const isImage = (ext) => {
const expr = /\.(jpg|jpeg|png|gif|bmp|svg)$/i;
return expr.test(ext);
}
const getFileIcon = (ext) => { const getFileIcon = (ext) => {
const files = { const files = {
".docx": "doc.png", ".docx": "doc.png",

View File

@ -161,3 +161,8 @@ export function substr(str, length) {
return result return result
} }
export function isImage(url) {
const expr = /\.(jpg|jpeg|png|gif|bmp|svg)$/i;
return expr.test(url);
}

View File

@ -282,7 +282,7 @@ import {
VideoPause VideoPause
} from '@element-plus/icons-vue' } from '@element-plus/icons-vue'
import 'highlight.js/styles/a11y-dark.css' import 'highlight.js/styles/a11y-dark.css'
import {dateFormat, isMobile, randString, removeArrayItem, UUID} from "@/utils/libs"; import {dateFormat, isImage, isMobile, randString, removeArrayItem, UUID} from "@/utils/libs";
import {ElMessage, ElMessageBox} from "element-plus"; import {ElMessage, ElMessageBox} from "element-plus";
import hl from "highlight.js"; import hl from "highlight.js";
import {getSessionId, getUserToken, removeUserToken} from "@/store/session"; import {getSessionId, getUserToken, removeUserToken} from "@/store/session";
@ -636,7 +636,7 @@ const connect = function (chat_id, role_id) {
lineBuffer.value += data.content; lineBuffer.value += data.content;
const reply = chatData.value[chatData.value.length - 1] const reply = chatData.value[chatData.value.length - 1]
reply['orgContent'] = lineBuffer.value; reply['orgContent'] = lineBuffer.value;
reply['content'] = md.render(processBlankQuote(lineBuffer.value)); reply['content'] = md.render(processContent(lineBuffer.value));
} }
// //
nextTick(() => { nextTick(() => {
@ -712,7 +712,7 @@ const sendMessage = function () {
type: "prompt", type: "prompt",
id: randString(32), id: randString(32),
icon: loginUser.value.avatar, icon: loginUser.value.avatar,
content: md.render(prompt.value), content: md.render(processContent(prompt.value)),
created_at: new Date().getTime(), created_at: new Date().getTime(),
}); });
@ -778,7 +778,7 @@ const loadChatHistory = function (chatId) {
showHello.value = false showHello.value = false
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
data[i].orgContent = data[i].content; data[i].orgContent = data[i].content;
data[i].content = md.render(processBlankQuote(data[i].content)) data[i].content = md.render(processContent(data[i].content))
chatData.value.push(data[i]); chatData.value.push(data[i]);
} }
@ -792,7 +792,22 @@ const loadChatHistory = function (chatId) {
}) })
} }
const processBlankQuote = (content) => { const processContent = (content) => {
//process img url
const linkRegex = /(https?:\/\/\S+)/g;
const links = content.match(linkRegex);
if (links) {
for (let link of links) {
if (isImage(link)) {
const index = content.indexOf(link)
if (content.substring(index - 1, 2) !== "]") {
content = content.replace(link, "\n![](" + link + ")\n")
}
}
}
}
//
if (content.indexOf("\n") === -1) { if (content.indexOf("\n") === -1) {
return content return content
} }