在 shiki 环境不存在某语言时 Fallback
This commit is contained in:
@ -4,6 +4,31 @@ import { createHighlighter, type Highlighter } from 'shiki'
|
|||||||
|
|
||||||
let highlighter: Highlighter | null = null
|
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 渲染函数。
|
* Markdown 渲染函数。
|
||||||
*
|
*
|
||||||
@ -14,29 +39,7 @@ export async function renderMarkdown(content: string): Promise<string> {
|
|||||||
highlighter = await createHighlighter({
|
highlighter = await createHighlighter({
|
||||||
themes: ['one-light', 'one-dark-pro'],
|
themes: ['one-light', 'one-dark-pro'],
|
||||||
/* 额...要我自己定义需要的所有语言吗 */
|
/* 额...要我自己定义需要的所有语言吗 */
|
||||||
langs: [
|
langs: allowed_langs,
|
||||||
'python',
|
|
||||||
'javascript',
|
|
||||||
'typescript',
|
|
||||||
'typst',
|
|
||||||
'markdown',
|
|
||||||
'json',
|
|
||||||
'toml',
|
|
||||||
'yaml',
|
|
||||||
'bash',
|
|
||||||
'c',
|
|
||||||
'c++',
|
|
||||||
'rust',
|
|
||||||
'go',
|
|
||||||
'zig',
|
|
||||||
'makefile',
|
|
||||||
'make',
|
|
||||||
'nim',
|
|
||||||
'nix',
|
|
||||||
'kdl',
|
|
||||||
'md',
|
|
||||||
'sh',
|
|
||||||
],
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +48,7 @@ export async function renderMarkdown(content: string): Promise<string> {
|
|||||||
linkify: true,
|
linkify: true,
|
||||||
highlight: (code, lang) => {
|
highlight: (code, lang) => {
|
||||||
return highlighter!.codeToHtml(code, {
|
return highlighter!.codeToHtml(code, {
|
||||||
lang: lang || 'text',
|
lang: allowed_langs.indexOf(lang) != -1 ? lang : 'text',
|
||||||
themes: {
|
themes: {
|
||||||
light: 'one-light',
|
light: 'one-light',
|
||||||
dark: 'one-dark-pro',
|
dark: 'one-dark-pro',
|
||||||
|
|||||||
Reference in New Issue
Block a user