Skip to content

Commit

Permalink
feat: additional codeToHast util in transformer context
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 11, 2023
1 parent 533434a commit fc857f0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
5 changes: 2 additions & 3 deletions packages/shikiji/src/core/renderer-hast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function codeToHast(
options: CodeToHastOptions,
transformerContext: ShikijiTransformerContextCommon = {
meta: {},
codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
},
) {
let bg: string
Expand Down Expand Up @@ -138,9 +139,7 @@ function flattenToken(
export function tokensToHast(
tokens: ThemedToken[][],
options: HtmlRendererOptions,
transformerContext: ShikijiTransformerContextCommon = {
meta: {},
},
transformerContext: ShikijiTransformerContextCommon,
) {
const {
mergeWhitespaces = true,
Expand Down
4 changes: 2 additions & 2 deletions packages/shikiji/src/core/renderer-html-themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { codeToThemedTokens } from './tokenizer'
* Get tokens with multiple themes
*/
export function codeToTokensWithThemes(
context: ShikiInternal,
internal: ShikiInternal,
code: string,
options: CodeToTokensWithThemesOptions,
) {
Expand All @@ -19,7 +19,7 @@ export function codeToTokensWithThemes(
.map(i => ({ color: i[0], theme: i[1]! }))

const tokens = syncThemesTokenization(
...themes.map(t => codeToThemedTokens(context, code, {
...themes.map(t => codeToThemedTokens(internal, code, {
...options,
theme: t.theme,
includeExplanation: false,
Expand Down
1 change: 1 addition & 0 deletions packages/shikiji/src/core/renderer-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function codeToHtml(
): string {
const context: ShikijiTransformerContextCommon = {
meta: {},
codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
}

let intput = code
Expand Down
10 changes: 4 additions & 6 deletions packages/shikiji/src/core/tokenizer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* ---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*-------------------------------------------------------- */
'use strict'

import type { IGrammar, IRawTheme } from 'vscode-textmate'
import { INITIAL } from 'vscode-textmate'
import type { CodeToThemedTokensOptions, ShikiInternal, ThemedToken, ThemedTokenScopeExplanation } from '../types'
Expand All @@ -16,13 +14,13 @@ export interface TokenizeWithThemeOptions {
}

export function codeToThemedTokens(
context: ShikiInternal,
internal: ShikiInternal,
code: string,
options: CodeToThemedTokensOptions = {},
): ThemedToken[][] {
const {
lang = 'text',
theme: themeName = context.getLoadedThemes()[0],
theme: themeName = internal.getLoadedThemes()[0],
includeExplanation = true,
} = options

Expand All @@ -31,12 +29,12 @@ export function codeToThemedTokens(
return [...lines.map(line => [{ content: line }])]
}

const { theme, colorMap } = context.setTheme(themeName)
const { theme, colorMap } = internal.setTheme(themeName)

if (lang === 'ansi')
return tokenizeAnsiWithTheme(theme, code)

const _grammar = context.getLangGrammar(lang)
const _grammar = internal.getLangGrammar(lang)
return tokenizeWithTheme(code, _grammar, theme, colorMap, {
includeExplanation,
})
Expand Down
1 change: 1 addition & 0 deletions packages/shikiji/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export interface ShikijiTransformerContextMeta {}

export interface ShikijiTransformerContextCommon {
meta: ShikijiTransformerContextMeta
codeToHast: (code: string, options: CodeToHastOptions) => Root
}

export interface ShikijiTransformerContext extends ShikijiTransformerContextCommon {
Expand Down

0 comments on commit fc857f0

Please sign in to comment.