Skip to content

Commit f4d3fe5

Browse files
fix(colorized-brackets): fix default color does not work with colorized-brackets (#1082)
1 parent fd7326a commit f4d3fe5

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

packages/colorized-brackets/src/colorizeBracketTokens.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CodeOptionsSingleTheme, CodeOptionsThemes, ThemedToken } from 'shiki'
22
import type { TransformerColorizedBracketsOptions } from './types'
3+
import { ShikiError } from 'shiki'
34
import builtInThemes from './themes'
45
import { getEmbeddedLang, resolveConfig, shouldIgnoreToken } from './utils'
56

@@ -119,6 +120,15 @@ function assignColorToToken(
119120
styles[cssProperty] = getColor(themes, themeName, level)
120121
}
121122

123+
if (defaultColor === 'light-dark()') {
124+
const lightColor = styles[`${cssVariablePrefix}light`]
125+
const darkColor = styles[`${cssVariablePrefix}dark`]
126+
if (!lightColor || !darkColor) {
127+
throw new ShikiError('When using `defaultColor: "light-dark()"`, you must provide both `light` and `dark` themes')
128+
}
129+
styles.color = `light-dark(${lightColor},${darkColor})`
130+
}
131+
122132
token.htmlStyle = styles
123133
}
124134
}

packages/colorized-brackets/test/dual-themes.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,36 @@ describe('dual themes', async () => {
7272
expect(htmlStr).toContain('<span style="color:y;--shiki-light:Y">{</span>')
7373
})
7474

75+
it('light-dark() default', () => {
76+
const htmlStr = highlighter.codeToHtml('{}', {
77+
lang,
78+
themes: { light: 'light-plus', dark: 'dark-plus' },
79+
defaultColor: 'light-dark()',
80+
transformers: [
81+
transformerColorizedBrackets({
82+
themes: {
83+
'light-plus': ['Y', 'P', 'B', 'R'],
84+
'dark-plus': ['y', 'p', 'b', 'r'],
85+
},
86+
}),
87+
],
88+
})
89+
expect(htmlStr).toContain(
90+
'<span style="color:light-dark(Y,y);--shiki-light:Y;--shiki-dark:y">{</span>',
91+
)
92+
})
93+
94+
it('light-dark() throws error if light and dark themes are not provided', () => {
95+
expect(() => highlighter.codeToHtml('{}', {
96+
lang,
97+
themes: { light: 'light-plus' },
98+
defaultColor: 'light-dark()',
99+
transformers: [
100+
transformerColorizedBrackets(),
101+
],
102+
})).throws('When using `defaultColor: "light-dark()"`, you must provide both `light` and `dark` themes')
103+
})
104+
75105
it('no default', () => {
76106
const htmlStr = highlighter.codeToHtml('{}', {
77107
lang,

0 commit comments

Comments
 (0)