|
|
|
@ -1,7 +1,3 @@
|
|
|
|
|
import { customAlphabet } from 'nanoid'
|
|
|
|
|
import c from 'picocolors'
|
|
|
|
|
import type { ShikiTransformer } from 'shiki'
|
|
|
|
|
import { bundledLanguages, createHighlighter, isSpecialLang } from 'shiki'
|
|
|
|
|
import {
|
|
|
|
|
transformerCompactLineOptions,
|
|
|
|
|
transformerNotationDiff,
|
|
|
|
@ -10,9 +6,21 @@ import {
|
|
|
|
|
transformerNotationHighlight,
|
|
|
|
|
type TransformerCompactLineOption
|
|
|
|
|
} from '@shikijs/transformers'
|
|
|
|
|
import { customAlphabet } from 'nanoid'
|
|
|
|
|
import { createRequire } from 'node:module'
|
|
|
|
|
import c from 'picocolors'
|
|
|
|
|
import type { ShikiTransformer } from 'shiki'
|
|
|
|
|
import { createHighlighter, isSpecialLang } from 'shiki'
|
|
|
|
|
import { createSyncFn } from 'synckit'
|
|
|
|
|
import type { Logger } from 'vite'
|
|
|
|
|
import type { ShikiResolveLang } from 'worker_shikiResolveLang'
|
|
|
|
|
import type { MarkdownOptions, ThemeOptions } from '../markdown'
|
|
|
|
|
|
|
|
|
|
const require = createRequire(import.meta.url)
|
|
|
|
|
|
|
|
|
|
const resolveLangSync = createSyncFn<ShikiResolveLang>(
|
|
|
|
|
require.resolve('vitepress/dist/node/worker_shikiResolveLang.js')
|
|
|
|
|
)
|
|
|
|
|
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -62,7 +70,10 @@ export async function highlight(
|
|
|
|
|
typeof theme === 'object' && 'light' in theme && 'dark' in theme
|
|
|
|
|
? [theme.light, theme.dark]
|
|
|
|
|
: [theme],
|
|
|
|
|
langs: [...Object.keys(bundledLanguages), ...(options.languages || [])],
|
|
|
|
|
langs: [
|
|
|
|
|
...(options.languages || []),
|
|
|
|
|
...Object.values(options.languageAlias || {})
|
|
|
|
|
],
|
|
|
|
|
langAlias: options.languageAlias
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
@ -108,14 +119,19 @@ export async function highlight(
|
|
|
|
|
if (lang) {
|
|
|
|
|
const langLoaded = highlighter.getLoadedLanguages().includes(lang)
|
|
|
|
|
if (!langLoaded && !isSpecialLang(lang)) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
c.yellow(
|
|
|
|
|
`\nThe language '${lang}' is not loaded, falling back to '${
|
|
|
|
|
defaultLang || 'txt'
|
|
|
|
|
}' for syntax highlighting.`
|
|
|
|
|
const resolvedLang = resolveLangSync(lang)
|
|
|
|
|
if (!resolvedLang) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
c.yellow(
|
|
|
|
|
`\nThe language '${lang}' is not loaded, falling back to '${
|
|
|
|
|
defaultLang || 'txt'
|
|
|
|
|
}' for syntax highlighting.`
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
lang = defaultLang
|
|
|
|
|
lang = defaultLang
|
|
|
|
|
} else {
|
|
|
|
|
highlighter.loadLanguageSync(resolvedLang)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|