Skip to content

Commit c0a0e40

Browse files
authored
fix: shuold cache Shiki setup result (#1788)
1 parent 787de2c commit c0a0e40

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/slidev/node/setups/shiki.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import fs from 'node:fs/promises'
22
import type { MarkdownItShikiOptions } from '@shikijs/markdown-it'
33
import type { ShikiSetup } from '@slidev/types'
4+
import type { Highlighter } from 'shiki'
45
import { bundledLanguages, createHighlighter } from 'shiki'
56
import { loadSetups } from './load'
67

8+
let cachedRoots: string[] | undefined
9+
let cachedShiki: {
10+
shiki: Highlighter
11+
shikiOptions: MarkdownItShikiOptions
12+
} | undefined
13+
714
export default async function setupShiki(roots: string[]) {
15+
// Here we use shallow equality because when server is restarted, the roots will be different object.
16+
if (cachedRoots === roots)
17+
return cachedShiki!
18+
cachedShiki?.shiki.dispose()
19+
820
const options = await loadSetups<ShikiSetup>(
921
roots,
1022
'shiki.ts',
@@ -44,8 +56,9 @@ export default async function setupShiki(roots: string[]) {
4456
themes: 'themes' in mergedOptions ? Object.values(mergedOptions.themes) : [mergedOptions.theme],
4557
})
4658

47-
return {
59+
cachedRoots = roots
60+
return cachedShiki = {
4861
shiki,
49-
shikiOptions: mergedOptions as MarkdownItShikiOptions,
62+
shikiOptions: mergedOptions,
5063
}
5164
}

0 commit comments

Comments
 (0)