Skip to content

Commit 70be44f

Browse files
committed
refactor(OACodeSamples): preload highlighter languages using Shiki
1 parent da7c4c6 commit 70be44f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/components/Sample/OACodeSamples.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { OperationData } from '@/lib/operation/operationData'
33
import type { LanguageConfig } from '../../composables/useTheme'
44
import { OPERATION_DATA_KEY } from '@/lib/operation/operationData'
55
import { inject, ref, watch } from 'vue'
6+
import { useShiki } from '../../composables/useShiki'
67
import { useTheme } from '../../composables/useTheme'
78
import OACodeBlock from '../Common/OACodeBlock.vue'
89
@@ -24,6 +25,8 @@ const operationData = inject(OPERATION_DATA_KEY) as OperationData
2425
2526
const themeConfig = useTheme()
2627
28+
const shiki = useShiki()
29+
2730
const availableLanguages = themeConfig.getCodeSamplesAvailableLanguages()
2831
2932
const generator = themeConfig.getCodeSamplesGenerator()
@@ -83,6 +86,15 @@ watch(operationData.playground.request, async (playgroundRequest, _, onInvalidat
8386
if (cancelled) {
8487
return
8588
}
89+
90+
// Preload all highlighter languages
91+
const uniqueHighlighters = [...new Set(nextSamples.map(s => s.highlighter).filter(Boolean))]
92+
await Promise.all(uniqueHighlighters.map(lang => shiki.ensureLanguage(lang)))
93+
94+
if (cancelled) {
95+
return
96+
}
97+
8698
samples.value = nextSamples
8799
88100
if (!activeSampleKey.value || !samples.value.some(s => s.key === activeSampleKey.value)) {
@@ -94,6 +106,7 @@ watch(operationData.playground.request, async (playgroundRequest, _, onInvalidat
94106
}
95107
}, {
96108
deep: true,
109+
immediate: true,
97110
})
98111
</script>
99112

0 commit comments

Comments
 (0)