Skip to content

Commit

Permalink
fix(core)!: rename transformer hook token to span to avoid confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 14, 2024
1 parent 9016da6 commit eb3b811
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/guide/transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const code = await codeToHtml('foo\bar', {
if ([1, 3, 4].includes(line))
addClassToHast(node, 'highlight')
},
token(node, line, col) {
span(node, line, col) {
node.properties['data-token'] = `token:${line}:${col}`
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/shikiji-core/src/renderer-hast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export function tokensToHast(
tokenNode.properties.style = style

for (const transformer of transformers)
tokenNode = transformer?.token?.call(context, tokenNode, idx + 1, col, lineNode) || tokenNode
tokenNode = (transformer?.span || transformer?.token)?.call(context, tokenNode, idx + 1, col, lineNode) || tokenNode

lineNode.children.push(tokenNode)
col += token.content.length
Expand Down
8 changes: 7 additions & 1 deletion packages/shikiji-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,18 @@ export interface ShikijiTransformer {
/**
* Transform each token `<span>` element.
*/
token?(this: ShikijiTransformerContext, hast: Element, line: number, col: number, lineElement: Element): Element | void
span?(this: ShikijiTransformerContext, hast: Element, line: number, col: number, lineElement: Element): Element | void
/**
* Transform the generated HTML string before returning.
* This hook will only be called with `codeToHtml`.
*/
postprocess?(this: ShikijiTransformerContextCommon, html: string, options: CodeToHastOptions): string | void

// deprecated
/**
* @deprecated Use `span` instead
*/
token?(this: ShikijiTransformerContext, hast: Element, line: number, col: number, lineElement: Element): Element | void
}

export interface HtmlRendererOptionsCommon extends TransformerOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/shikiji/test/hast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ it('hasfocus support', async () => {
code(node) {
node.properties.class = 'language-php'
},
token(node, line, col, parent) {
span(node, line, col, parent) {
node.children.forEach((child) => {
if (child.type === 'text' && child.value.includes('[!code focus]')) {
parent.properties['data-has-focus'] = 'true'
Expand Down

0 comments on commit eb3b811

Please sign in to comment.