Skip to content

Commit 741da65

Browse files
authored
Merge pull request #25 from code-hike/from-css
Themes with CSS custom properties
2 parents 79a17c9 + 7257e74 commit 741da65

19 files changed

+2197
-66
lines changed

.changeset/purple-years-scream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@code-hike/lighter": patch
3+
---
4+
5+
Support css variables

lib/dist/browser.esm.mjs

+43-11
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function transparent(color, opacity) {
8787
}
8888

8989
function getThemeColors(theme) {
90-
return Object.assign({ colorScheme: theme.type }, getColors(theme));
90+
return Object.assign({ colorScheme: theme.type === "from-css" ? "var(--ch-0)" : theme.type }, getColors(theme));
9191
}
9292
const colorNamesToKeys = {
9393
background: "editor.background",
@@ -231,7 +231,7 @@ async function preloadTheme(theme) {
231231
if (typeof theme === "string") {
232232
const name = theme;
233233
if (!THEME_NAMES.includes(name)) {
234-
throw new UnknownThemeError$1(name);
234+
throw new UnknownThemeError(name);
235235
}
236236
if (!promiseCache.has(name)) {
237237
const promise = reallyLoadThemeByName(name).then((theme) => {
@@ -269,7 +269,7 @@ function toFinalTheme(theme) {
269269
if (!theme) {
270270
return undefined;
271271
}
272-
const finalTheme = Object.assign(Object.assign({}, theme), { name: theme.name || "unknown-theme", type: getColorScheme(theme), settings: theme.settings || theme.tokenColors || [], colors: theme.colors || {} });
272+
const finalTheme = Object.assign(Object.assign({}, theme), { name: theme.name || "unknown-theme", type: getColorScheme(theme), settings: theme.settings || theme.tokenColors || [], colors: theme.colors || {}, colorNames: theme.colorNames });
273273
const globalSetting = finalTheme.settings.find((s) => !s.name && !s.scope);
274274
if (globalSetting) {
275275
const { foreground, background } = globalSetting.settings || {};
@@ -295,10 +295,37 @@ function toFinalTheme(theme) {
295295
...finalTheme.settings,
296296
];
297297
}
298+
if (theme.type === "from-css" && !finalTheme.colorNames) {
299+
const colorNames = {};
300+
let counter = 0;
301+
finalTheme.settings = finalTheme.settings.map((s) => {
302+
const setting = Object.assign(Object.assign({}, s), { settings: Object.assign({}, s.settings) });
303+
const { foreground, background } = setting.settings || {};
304+
if (foreground && !colorNames[foreground]) {
305+
colorNames[foreground] = `#${counter.toString(16).padStart(6, "0")}`;
306+
counter++;
307+
}
308+
if (background && !colorNames[background]) {
309+
colorNames[background] = `#${counter.toString(16).padStart(6, "0")}`;
310+
counter++;
311+
}
312+
if (foreground) {
313+
setting.settings.foreground = colorNames[foreground];
314+
}
315+
if (background) {
316+
setting.settings.background = colorNames[background];
317+
}
318+
return setting;
319+
});
320+
finalTheme.colorNames = colorNames;
321+
}
298322
return finalTheme;
299323
}
300324
function getColorScheme(theme) {
301325
var _a;
326+
if (theme.type === "from-css") {
327+
return "from-css";
328+
}
302329
const themeType = theme.type
303330
? theme.type
304331
: ((_a = theme.name) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes("light"))
@@ -317,10 +344,12 @@ const THEME_NAMES = [
317344
"dracula",
318345
"github-dark",
319346
"github-dark-dimmed",
347+
"github-from-css",
320348
"github-light",
321349
"light-plus",
322350
"material-darker",
323351
"material-default",
352+
"material-from-css",
324353
"material-lighter",
325354
"material-ocean",
326355
"material-palenight",
@@ -335,7 +364,7 @@ const THEME_NAMES = [
335364
"solarized-dark",
336365
"solarized-light",
337366
];
338-
class UnknownThemeError$1 extends Error {
367+
class UnknownThemeError extends Error {
339368
constructor(theme) {
340369
super(`Unknown theme: ${theme}`);
341370
this.theme = theme;
@@ -2582,9 +2611,18 @@ class UnknownLanguageError extends Error {
25822611
}
25832612
function highlightTokens(code, grammar, theme) {
25842613
registry.setTheme(theme);
2585-
const colorMap = registry.getColorMap();
2614+
const colorMap = getColorMap(theme);
25862615
return tokenize(code, grammar, colorMap);
25872616
}
2617+
function getColorMap(theme) {
2618+
const colorMap = registry.getColorMap();
2619+
if (!theme.colorNames)
2620+
return colorMap;
2621+
return colorMap.map((c) => {
2622+
const key = Object.keys(theme.colorNames).find((key) => theme.colorNames[key].toUpperCase() === c.toUpperCase());
2623+
return key || c;
2624+
});
2625+
}
25882626
function highlightTokensWithScopes(code, grammar, theme) {
25892627
registry.setTheme(theme);
25902628
const colorMap = registry.getColorMap();
@@ -3054,12 +3092,6 @@ function splitAnnotations(annotations) {
30543092
};
30553093
}
30563094

3057-
class UnknownThemeError extends Error {
3058-
constructor(theme) {
3059-
super(`Unknown theme: ${theme}`);
3060-
this.theme = theme;
3061-
}
3062-
}
30633095
function isAnnotatedConfig(config) {
30643096
return "annotations" in config;
30653097
}

lib/dist/index.cjs.js

+43-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dist/index.d.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@ type ThemeSetting = {
1818
};
1919
type FinalTheme = {
2020
name: string;
21-
type: "dark" | "light";
21+
type: "dark" | "light" | "from-css";
2222
settings: ThemeSetting[];
2323
colors: {
2424
[key: string]: string;
2525
};
26+
colorNames?: {
27+
[key: string]: string;
28+
};
2629
};
27-
declare const THEME_NAMES: readonly ["dark-plus", "dracula-soft", "dracula", "github-dark", "github-dark-dimmed", "github-light", "light-plus", "material-darker", "material-default", "material-lighter", "material-ocean", "material-palenight", "min-dark", "min-light", "monokai", "nord", "one-dark-pro", "poimandres", "slack-dark", "slack-ochin", "solarized-dark", "solarized-light"];
30+
declare const THEME_NAMES: readonly ["dark-plus", "dracula-soft", "dracula", "github-dark", "github-dark-dimmed", "github-from-css", "github-light", "light-plus", "material-darker", "material-default", "material-from-css", "material-lighter", "material-ocean", "material-palenight", "min-dark", "min-light", "monokai", "nord", "one-dark-pro", "poimandres", "slack-dark", "slack-ochin", "solarized-dark", "solarized-light"];
2831
type NamesTuple$1 = typeof THEME_NAMES;
2932
type StringTheme = NamesTuple$1[number];
3033
type Theme = StringTheme | RawTheme;
34+
declare class UnknownThemeError extends Error {
35+
theme: string;
36+
constructor(theme: string);
37+
}
3138

3239
declare const LANG_NAMES: string[];
3340
type NamesTuple = typeof LANG_NAMES;
@@ -64,7 +71,7 @@ declare function getThemeColors(theme: FinalTheme): {
6471
activeTabTopBorder: string;
6572
hoverTabBackground: string;
6673
hoverTabForeground: string;
67-
colorScheme: "dark" | "light";
74+
colorScheme: string;
6875
};
6976

7077
type LineNumber = number;
@@ -122,10 +129,6 @@ declare class UnknownLanguageError extends Error {
122129
constructor(alias: string);
123130
}
124131

125-
declare class UnknownThemeError extends Error {
126-
theme: string;
127-
constructor(theme: string);
128-
}
129132
type Config = {
130133
scopes?: boolean;
131134
};

0 commit comments

Comments
 (0)