Skip to content

Commit

Permalink
fix(colorized-brackets): use object style htmlStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 20, 2025
1 parent 7ac62e1 commit eab5bd1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
8 changes: 5 additions & 3 deletions packages/colorized-brackets/src/colorizeBracketTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,20 @@ function assignColorToToken(
else {
const { defaultColor = 'light', cssVariablePrefix = '--shiki-' }
= shikiOptions
const styles: string[] = []
const styles: Record<string, string> = typeof token.htmlStyle === 'string'
? {}
: token.htmlStyle || {}

for (const [colorName, theme] of Object.entries(shikiOptions.themes)) {
const themeName = typeof theme === 'string' ? theme : theme?.name
const cssProperty
= colorName === defaultColor
? 'color'
: `${cssVariablePrefix}${colorName}`
styles.push(`${cssProperty}:${getColor(themes, themeName, level)}`)
styles[cssProperty] = getColor(themes, themeName, level)
}

token.htmlStyle = styles.join(';')
token.htmlStyle = styles
}
}

Expand Down
13 changes: 8 additions & 5 deletions packages/colorized-brackets/test/fixtures.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { lstatSync, readdirSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { join, sep } from 'node:path'
Expand All @@ -11,6 +12,8 @@ import {
prettifyBrackets,
} from './utils'

const SHOULD_LOG = false

/**
* `tests/samples` contains code snippets that annotate expected colors with `@colors` comments.
* `Y`, `P`, `B` are for the 3 levels of matched brackets (yellow, purple, blue), and `R` is for mismatched brackets (red).
Expand Down Expand Up @@ -63,11 +66,11 @@ describe('file-driven tests', async () => {
})
const actualBrackets = parseActualBrackets(html)
// Logging the colored brackets is much easier to read
/* eslint-disable no-console */
console.log(c.bold(fileName))
console.log(' Expected:', prettifyBrackets(expectedBrackets))
console.log(' Actual: ', prettifyBrackets(actualBrackets))
/* eslint-enable no-console */
if (SHOULD_LOG) {
console.log(c.bold(fileName))
console.log(' Expected:', prettifyBrackets(expectedBrackets))
console.log(' Actual: ', prettifyBrackets(actualBrackets))
}
expect(prettifyBrackets(actualBrackets, { noAnsi: true })).toEqual(
prettifyBrackets(expectedBrackets, { noAnsi: true }),
)
Expand Down

0 comments on commit eab5bd1

Please sign in to comment.