Skip to content

Commit

Permalink
feat: add preprocess and postprocess hooks to transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Nov 22, 2023
1 parent 4cf00d9 commit c1aba81
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
11 changes: 10 additions & 1 deletion packages/shikiji/src/core/renderer-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
48 changes: 28 additions & 20 deletions packages/shikiji/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,8 @@ export interface CodeToThemedTokensOptions<Languages = string, Themes = string>
includeExplanation?: boolean
}

export interface CodeToHastOptionsCommon<Languages extends string = string> {
export interface CodeToHastOptionsCommon<Languages extends string = string> extends TransformerOptions {
lang: StringLiteralUnion<Languages | SpecialLanguage>
/**
* Transform the generated HAST tree.
*/
transformers?: ShikijiTransformer[]

/**
* @deprecated use `transformers` instead
*/
transforms?: ShikijiTransformer
}

export interface CodeToTokensWithThemesOptions<Languages = string, Themes = string> {
Expand Down Expand Up @@ -224,6 +215,18 @@ export interface CodeOptionsMeta {
meta?: Record<string, any>
}

export interface TransformerOptions {
/**
* Transform the generated HAST tree.
*/
transformers?: ShikijiTransformer[]

/**
* @deprecated use `transformers` instead
*/
transforms?: ShikijiTransformer
}

export type CodeToHastOptions<Languages extends string = string, Themes extends string = string> =
& CodeToHastOptionsCommon<Languages>
& CodeOptionsThemes<Themes>
Expand Down Expand Up @@ -297,31 +300,36 @@ export interface ShikijiTransformer {
*/
code?(this: ShikijiTransformerContext, hast: Element): Element | void
/**
* Transform each line element.
* Transform each line `<span class="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 `<span>` 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

/**
Expand Down

0 comments on commit c1aba81

Please sign in to comment.