在 shiki 环境不存在某语言时 Fallback

This commit is contained in:
2026-03-27 15:17:54 +08:00
parent 7501bb9f57
commit 621d744efc

View File

@ -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<string> {
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<string> {
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',