diff --git a/packages/shiki/src/__tests__/simple.test.ts b/packages/shiki/src/__tests__/simple.test.ts index 12bc6b800..6c33fdb62 100644 --- a/packages/shiki/src/__tests__/simple.test.ts +++ b/packages/shiki/src/__tests__/simple.test.ts @@ -1,3 +1,4 @@ +import { BUNDLED_LANGUAGES, Lang } from '../../dist' import { getHighlighter } from '../index' test('Nord highlighter highlights simple JavaScript', async () => { @@ -9,18 +10,72 @@ test('Nord highlighter highlights simple JavaScript', async () => { expect(out).toMatchSnapshot() }) -test('Highlighter can load languages', async () => { +test('Highlighter loads languages and embedded languages one level deep', async () => { const highlighter = await getHighlighter({ theme: 'nord', langs: ['js'] }) await highlighter.loadLanguage('vue') - await highlighter.loadLanguage('css') - await highlighter.loadLanguage('yaml') await highlighter.loadLanguage('php') expect(highlighter.getLoadedLanguages()).toEqual( - expect.arrayContaining(['javascript', 'js', 'vue', 'css', 'yaml', 'php']) + expect.arrayContaining([ + 'javascript', + 'js', + 'vue', + 'json', + 'haml', + 'vue-html', + 'sass', + 'less', + 'postcss', + 'pug', + 'jade', + 'stylus', + 'styl', + 'coffee', + 'scss', + 'markdown', + 'md', + 'css', + 'typescript', + 'ts', + 'php', + 'xml', + 'sql', + 'html' + ]) + ) +}) + +test('Highlighter does not load embedded languages when embedded depth to load is zero', async () => { + const highlighter = await getHighlighter({ + theme: 'nord', + langs: ['js'] + }) + + await highlighter.loadLanguage('vue', 0) + await highlighter.loadLanguage('php', 0) + + expect(highlighter.getLoadedLanguages()).toEqual( + expect.arrayContaining(['javascript', 'js', 'vue', 'php']) + ) +}) + +test('Highlighter can load all languages', async () => { + const highlighter = await getHighlighter({ + theme: 'nord', + langs: ['js'] + }) + + await highlighter.loadLanguage('abap') + + await Promise.all( + BUNDLED_LANGUAGES.map(async lang => await highlighter.loadLanguage(lang.id as Lang)) + ) + + expect(highlighter.getLoadedLanguages()).toEqual( + expect.arrayContaining(BUNDLED_LANGUAGES.map(lang => lang.id as Lang)) ) }) diff --git a/packages/shiki/src/highlighter.ts b/packages/shiki/src/highlighter.ts index b9c5f57c6..14583f83c 100644 --- a/packages/shiki/src/highlighter.ts +++ b/packages/shiki/src/highlighter.ts @@ -138,11 +138,28 @@ export async function getHighlighter(options: HighlighterOptions): Promise !getLoadedLanguages().includes(embeddedLang) + ) + + await Promise.all( + notLoaded.map( + async embeddedLang => await loadLanguage(embeddedLang, embeddedLangMaxDepth, ++depth) + ) + ) + } } function getLoadedThemes() { diff --git a/packages/shiki/src/types.ts b/packages/shiki/src/types.ts index 9a4d84033..050fd95d3 100644 --- a/packages/shiki/src/types.ts +++ b/packages/shiki/src/types.ts @@ -64,9 +64,13 @@ export interface Highlighter { loadTheme(theme: IThemeRegistration): Promise /** - * Load a language + * Load a language with its embedded languages. */ - loadLanguage(lang: ILanguageRegistration | Lang): Promise + loadLanguage( + lang: ILanguageRegistration | Lang, + embeddedLangMaxDepth?: number, + depth?: number + ): Promise /** * Get all loaded themes