From 621d744efc84696df133729643c3087080daabda Mon Sep 17 00:00:00 2001 From: passthem Date: Fri, 27 Mar 2026 15:17:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=20shiki=20=E7=8E=AF=E5=A2=83=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E6=9F=90=E8=AF=AD=E8=A8=80=E6=97=B6=20Fallba?= =?UTF-8?q?ck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/markdown.ts | 51 ++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/lib/markdown.ts b/src/lib/markdown.ts index 0f8ebf0..cc0da34 100644 --- a/src/lib/markdown.ts +++ b/src/lib/markdown.ts @@ -4,6 +4,31 @@ import { createHighlighter, type Highlighter } from 'shiki' let highlighter: Highlighter | null = null +const allowed_langs = [ + 'python', + 'javascript', + 'typescript', + 'typst', + 'markdown', + 'json', + 'toml', + 'yaml', + 'bash', + 'c', + 'c++', + 'rust', + 'go', + 'zig', + 'makefile', + 'make', + 'nim', + 'nix', + 'kdl', + 'md', + 'sh', + 'mermaid', +] + /** * Markdown 渲染函数。 * @@ -14,29 +39,7 @@ export async function renderMarkdown(content: string): Promise { highlighter = await createHighlighter({ themes: ['one-light', 'one-dark-pro'], /* 额...要我自己定义需要的所有语言吗 */ - langs: [ - 'python', - 'javascript', - 'typescript', - 'typst', - 'markdown', - 'json', - 'toml', - 'yaml', - 'bash', - 'c', - 'c++', - 'rust', - 'go', - 'zig', - 'makefile', - 'make', - 'nim', - 'nix', - 'kdl', - 'md', - 'sh', - ], + langs: allowed_langs, }) } @@ -45,7 +48,7 @@ export async function renderMarkdown(content: string): Promise { linkify: true, highlight: (code, lang) => { return highlighter!.codeToHtml(code, { - lang: lang || 'text', + lang: allowed_langs.indexOf(lang) != -1 ? lang : 'text', themes: { light: 'one-light', dark: 'one-dark-pro',