Skip to content

Commit 4dac35f

Browse files
committed
fix: (temporary patch) lang lazy load not working with twoslash
1 parent dcb8450 commit 4dac35f

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

src/node/cli.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ import minimist from 'minimist'
22
import c from 'picocolors'
33
import { createLogger } from 'vite'
44
import { build, createServer, serve } from '.'
5-
import { init } from './init/init'
65
import { version } from '../../package.json'
6+
import { init } from './init/init'
77
import { bindShortcuts } from './shortcuts'
88

9+
if (process.env.DEBUG) {
10+
Error.stackTraceLimit = Infinity
11+
}
12+
913
const argv: any = minimist(process.argv.slice(2))
1014

1115
const logVersion = (logger = createLogger()) => {

src/node/markdown/plugins/highlight.ts

+32-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { customAlphabet } from 'nanoid'
1010
import { createRequire } from 'node:module'
1111
import c from 'picocolors'
12-
import type { ShikiTransformer } from 'shiki'
12+
import type { LanguageRegistration, ShikiTransformer } from 'shiki'
1313
import { createHighlighter, isSpecialLang } from 'shiki'
1414
import { createSyncFn } from 'synckit'
1515
import type { Logger } from 'vite'
@@ -61,22 +61,44 @@ export async function highlight(
6161
logger: Pick<Logger, 'warn'> = console
6262
): Promise<[(str: string, lang: string, attrs: string) => string, () => void]> {
6363
const {
64-
defaultHighlightLang: defaultLang = '',
64+
defaultHighlightLang: defaultLang = 'txt',
6565
codeTransformers: userTransformers = []
6666
} = options
6767

68+
const usingTwoslash = userTransformers.some(
69+
({ name }) => name === '@shikijs/vitepress-twoslash'
70+
)
71+
6872
const highlighter = await createHighlighter({
6973
themes:
7074
typeof theme === 'object' && 'light' in theme && 'dark' in theme
7175
? [theme.light, theme.dark]
7276
: [theme],
7377
langs: [
7478
...(options.languages || []),
75-
...Object.values(options.languageAlias || {})
79+
...Object.values(options.languageAlias || {}),
80+
81+
// patch for twoslash - https://github.com/vuejs/vitepress/issues/4334
82+
...(usingTwoslash
83+
? Object.keys((await import('shiki')).bundledLanguages)
84+
: [])
7685
],
7786
langAlias: options.languageAlias
7887
})
7988

89+
function loadLanguage(name: string | LanguageRegistration) {
90+
const lang = typeof name === 'string' ? name : name.name
91+
if (
92+
!isSpecialLang(lang) &&
93+
!highlighter.getLoadedLanguages().includes(lang)
94+
) {
95+
const resolvedLang = resolveLangSync(lang)
96+
if (resolvedLang.length) highlighter.loadLanguageSync(resolvedLang)
97+
else return false
98+
}
99+
return true
100+
}
101+
80102
await options?.shikiSetup?.(highlighter)
81103

82104
const transformers: ShikiTransformer[] = [
@@ -116,23 +138,13 @@ export async function highlight(
116138
.replace(vueRE, '')
117139
.toLowerCase() || defaultLang
118140

119-
if (lang) {
120-
const langLoaded = highlighter.getLoadedLanguages().includes(lang)
121-
if (!langLoaded && !isSpecialLang(lang)) {
122-
const resolvedLang = resolveLangSync(lang)
123-
if (!resolvedLang.length) {
124-
logger.warn(
125-
c.yellow(
126-
`\nThe language '${lang}' is not loaded, falling back to '${
127-
defaultLang || 'txt'
128-
}' for syntax highlighting.`
129-
)
130-
)
131-
lang = defaultLang
132-
} else {
133-
highlighter.loadLanguageSync(resolvedLang)
134-
}
135-
}
141+
if (!loadLanguage(lang)) {
142+
logger.warn(
143+
c.yellow(
144+
`\nThe language '${lang}' is not loaded, falling back to '${defaultLang}' for syntax highlighting.`
145+
)
146+
)
147+
lang = defaultLang
136148
}
137149

138150
const lineOptions = attrsToLines(attrs)

src/node/worker_shikiResolveLang.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { bundledLanguages, type DynamicImportLanguageRegistration } from 'shiki'
1+
import {
2+
bundledLanguages,
3+
type DynamicImportLanguageRegistration,
4+
type LanguageRegistration
5+
} from 'shiki'
26
import { runAsWorker } from 'synckit'
37

48
async function resolveLang(lang: string) {
@@ -10,7 +14,7 @@ async function resolveLang(lang: string) {
1014
>
1115
)
1216
[lang]?.()
13-
.then((m) => m.default) || []
17+
.then((m) => m.default) || ([] as LanguageRegistration[])
1418
)
1519
}
1620

0 commit comments

Comments
 (0)