Skip to content

Commit c42f8e5

Browse files
committed
refactor(colors): simplify command color representation and improve formatting
1 parent 9aa5ac4 commit c42f8e5

File tree

2 files changed

+43
-54
lines changed

2 files changed

+43
-54
lines changed

src/lib/command-colors.ts

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,30 @@
1-
export interface CommandColor {
2-
hsl: string;
3-
}
4-
51
/**
62
* Represents a color used by the command highlighter.
7-
* Currently only HSL is used by the transformer for inline styles.
83
*
94
* @example
105
* ```command
116
* <green>content</green>
127
* ```
138
*/
14-
export const commandColors: Record<string, CommandColor> = {
15-
green: {
16-
hsl: "hsl(142, 76%, 36%)",
17-
},
18-
red: {
19-
hsl: "hsl(0, 84%, 60%)",
20-
},
21-
blue: {
22-
hsl: "hsl(221, 83%, 53%)",
23-
},
24-
yellow: {
25-
hsl: "hsl(48, 96%, 53%)",
26-
},
27-
purple: {
28-
hsl: "hsl(262, 83%, 58%)",
29-
},
30-
orange: {
31-
hsl: "hsl(25, 95%, 53%)",
32-
},
33-
cyan: {
34-
hsl: "hsl(187, 100%, 42%)",
35-
},
36-
pink: {
37-
hsl: "hsl(330, 81%, 60%)",
38-
},
39-
gray: {
40-
hsl: "hsl(215, 16%, 47%)",
41-
},
42-
white: {
43-
hsl: "hsl(0, 0%, 100%)",
44-
},
45-
black: {
46-
hsl: "hsl(0, 0%, 0%)",
47-
}
48-
};
9+
export const commandColors: Record<string, string> = {
10+
black: "#000000",
11+
dark_blue: "#0000aa",
12+
dark_green: "#00aa00",
13+
dark_aqua: "#00aaaa",
14+
dark_red: "#aa0000",
15+
dark_purple: "#aa00aa",
16+
gold: "#ffaa00",
17+
gray: "#aaaaaa",
18+
dark_gray: "#555555",
19+
blue: "#5555ff",
20+
green: "#55ff55",
21+
aqua: "#55ffff",
22+
red: "#ff5555",
23+
light_purple: "#ff55ff",
24+
yellow: "#ffff55",
25+
white: "#ffffff",
26+
}
4927

50-
export function getCommandColor(colorName: string): CommandColor | null {
51-
return commandColors[colorName.toLowerCase()] || null;
28+
export function getCommandColor(colorName: string): string | null {
29+
return commandColors[colorName.toLowerCase()] || null
5230
}

src/lib/command-transformer.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ShikiTransformer, ThemedToken } from '@shikijs/types'
2-
import { getCommandColor } from './command-colors'
1+
import type { ShikiTransformer, ThemedToken } from "@shikijs/types"
2+
import { getCommandColor } from "./command-colors"
33

44
interface ColorContentRange {
55
start: number
@@ -39,8 +39,11 @@ function detectRanges(code: string): MetaRanges {
3939

4040
function splitTokenAtOffsets(token: ThemedToken, breakpoints: number[]): ThemedToken[] {
4141
if (!breakpoints.length) return [token]
42-
const local = Array.from(new Set(breakpoints.filter(bp => bp > token.offset && bp < token.offset + token.content.length)))
43-
.sort((a, b) => a - b)
42+
const local = Array.from(
43+
new Set(
44+
breakpoints.filter((bp) => bp > token.offset && bp < token.offset + token.content.length),
45+
),
46+
).sort((a, b) => a - b)
4447

4548
if (!local.length) return [token]
4649

@@ -49,7 +52,11 @@ function splitTokenAtOffsets(token: ThemedToken, breakpoints: number[]): ThemedT
4952
for (const bp of local) {
5053
const idx = bp - token.offset
5154
if (idx > last) {
52-
result.push({ ...token, content: token.content.slice(last, idx), offset: token.offset + last })
55+
result.push({
56+
...token,
57+
content: token.content.slice(last, idx),
58+
offset: token.offset + last,
59+
})
5360
}
5461
last = idx
5562
}
@@ -63,7 +70,7 @@ export function transformerCommandColor(): ShikiTransformer {
6370
const map = new WeakMap<object, MetaRanges>()
6471

6572
return {
66-
name: 'color-tags',
73+
name: "color-tags",
6774
preprocess(code) {
6875
const ranges = detectRanges(code)
6976
const metaKey = (this as unknown as { meta?: object }).meta ?? {}
@@ -91,24 +98,28 @@ export function transformerCommandColor(): ShikiTransformer {
9198

9299
if (!bps.length) return line
93100

94-
const splitted = line.flatMap(t => splitTokenAtOffsets(t, bps))
101+
const splitted = line.flatMap((t) => splitTokenAtOffsets(t, bps))
95102

96-
const styled: ThemedToken[] = splitted.map(seg => {
97-
const inHide = ranges.hides.some(r => r.start <= seg.offset && seg.offset + seg.content.length <= r.end)
103+
const styled: ThemedToken[] = splitted.map((seg) => {
104+
const inHide = ranges.hides.some(
105+
(r) => r.start <= seg.offset && seg.offset + seg.content.length <= r.end,
106+
)
98107
if (inHide) {
99108
return {
100109
...seg,
101-
htmlStyle: { ...(seg.htmlStyle || {}), display: 'none' },
110+
htmlStyle: { ...(seg.htmlStyle || {}), display: "none" },
102111
}
103112
}
104113

105-
const contentRange = ranges.contents.find(r => r.start <= seg.offset && seg.offset + seg.content.length <= r.end)
114+
const contentRange = ranges.contents.find(
115+
(r) => r.start <= seg.offset && seg.offset + seg.content.length <= r.end,
116+
)
106117
if (contentRange) {
107118
const cfg = getCommandColor(contentRange.color)
108119
if (cfg) {
109120
return {
110121
...seg,
111-
htmlStyle: { ...(seg.htmlStyle || {}), color: cfg.hsl },
122+
htmlStyle: { ...(seg.htmlStyle || {}), color: cfg },
112123
}
113124
}
114125
}

0 commit comments

Comments
 (0)