改进 markdown 渲染
Some checks failed
Build / build (push) Has been cancelled

This commit is contained in:
Twilight 2024-09-16 12:03:35 +08:00
parent 41528c9d17
commit 7bf232be6a
4 changed files with 1277 additions and 16 deletions

View File

@ -26,6 +26,7 @@
"devDependencies": { "devDependencies": {
"@babel/types": "^7.18.10", "@babel/types": "^7.18.10",
"@openapitools/openapi-generator-cli": "^2.13.5", "@openapitools/openapi-generator-cli": "^2.13.5",
"@shikijs/markdown-it": "^1.17.6",
"@types/markdown-it": "^14.1.2", "@types/markdown-it": "^14.1.2",
"@types/node": "^22.5.4", "@types/node": "^22.5.4",
"@vicons/ionicons5": "^0.12.0", "@vicons/ionicons5": "^0.12.0",
@ -45,6 +46,7 @@
"naive-ui": "^2.39.0", "naive-ui": "^2.39.0",
"postcss": "^8.4.45", "postcss": "^8.4.45",
"prettier": "^3.3.3", "prettier": "^3.3.3",
"shiki": "^1.17.6",
"tailwindcss": "^3.4.10", "tailwindcss": "^3.4.10",
"typescript": "^5.5.2", "typescript": "^5.5.2",
"unplugin-auto-import": "^0.18.2", "unplugin-auto-import": "^0.18.2",

View File

@ -32,7 +32,7 @@
:plugins="markdownPlugins" :plugins="markdownPlugins"
/> --> /> -->
<!-- <v-md-preview :text="message.content" height="500px"></v-md-preview> --> <!-- <v-md-preview :text="message.content" height="500px"></v-md-preview> -->
<div v-html="mdIt.render(message.content)"></div> <div v-if="mdInited" class="markdown-body" v-html="mdIt.render(message.content)"></div>
<div class="relative h-full"> <div class="relative h-full">
<n-avatar <n-avatar
round round
@ -48,7 +48,6 @@
<!-- 助理消息 --> <!-- 助理消息 -->
<n-flex justify="start" class="!flex-nowrap"> <n-flex justify="start" class="!flex-nowrap">
<div class="relative h-full"> <div class="relative h-full">
<n-avatar <n-avatar
round round
size="large" size="large"
@ -66,7 +65,13 @@
<!-- <div v-html="mdIt.render('# Math Rulez! \n $\\sqrt{3x-1}+(1+x)^2$')"></div> --> <!-- <div v-html="mdIt.render('# Math Rulez! \n $\\sqrt{3x-1}+(1+x)^2$')"></div> -->
<div v-html="mdIt.render(message.content)"></div> <!-- message.content 变化时重新渲染 -->
<div
v-if="mdInited"
class="break-all break-words markdown-body"
v-html="mdIt.render(message.content)"
></div>
<!-- <div v-html="mdIt.render(message.content)"></div> -->
<!-- <v-md-preview :text="message.content" height="500px"></v-md-preview> --> <!-- <v-md-preview :text="message.content" height="500px"></v-md-preview> -->
</div> </div>
@ -88,23 +93,37 @@ import markdownIt from "markdown-it";
// highlightjs // highlightjs
import hljs from "highlight.js"; import hljs from "highlight.js";
import config from "@/config/config"; import config from "@/config/config";
import Shiki from "@shikijs/markdown-it";
const mdIt = markdownIt(); const mdIt = markdownIt();
const mdInited = ref(false);
// set options // set options
mdIt.options.highlight = function (str: string, lang: string) { // mdIt.options.highlight = function (str: string, lang: string) {
if (!lang) { // if (!lang) {
return ""; // return "";
} // }
return hljs.highlight(str, { language: lang }).value; // return hljs.highlight(str, { language: lang }).value;
}; // };
mdIt.use(markdownKatex, { async function initMD() {
mdIt.use(
await Shiki({
themes: {
light: "vitesse-light",
dark: "vitesse-dark",
},
})
);
mdIt.use(markdownKatex, {
throwOnError: false, throwOnError: false,
errorColor: "#cc0000", errorColor: "#cc0000",
output: "html", output: "html",
}); });
mdInited.value = true;
}
const userStore = useUserStore(); const userStore = useUserStore();
const props = defineProps({ const props = defineProps({
@ -116,4 +135,8 @@ const props = defineProps({
const chat_messages = toRef(props, "chat_messages") as Ref<EntityChatMessage[]>; const chat_messages = toRef(props, "chat_messages") as Ref<EntityChatMessage[]>;
const fileBaseUrl = config.backend + "/api/v1/files"; const fileBaseUrl = config.backend + "/api/v1/files";
onMounted(() => {
initMD();
});
</script> </script>

View File

@ -1,5 +1,5 @@
@import url("https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@100..900&display=swap"); @import url("https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@100..900&display=swap");
@import "styles/github-markdown.css";
@tailwind base; @tailwind base;
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
@ -33,8 +33,18 @@ body {
font-style: normal; font-style: normal;
} }
@font-face { @font-face {
font-family: "Jetbrains Mono"; font-family: "Jetbrains Mono";
src: url("/src/assets/fonts/JetBrainsMono-Medium.woff2") format("woff2"); src: url("/src/assets/fonts/JetBrainsMono-Medium.woff2") format("woff2");
} }
.markdown-body pre > code {
font-family: "Jetbrains Mono", monospace;
/* 强制换行 */
white-space: pre-wrap;
/* background-color: #f5f5f5; */
}
.markdown-body ol {
list-style: decimal;
}

File diff suppressed because it is too large Load Diff