2024-10-27 23:12:55 +02:00
|
|
|
import fs from "fs";
|
2024-10-27 23:46:03 +02:00
|
|
|
import themeNames from "./code_block_theme_names.json" assert { type: "json" }
|
2024-10-31 18:03:52 +02:00
|
|
|
import { t } from "i18next";
|
2024-10-27 23:12:55 +02:00
|
|
|
|
2024-10-31 20:54:33 +02:00
|
|
|
interface ColorTheme {
|
|
|
|
val: string;
|
|
|
|
title: string;
|
|
|
|
}
|
|
|
|
|
2024-10-27 23:12:55 +02:00
|
|
|
export function listSyntaxHighlightingThemes() {
|
|
|
|
const path = "node_modules/@highlightjs/cdn-assets/styles";
|
2024-10-31 20:54:33 +02:00
|
|
|
const systemThemes = readThemesFromFileSystem(path);
|
|
|
|
|
2024-10-31 21:17:40 +02:00
|
|
|
return {
|
|
|
|
"": [
|
|
|
|
{
|
|
|
|
val: "none",
|
|
|
|
title: t("code_block.theme_none")
|
|
|
|
}
|
|
|
|
],
|
|
|
|
...groupThemesByLightOrDark(systemThemes)
|
|
|
|
}
|
2024-10-31 20:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function readThemesFromFileSystem(path: string): ColorTheme[] {
|
|
|
|
return fs.readdirSync(path)
|
2024-10-27 23:12:55 +02:00
|
|
|
.filter((el) => el.endsWith(".min.css"))
|
|
|
|
.map((name) => {
|
|
|
|
const nameWithoutExtension = name.replace(".min.css", "");
|
2024-10-27 23:46:03 +02:00
|
|
|
let title = nameWithoutExtension.replace(/-/g, " ");
|
|
|
|
|
|
|
|
if (title in themeNames) {
|
|
|
|
title = (themeNames as Record<string, string>)[title];
|
|
|
|
}
|
2024-10-27 23:12:55 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
val: `default:${nameWithoutExtension}`,
|
2024-10-27 23:46:03 +02:00
|
|
|
title: title
|
2024-10-27 23:12:55 +02:00
|
|
|
};
|
|
|
|
});
|
2024-10-31 20:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function groupThemesByLightOrDark(listOfThemes: ColorTheme[]) {
|
2024-10-31 21:00:48 +02:00
|
|
|
const darkThemes = [];
|
|
|
|
const lightThemes = [];
|
2024-10-31 20:54:33 +02:00
|
|
|
|
|
|
|
for (const theme of listOfThemes) {
|
|
|
|
if (theme.title.includes("Dark")) {
|
2024-10-31 21:00:48 +02:00
|
|
|
darkThemes.push(theme);
|
2024-10-31 20:54:33 +02:00
|
|
|
} else {
|
2024-10-31 21:00:48 +02:00
|
|
|
lightThemes.push(theme);
|
2024-10-31 20:54:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-31 21:00:48 +02:00
|
|
|
const output: Record<string, ColorTheme[]> = {};
|
|
|
|
output[t("code_block.theme_group_light")] = lightThemes;
|
|
|
|
output[t("code_block.theme_group_dark")] = darkThemes;
|
|
|
|
return output;
|
2024-10-27 23:12:55 +02:00
|
|
|
}
|