Skip to content

Commit f36ea6d

Browse files
Chanakyasindeantfu
andauthored
feat(lang-ansi): Fallback for missing theme ANSI colors (#1095)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 218c901 commit f36ea6d

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

packages/core/src/highlight/code-to-tokens-ansi.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ import { createAnsiSequenceParser, createColorPalette, namedColors } from 'ansi-
99

1010
import { applyColorReplacements, resolveColorReplacements, splitLines } from '../utils'
1111

12+
/**
13+
* Default ANSI palette (VSCode compatible fallbacks)
14+
* Used when the theme does not define terminal.ansi* colors.
15+
*/
16+
const defaultAnsiColors: Record<string, string> = {
17+
black: '#000000',
18+
red: '#cd3131',
19+
green: '#0DBC79',
20+
yellow: '#E5E510',
21+
blue: '#2472C8',
22+
magenta: '#BC3FBC',
23+
cyan: '#11A8CD',
24+
white: '#E5E5E5',
25+
26+
brightBlack: '#666666',
27+
brightRed: '#F14C4C',
28+
brightGreen: '#23D18B',
29+
brightYellow: '#F5F543',
30+
brightBlue: '#3B8EEA',
31+
brightMagenta: '#D670D6',
32+
brightCyan: '#29B8DB',
33+
brightWhite: '#FFFFFF',
34+
}
35+
1236
export function tokenizeAnsiWithTheme(
1337
theme: ThemeRegistrationResolved,
1438
fileContents: string,
@@ -17,21 +41,22 @@ export function tokenizeAnsiWithTheme(
1741
const colorReplacements = resolveColorReplacements(theme, options)
1842
const lines = splitLines(fileContents)
1943

20-
const colorPalette = createColorPalette(
21-
Object.fromEntries(
22-
namedColors.map(name => [
23-
name,
24-
theme.colors?.[`terminal.ansi${name[0].toUpperCase()}${name.substring(1)}`],
25-
]),
26-
) as any,
27-
)
44+
const ansiPalette = Object.fromEntries(
45+
namedColors.map((name) => {
46+
const key = `terminal.ansi${name[0].toUpperCase()}${name.substring(1)}`
47+
const themeColor = theme.colors?.[key]
48+
return [name, themeColor || defaultAnsiColors[name]]
49+
}),
50+
) as Record<string, string>
2851

52+
const colorPalette = createColorPalette(ansiPalette)
2953
const parser = createAnsiSequenceParser()
3054

3155
return lines.map(line =>
3256
parser.parse(line[0]).map((token): ThemedToken => {
3357
let color: string
3458
let bgColor: string | undefined
59+
3560
if (token.decorations.has('reverse')) {
3661
color = token.background ? colorPalette.value(token.background) : theme.bg
3762
bgColor = token.foreground ? colorPalette.value(token.foreground) : theme.fg
@@ -75,7 +100,7 @@ export function tokenizeAnsiWithTheme(
75100
* Adds 50% alpha to a hex color string or the "-dim" postfix to a CSS variable
76101
*/
77102
function dimColor(color: string): string {
78-
const hexMatch = color.match(/#([0-9a-f]{3})([0-9a-f]{3})?([0-9a-f]{2})?/)
103+
const hexMatch = color.match(/#([0-9a-f]{3})([0-9a-f]{3})?([0-9a-f]{2})?/i)
79104
if (hexMatch) {
80105
if (hexMatch[3]) {
81106
// convert from #rrggbbaa to #rrggbb(aa/2)

0 commit comments

Comments
 (0)