Skip to content

Commit

Permalink
fix: fix lang
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jan 24, 2025
1 parent 2cbfbaa commit 2eeec6e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createHighlighter, isSpecialLang } from 'shiki'
import { createSyncFn } from 'synckit'
import type { ShikiResolveLang } from '../../resolveLang.js'
import type { ShikiHighlightOptions } from '../../types.js'
import { resolveLanguage } from '../../utils.js'

const require = createRequire(import.meta.url)

Expand All @@ -26,7 +27,7 @@ export const createShikiHighlighter = async ({
...options
}: ShikiHighlightOptions = {}): Promise<{
highlighter: HighlighterGeneric<BundledLanguage, BundledTheme>
loadLang: (lang: LanguageRegistration | string) => boolean
loadLang: (lang: string) => boolean
}> => {
const highlighter = await createHighlighter({
langs: [...langs, ...Object.values(langAlias)],
Expand All @@ -37,29 +38,29 @@ export const createShikiHighlighter = async ({
: [options.theme ?? 'nord'],
})

const loadLang = (langConfig: LanguageRegistration | string): boolean => {
const lang = typeof langConfig === 'string' ? langConfig : langConfig.name
const loadLang = (lang: string): boolean => {
if (isSpecialLang(lang)) return true

if (
!isSpecialLang(lang) &&
!highlighter.getLoadedLanguages().includes(lang)
) {
const loadedLangs = highlighter.getLoadedLanguages()

if (!loadedLangs.includes(lang)) {
const resolvedLang = resolveLangSync(lang)

if (!resolvedLang.length) return false

console.log('loading lang', lang)

highlighter.loadLanguageSync(resolvedLang)
}

return true
}

// patch for twoslash - https://github.com/vuejs/vitepress/issues/4334
const rawGetLanguage = highlighter.getLanguage

highlighter.getLanguage = (name) => {
loadLang(name)
const lang = typeof name === 'string' ? name : name.name

loadLang(resolveLanguage(lang))

return rawGetLanguage.call(highlighter, name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getLanguage = (
): string => {
let result = resolveLanguage(lang)

if (result && !loadLang(lang)) {
if (result && !loadLang(result)) {
// warn for unknown languages only once
if (logLevel !== 'silent' && !WARNED_LANGS.has(result)) {
logger.warn(
Expand Down

0 comments on commit 2eeec6e

Please sign in to comment.