|
| 1 | +import type { ThemeRegistration } from 'shiki' |
1 | 2 | import { createHighlighterCore, type HighlighterCore } from 'shiki/core' |
2 | 3 | import { createJavaScriptRegexEngine } from 'shiki/engine/javascript' |
3 | 4 |
|
4 | 5 | let highlighter: HighlighterCore | null = null |
5 | 6 |
|
| 7 | +function replaceThemeColors( |
| 8 | + theme: ThemeRegistration, |
| 9 | + replacements: Record<string, string>, |
| 10 | +): ThemeRegistration { |
| 11 | + let themeString = JSON.stringify(theme) |
| 12 | + for (const [oldColor, newColor] of Object.entries(replacements)) { |
| 13 | + themeString = themeString.replaceAll(oldColor, newColor) |
| 14 | + themeString = themeString.replaceAll(oldColor.toLowerCase(), newColor) |
| 15 | + themeString = themeString.replaceAll(oldColor.toUpperCase(), newColor) |
| 16 | + } |
| 17 | + return JSON.parse(themeString) |
| 18 | +} |
| 19 | + |
6 | 20 | export async function getShikiHighlighter(): Promise<HighlighterCore> { |
7 | 21 | if (!highlighter) { |
8 | 22 | highlighter = await createHighlighterCore({ |
9 | | - themes: [import('@shikijs/themes/github-dark'), import('@shikijs/themes/github-light')], |
| 23 | + themes: [ |
| 24 | + import('@shikijs/themes/github-dark'), |
| 25 | + import('@shikijs/themes/github-light').then(t => |
| 26 | + replaceThemeColors(t.default ?? t, { |
| 27 | + '#22863A': '#227436', // green |
| 28 | + '#B31D28': '#AC222F', // red |
| 29 | + }), |
| 30 | + ), |
| 31 | + ], |
10 | 32 | langs: [ |
11 | 33 | // Core web languages |
12 | 34 | import('@shikijs/langs/javascript'), |
|
0 commit comments