8000 feat: additional `codeToHast` util in transformer context · antfu/shikiji@fc857f0 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit fc857f0

Browse files
committed
feat: additional codeToHast util in transformer context
1 parent 533434a commit fc857f0

File tree

5 files changed

+10
-11
lines changed

5 files changed

+10
-11
lines changed

packages/shikiji/src/core/renderer-hast.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function codeToHast(
1919
options: CodeToHastOptions,
2020
transformerContext: ShikijiTransformerContextCommon = {
2121
meta: {},
22+
codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
2223
},
2324
) {
2425
let bg: string
@@ -138,9 +139,7 @@ function flattenToken(
138139
export function tokensToHast(
139140
tokens: ThemedToken[][],
140141
options: HtmlRendererOptions,
141-
transformerContext: ShikijiTransformerContextCommon = {
142-
meta: {},
143-
},
142+
transformerContext: ShikijiTransformerContextCommon,
144143
) {
145144
const {
146145
mergeWhitespaces = true,

packages/shikiji/src/core/renderer-html-themes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { codeToThemedTokens } from './tokenizer'
1010
* Get tokens with multiple themes
1111
*/
1212
export function codeToTokensWithThemes(
13-
context: ShikiInternal,
13+
internal: ShikiInternal,
1414
code: string,
1515
options: CodeToTokensWithThemesOptions,
1616
) {
@@ -19,7 +19,7 @@ export function codeToTokensWithThemes(
1919
.map(i => ({ color: i[0], theme: i[1]! }))
2020

2121
const tokens = syncThemesTokenization(
22-
...themes.map(t => codeToThemedTokens(context, code, {
22+
...themes.map(t => codeToThemedTokens(internal, code, {
2323
...options,
2424
theme: t.theme,
2525
includeExplanation: false,

packages/shikiji/src/core/renderer-html.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function codeToHtml(
1212
): string {
1313
const context: ShikijiTransformerContextCommon = {
1414
meta: {},
15+
codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
1516
}
1617

1718
let intput = code

packages/shikiji/src/core/tokenizer.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* ---------------------------------------------------------
22
* Copyright (C) Microsoft Corporation. All rights reserved.
33
*-------------------------------------------------------- */
4-
'use strict'
5-
64
import type { IGrammar, IRawTheme } from 'vscode-textmate'
75
import { INITIAL } from 'vscode-textmate'
86
import type { CodeToThemedTokensOptions, ShikiInternal, ThemedToken, ThemedTokenScopeExplanation } from '../types'
@@ -16,13 +14,13 @@ export interface TokenizeWithThemeOptions {
1614
}
1715

1816
export function codeToThemedTokens(
19-
context: ShikiInternal,
17+
internal: ShikiInternal,
2018
code: string,
2119
options: CodeToThemedTokensOptions = {},
2220
): ThemedToken[][] {
2321
const {
2422
lang = 'text',
25-
theme: themeName = context.getLoadedThemes()[0],
23+
theme: themeName = internal.getLoadedThemes()[0],
2624
includeExplanation = true,
2725
} = options
2826

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

34-
const { theme, colorMap } = context.setTheme(themeName)
32+
const { theme, colorMap } = internal.setTheme(themeName)
3533

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

39-
const _grammar = context.getLangGrammar(lang)
37+
const _grammar = internal.getLangGrammar(lang)
4038
return tokenizeWithTheme(code, _grammar, theme, colorMap, {
4139
includeExplanation,
4240
})

packages/shikiji/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ export interface ShikijiTransformerContextMeta {}
360360

361361
export interface ShikijiTransformerContextCommon {
362362
meta: ShikijiTransformerContextMeta
363+
codeToHast: (code: string, options: CodeToHastOptions) => Root
363364
}
364365

365366
export interface ShikijiTransformerContext extends ShikijiTransformerContextCommon {

0 commit comments

Comments
 (0)
0