11import { ExpressiveCodeLine , type ExpressiveCodePlugin , ExpressiveCodeTheme , InlineStyleAnnotation } from '@expressive-code/core'
2- import type { ThemedToken , ShikiTransformer , Awaitable , RegexEngine , ThemeInput , StringLiteralUnion } from 'shiki/core'
2+ import type { ThemedToken , ShikiTransformer , Awaitable , RegexEngine , ThemeInput } from 'shiki/core'
33import { ensureLanguagesAreLoaded , ensureThemeIsLoaded , getCachedHighlighter , runHighlighterTask , type ShikiHighlighter } from './highlighter'
4- import type { LanguageInput } from './languages'
4+ import type { LanguageInput , LanguageAlias } from './languages'
55import { runPreprocessHook , runTokensHook , validateTransformers } from './transformers'
66
77export interface PluginShikiCoreOptions < L extends string > {
@@ -12,11 +12,13 @@ export interface PluginShikiCoreOptions<L extends string> {
1212 * The values can either be bundled languages, or additional languages
1313 * defined in `langs`.
1414 *
15+ * Note that when referencing a language provided in `langs`, the value
16+ * must match the `name` property (the language ID) of the language object
17+ * that was provided in `langs`.
18+ *
1519 * @example { 'mjs': 'javascript' }
1620 */
17- // TODO: This should be simply L but for backwards compat, need to support string. This should be changed so that you cannot provide
18- // a mapping to a language that does not exist in the bundle.
19- langAlias ?: Record < string , StringLiteralUnion < L > > | undefined
21+ langAlias ?: LanguageAlias < L > | undefined
2022 /**
2123 * By default, the additional languages defined in `langs` are only available in
2224 * top-level code blocks contained directly in their parent Markdown or MDX document.
@@ -53,7 +55,15 @@ export interface PluginShikiCoreOptions<L extends string> {
5355
5456export interface PluginShikiBundleOptions < L extends string , T extends string > extends PluginShikiCoreOptions < L > {
5557 /**
56- * A list of languages from your `bundledLangs` that you want eagerly loaded.
58+ * A list of additional languages that should be available for syntax highlighting.
59+ *
60+ * You can pass any of the language input types supported by Shiki, e.g.:
61+ * - `import('./some-exported-grammar.mjs')`
62+ * - `async () => JSON.parse(await fs.readFile('some-json-grammar.json', 'utf-8'))`
63+ *
64+ * Any languages specified will be eagerly loaded.
65+ *
66+ * See the Shiki documentation for more information on [Loading Custom Languages](https://shiki.style/guide/load-lang).
5767 */
5868 langs ?: LanguageInput [ ] | undefined
5969 /**
@@ -83,7 +93,7 @@ export interface PluginShikiBundleOptions<L extends string, T extends string> ex
8393 bundledThemes : Record < T , ThemeInput >
8494}
8595
86- export interface PluginShikiWithHighlighterOptions < L extends string , T extends string > extends PluginShikiCoreOptions < L > {
96+ export interface PluginShikiWithHighlighterOptions < L extends string , T extends string > extends Omit < PluginShikiCoreOptions < L > , 'langAlias' > {
8797 /**
8898 * Allows full control over the highlighter used.
8999 *
@@ -129,13 +139,18 @@ enum FontStyle {
129139}
130140
131141export function pluginShikiBundle < L extends string , T extends string > ( options : PluginShikiBundleOptions < L , T > ) : ExpressiveCodePlugin {
132- return pluginShikiWithHighlighter ( {
142+ return createPlugin ( {
133143 ...options ,
134144 highlighter : ( ) => getCachedHighlighter ( options ) ,
135145 } )
136146}
137147
138148export function pluginShikiWithHighlighter < L extends string , T extends string > ( options : PluginShikiWithHighlighterOptions < L , T > ) : ExpressiveCodePlugin {
149+ return createPlugin ( options )
150+ }
151+
152+ type CreatePluginOptions < L extends string , T extends string > = PluginShikiWithHighlighterOptions < L , T > & { langAlias ?: LanguageAlias < L > | undefined }
153+ function createPlugin < L extends string , T extends string > ( options : CreatePluginOptions < L , T > ) : ExpressiveCodePlugin {
139154 const { langAlias = { } , highlighter : getHighlighter } = options
140155
141156 // Validate all configured transformers
0 commit comments