Skip to content

Commit 218c901

Browse files
authored
fix: allow singleton highlighter recovery after invalid language error (#1091)
1 parent 3c5d13e commit 218c901

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/core/src/constructors/bundle-factory.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,15 @@ export function makeSingletonHighlighter<L extends string, T extends string>(
183183
if (!_shiki) {
184184
_shiki = createHighlighter({
185185
...options,
186-
themes: options.themes || [],
187-
langs: options.langs || [],
186+
themes: [],
187+
langs: [],
188188
})
189-
return _shiki
189+
const s = await _shiki
190+
await Promise.all([
191+
s.loadTheme(...(options.themes as T[] || [])),
192+
s.loadLanguage(...(options.langs as L[] || [])),
193+
])
194+
return s
190195
}
191196
else {
192197
const s = await _shiki

packages/shiki/test/shorthands.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,22 @@ describe('should', () => {
6767
]
6868
`)
6969
})
70+
71+
it('should allow subsequent valid calls after first invalid language', async () => {
72+
const lang = 'invalid'
73+
const code = 'console.log("hello")'
74+
const theme = 'vitesse-light'
75+
76+
// First call with invalid language should throw
77+
await expect(codeToHtml(code, { lang, theme }))
78+
.rejects
79+
.toThrow(/Language `invalid` is not included in this bundle/)
80+
81+
// Subsequent call with valid language should succeed
82+
const result = await codeToHtml(code, { lang: 'javascript', theme })
83+
expect(result).toBeTruthy()
84+
// The HTML contains the code in structured format, check for "console" which appears in the output
85+
expect(result).toContain('console')
86+
expect(result).toContain('shiki')
87+
})
7088
})

0 commit comments

Comments
 (0)