|
1 | 1 | import { expect, it } from 'vitest'
|
2 |
| -import { codeToHtml } from '../src' |
| 2 | +import type { ThemeRegistrationResolved } from '../src' |
| 3 | +import { codeToHtml, resolveColorReplacements } from '../src' |
3 | 4 |
|
4 |
| -it('colorReplacements', async () => { |
| 5 | +it('resolveColorReplacements', async () => { |
| 6 | + expect(resolveColorReplacements('nord', { |
| 7 | + colorReplacements: { |
| 8 | + '#000000': '#ffffff', |
| 9 | + 'nord': { |
| 10 | + '#000000': '#222222', |
| 11 | + '#abcabc': '#defdef', |
| 12 | + '#ffffff': '#111111', |
| 13 | + }, |
| 14 | + 'other': { |
| 15 | + '#000000': '#444444', |
| 16 | + '#ffffff': '#333333', |
| 17 | + }, |
| 18 | + '#ffffff': '#000000', |
| 19 | + }, |
| 20 | + })).toEqual( |
| 21 | + { |
| 22 | + '#abcabc': '#defdef', |
| 23 | + '#000000': '#222222', |
| 24 | + '#ffffff': '#000000', |
| 25 | + }, |
| 26 | + ) |
| 27 | +}) |
| 28 | + |
| 29 | +it('flat colorReplacements', async () => { |
5 | 30 | const result = await codeToHtml('console.log("hi")', {
|
6 | 31 | lang: 'js',
|
7 | 32 | themes: {
|
@@ -44,3 +69,60 @@ it('colorReplacements', async () => {
|
44 | 69 | "
|
45 | 70 | `)
|
46 | 71 | })
|
| 72 | + |
| 73 | +it('scoped colorReplacements', async () => { |
| 74 | + const customLightTheme: ThemeRegistrationResolved = { |
| 75 | + name: 'custom-light', |
| 76 | + type: 'light', |
| 77 | + settings: [ |
| 78 | + { scope: 'string', settings: { foreground: '#a3be8c' } }, |
| 79 | + ], |
| 80 | + fg: '#393a34', |
| 81 | + bg: '#b07d48', |
| 82 | + } |
| 83 | + const customDarkTheme: ThemeRegistrationResolved = { |
| 84 | + ...customLightTheme, |
| 85 | + type: 'dark', |
| 86 | + name: 'custom-dark', |
| 87 | + } |
| 88 | + |
| 89 | + const result = await codeToHtml('console.log("hi")', { |
| 90 | + lang: 'js', |
| 91 | + themes: { |
| 92 | + light: customLightTheme, |
| 93 | + dark: customDarkTheme, |
| 94 | + }, |
| 95 | + colorReplacements: { |
| 96 | + 'custom-dark': { |
| 97 | + '#b07d48': 'var(---replaced-1)', |
| 98 | + }, |
| 99 | + 'custom-light': { |
| 100 | + '#393a34': 'var(---replaced-2)', |
| 101 | + '#b07d48': 'var(---replaced-3)', |
| 102 | + }, |
| 103 | + '#393a34': 'var(---replaced-4)', |
| 104 | + }, |
| 105 | + }) |
| 106 | + |
| 107 | + expect(result).toContain('var(---replaced-1)') |
| 108 | + expect(result).not.toContain('var(---replaced-2)') |
| 109 | + expect(result).toContain('var(---replaced-3)') |
| 110 | + expect(result).toContain('var(---replaced-4)') |
| 111 | + |
| 112 | + expect(result.replace(/>/g, '>\n')) |
| 113 | + .toMatchInlineSnapshot(` |
| 114 | + "<pre class="shiki shiki-themes custom-light custom-dark" style="background-color:var(---replaced-3);--shiki-dark-bg:var(---replaced-1);color:var(---replaced-4);--shiki-dark:var(---replaced-4)" tabindex="0"> |
| 115 | + <code> |
| 116 | + <span class="line"> |
| 117 | + <span style="color:var(---replaced-4);--shiki-dark:var(---replaced-4)"> |
| 118 | + console.log(</span> |
| 119 | + <span style="color:#A3BE8C;--shiki-dark:#A3BE8C"> |
| 120 | + "hi"</span> |
| 121 | + <span style="color:var(---replaced-4);--shiki-dark:var(---replaced-4)"> |
| 122 | + )</span> |
| 123 | + </span> |
| 124 | + </code> |
| 125 | + </pre> |
| 126 | + " |
| 127 | + `) |
| 128 | +}) |
0 commit comments