Skip to content

Commit

Permalink
feat: expose getLastGrammarState shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 28, 2024
1 parent 8a8faf9 commit f2b3956
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/core/src/bundle-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Root } from 'hast'
import type { BundledHighlighterOptions, CodeToHastOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, HighlighterCoreOptions, HighlighterGeneric, LanguageInput, RequireKeys, SpecialLanguage, SpecialTheme, ThemeInput, ThemedToken, ThemedTokenWithVariants, TokensResult } from './types'
import type { BundledHighlighterOptions, CodeToHastOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, GrammarState, HighlighterCoreOptions, HighlighterGeneric, LanguageInput, RequireKeys, SpecialLanguage, SpecialTheme, ThemeInput, ThemedToken, ThemedTokenWithVariants, TokensResult } from './types'
import { isSpecialLang, isSpecialTheme } from './utils'
import { createHighlighterCore } from './highlighter'
import { ShikiError } from './error'
Expand Down Expand Up @@ -115,9 +115,15 @@ export interface ShorthandsBundle<L extends string, T extends string> {
codeToTokensWithThemes: (code: string, options: RequireKeys<CodeToTokensWithThemesOptions<L, T>, 'themes' | 'lang'>) => Promise<ThemedTokenWithVariants[][]>

/**
* Get internal singleton highlighter.
* Get the singleton highlighter.
*/
getSingletonHighlighter: (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>

/**
* Shorthand for `getLastGrammarState` with auto-loaded theme and language.
* A singleton highlighter it maintained internally.
*/
getLastGrammarState: (code: string, options: CodeToTokensBaseOptions<L, T>) => Promise<GrammarState>
}

export function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>) {
Expand Down Expand Up @@ -196,5 +202,13 @@ export function createSingletonShorthands<L extends string, T extends string >(
})
return shiki.codeToTokensWithThemes(code, options)
},

async getLastGrammarState(code, options) {
const shiki = await getSingletonHighlighter({
langs: [options.lang as L],
themes: [options.theme as T],
})
return shiki.getLastGrammarState(code, options)
},
}
}
1 change: 1 addition & 0 deletions packages/shiki/src/bundle-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const {
codeToTokensBase,
codeToTokensWithThemes,
getSingletonHighlighter,
getLastGrammarState,
} = /* @__PURE__ */ createSingletonShorthands<
BundledLanguage,
BundledTheme
Expand Down
1 change: 1 addition & 0 deletions packages/shiki/src/bundle-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const {
codeToTokens,
codeToTokensWithThemes,
getSingletonHighlighter,
getLastGrammarState,
} = /* @__PURE__ */ createSingletonShorthands<
BundledLanguage,
BundledTheme
Expand Down

0 comments on commit f2b3956

Please sign in to comment.