From fc92a77a5d871b5252bcb82639f5c3551d5c70bb Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:07:34 +0530 Subject: [PATCH] fix: lazy load language definition on twoslash too closes #4334 --- src/node/markdown/plugins/highlight.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/node/markdown/plugins/highlight.ts b/src/node/markdown/plugins/highlight.ts index 5e0abf2862dc..12852caf7d94 100644 --- a/src/node/markdown/plugins/highlight.ts +++ b/src/node/markdown/plugins/highlight.ts @@ -65,10 +65,6 @@ export async function highlight( codeTransformers: userTransformers = [] } = options - const usingTwoslash = userTransformers.some( - ({ name }) => name === '@shikijs/vitepress-twoslash' - ) - const highlighter = await createHighlighter({ themes: typeof theme === 'object' && 'light' in theme && 'dark' in theme @@ -76,12 +72,7 @@ export async function highlight( : [theme], langs: [ ...(options.languages || []), - ...Object.values(options.languageAlias || {}), - - // patch for twoslash - https://github.com/vuejs/vitepress/issues/4334 - ...(usingTwoslash - ? Object.keys((await import('shiki')).bundledLanguages) - : []) + ...Object.values(options.languageAlias || {}) ], langAlias: options.languageAlias }) @@ -99,6 +90,14 @@ export async function highlight( return true } + // patch for twoslash - https://github.com/vuejs/vitepress/issues/4334 + const internal = highlighter.getInternalContext() + const getLanguage = internal.getLanguage + internal.getLanguage = (name) => { + loadLanguage(name) + return getLanguage.call(internal, name) + } + await options?.shikiSetup?.(highlighter) const transformers: ShikiTransformer[] = [