Skip to content

Commit f372f95

Browse files
committed
feat: implement theme name normalization and resolution for improved theme handling
1 parent ca4993d commit f372f95

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

src/utils/themes.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,36 @@ const defaultFontUrl = '/fonts/orbitron.woff2';
88

99
export const themes: { [key: string]: Theme } = { ...baseThemes, ...graphThemes };
1010

11+
/**
12+
* Normalises a theme name for fuzzy lookup:
13+
* lower-case + collapse spaces / underscores / hyphens to a single hyphen.
14+
* e.g. "Tokyo Night", "tokyoNight", "tokyo_night" all → "tokyo-night"
15+
*/
16+
function normalizeKey(name: string): string {
17+
return name.toLowerCase().replace(/[\s_]+/g, '-');
18+
}
19+
20+
/** Pre-built map: normalised key → original themes key */
21+
const themeIndex: Map<string, string> = new Map(
22+
Object.keys(themes).map(k => [normalizeKey(k), k])
23+
);
24+
25+
/** Resolve a user-supplied theme name to the actual themes key, or 'default'. */
26+
function resolveThemeName(name: string): string {
27+
// Exact match first (fast path)
28+
if (themes[name]) return name;
29+
// Normalised match (case / separator insensitive)
30+
const resolved = themeIndex.get(normalizeKey(name));
31+
return resolved ?? 'default';
32+
}
33+
1134
export function getTheme(themeName: string = 'default', customColors?: {
1235
bgColor?: string;
1336
borderColor?: string;
1437
textColor?: string;
1538
titleColor?: string;
1639
}): Theme {
17-
const theme = themes[themeName] || themes.default;
40+
const theme = themes[resolveThemeName(themeName)];
1841

1942
return {
2043
...themes.default,

0 commit comments

Comments
 (0)