Skip to content

Commit ed4477e

Browse files
committed
feat: call preprocess hook in codeToHast
1 parent 5225e29 commit ed4477e

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ export function codeToHast(
1919
options: CodeToHastOptions,
2020
transformerContext: ShikijiTransformerContextCommon = {
2121
meta: {},
22+
options,
2223
codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
2324
},
2425
) {
26+
let input = code
27+
for (const transformer of options.transformers || [])
28+
input = transformer.preprocess?.call(transformerContext, input, options) || input
29+
2530
let bg: string
2631
let fg: string
2732
let tokens: ThemedToken[][]
@@ -44,7 +49,7 @@ export function codeToHast(
4449

4550
const themeTokens = codeToTokensWithThemes(
4651
internal,
47-
code,
52+
input,
4853
options,
4954
)
5055

@@ -65,7 +70,7 @@ export function codeToHast(
6570
else if ('theme' in options) {
6671
tokens = codeToThemedTokens(
6772
internal,
68-
code,
73+
input,
6974
{
7075
...options,
7176
includeExplanation: false,

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ export function codeToHtml(
1212
): string {
1313
const context: ShikijiTransformerContextCommon = {
1414
meta: {},
15+
options,
1516
codeToHast: (_code, _options) => codeToHast(internal, _code, _options),
1617
}
1718

18-
let intput = code
19-
for (const transformer of options.transformers || [])
20-
intput = transformer.preprocess?.call(context, intput, options) || intput
21-
22-
let result = hastToHtml(codeToHast(internal, intput, options, context))
19+
let result = hastToHtml(codeToHast(internal, code, options, context))
2320

2421
for (const transformer of options.transformers || [])
2522
result = transformer.postprocess?.call(context, result, options) || result

packages/shikiji-core/src/types.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ export interface ShikijiTransformerContextMeta {}
410410

411411
export interface ShikijiTransformerContextCommon {
412412
meta: ShikijiTransformerContextMeta
413+
options: CodeToHastOptions
413414
codeToHast: (code: string, options: CodeToHastOptions) => Root
414415
}
415416

416417
export interface ShikijiTransformerContext extends ShikijiTransformerContextCommon {
417418
readonly tokens: ThemedToken[][]
418-
readonly options: CodeToHastOptions
419419
readonly root: Root
420420
readonly pre: Element
421421
readonly code: Element
@@ -453,15 +453,14 @@ export interface ShikijiTransformer {
453453

454454
/**
455455
* Transform the raw input code before passing to the highlighter.
456-
* This hook will only be called with `codeToHtml`.
456+
* This hook will only be called with `codeToHtml` or `codeToHast`.
457457
*/
458458
preprocess?(this: ShikijiTransformerContextCommon, code: string, options: CodeToHastOptions): string | void
459-
460459
/**
461460
* Transform the generated HTML string before returning.
462461
* This hook will only be called with `codeToHtml`.
463462
*/
464-
postprocess?(this: ShikijiTransformerContextCommon, code: string, options: CodeToHastOptions): string | void
463+
postprocess?(this: ShikijiTransformerContextCommon, html: string, options: CodeToHastOptions): string | void
465464
}
466465

467466
export interface HtmlRendererOptionsCommon extends TransformerOptions {

0 commit comments

Comments
 (0)