diff --git a/packages/shikiji/src/core/renderer-html.ts b/packages/shikiji/src/core/renderer-html.ts index db100dc45..dab40a660 100644 --- a/packages/shikiji/src/core/renderer-html.ts +++ b/packages/shikiji/src/core/renderer-html.ts @@ -10,5 +10,14 @@ export function codeToHtml( code: string, options: CodeToHastOptions, ): string { - return hastToHtml(codeToHast(context, code, options)) + let intput = code + for (const transformer of options.transformers || []) + intput = transformer.preprocess?.(intput, options) || intput + + let result = hastToHtml(codeToHast(context, intput, options)) + + for (const transformer of options.transformers || []) + result = transformer.postprocess?.(result, options) || result + + return result } diff --git a/packages/shikiji/src/types.ts b/packages/shikiji/src/types.ts index e0a1a7f2e..cf8018229 100644 --- a/packages/shikiji/src/types.ts +++ b/packages/shikiji/src/types.ts @@ -123,17 +123,8 @@ export interface CodeToThemedTokensOptions includeExplanation?: boolean } -export interface CodeToHastOptionsCommon { +export interface CodeToHastOptionsCommon extends TransformerOptions { lang: StringLiteralUnion - /** - * Transform the generated HAST tree. - */ - transformers?: ShikijiTransformer[] - - /** - * @deprecated use `transformers` instead - */ - transforms?: ShikijiTransformer } export interface CodeToTokensWithThemesOptions { @@ -224,6 +215,18 @@ export interface CodeOptionsMeta { meta?: Record } +export interface TransformerOptions { + /** + * Transform the generated HAST tree. + */ + transformers?: ShikijiTransformer[] + + /** + * @deprecated use `transformers` instead + */ + transforms?: ShikijiTransformer +} + export type CodeToHastOptions = & CodeToHastOptionsCommon & CodeOptionsThemes @@ -297,31 +300,36 @@ export interface ShikijiTransformer { */ code?(this: ShikijiTransformerContext, hast: Element): Element | void /** - * Transform each line element. + * Transform each line `` element. * * @param hast * @param line 1-based line number */ line?(this: ShikijiTransformerContext, hast: Element, line: number): Element | void /** - * Transform each token element. + * Transform each token `` element. */ token?(this: ShikijiTransformerContext, hast: Element, line: number, col: number, lineElement: Element): Element | void + + /** + * Transform the raw input code before passing to the highlighter. + * This hook will only be called with `codeToHtml`. + */ + preprocess?(code: string, options: CodeToHastOptions): string | undefined + + /** + * Transform the generated HTML string before returning. + * This hook will only be called with `codeToHtml`. + */ + postprocess?(code: string, options: CodeToHastOptions): string | undefined } -export interface HtmlRendererOptionsCommon { +export interface HtmlRendererOptionsCommon extends TransformerOptions { lang?: string langId?: string fg?: string bg?: string - transformers?: ShikijiTransformer[] - - /** - * @deprecated use `transformers` instead - */ - transforms?: ShikijiTransformer - themeName?: string /**