diff --git a/src/core/theme.ts b/src/core/theme.ts index 891ce35..4e8641c 100644 --- a/src/core/theme.ts +++ b/src/core/theme.ts @@ -1,4 +1,6 @@ import { createGlobalTheme, globalStyle } from "@vanilla-extract/css"; +import fs from "node:fs"; +import path from "node:path"; import { otherThemeVars, themeInfoVars, themeVars } from "src/types/vars"; import type { MapLeafNodes, WithOptionalLayer } from "./types"; @@ -32,9 +34,19 @@ const emoji = ` .emoji[aria-label="musical notes"] `; +// 版本号: 版本号.YYMMDD +const now = new Date(); +const year = now.getFullYear().toString().slice(-2); +const month = (now.getMonth() + 1).toString().padStart(2, "0"); +const day = now.getDate().toString().padStart(2, "0"); + +const pkgPath = path.join(__dirname, "../..", "package.json"); +const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8")); +const version = `"${pkg.version}.${year}${month}${day}"`; + export function createTheme(theme: Theme): void { const isDarkTheme: boolean = JSON.parse(theme.isDarkTheme); - createGlobalTheme(":root", themeInfoVars, { version: "1.24.6.250909" }); + createGlobalTheme(":root", themeInfoVars, { version }); createGlobalTheme(":root", themeVars, theme); createGlobalTheme(":root", otherThemeVars, { border: { radius: "6px" }, diff --git a/src/core/vite.ts b/src/core/vite.ts index b9873db..55337ca 100644 --- a/src/core/vite.ts +++ b/src/core/vite.ts @@ -50,9 +50,9 @@ export function themeInput(outDir: string, themeDir: string, mode: string): { [k return input; } -function getAutoThemeInfo(nameGroup: string[]): string { +function giteaThemeMetaInfo(nameGroup: string[]): string { const displayName = nameGroup.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" "); - return `gitea-theme-meta-info { --theme-display-name: GitHub ${displayName};}`; // 不要省略分号, 也不要追加任何变量, 否则 Gitea 不识别 + return `gitea-theme-meta-info{--theme-display-name:"GitHub ${displayName}";}`; // 不要省略分号, 也不要追加任何变量, 否则 Gitea 不识别 } const prefix = "theme-github-"; @@ -88,23 +88,22 @@ export function themePlugin(): Plugin { const originalFileName = value.originalFileNames.pop(); const type = value.type; // 合并样式文件和主题信息 - const meta = getAutoThemeInfo(key.split(".")[0].split("-")); - const source = `${meta}\n${value.source.toString()}${styles}`; + const meta = giteaThemeMetaInfo(key.split(".")[0].split("-")); + const source = `${meta}${value.source.toString()}${styles}`; // 添加主题到输出 this.emitFile({ name, fileName, source, type, originalFileName }); // 自动颜色主题 - const nameGroup = key.split(".")[0].split("-"); - const isDark = nameGroup.at(-1) === "dark"; + const isDark = key.endsWith("dark.css"); const darkName = key.replace("light", "dark"); const lightName = darkName.replace("dark", "light"); - const autoName = `${prefix}${darkName.replace("dark", "auto")}`; const findTheme = isDark ? lightName : darkName; if (findTheme in bundle) { + const autoName = `${prefix}${darkName.replace("dark", "auto")}`; const lightContent = `@import "./${prefix}${lightName}" (prefers-color-scheme: light);`; const darkContent = `@import "./${prefix}${darkName}" (prefers-color-scheme: dark);`; - const autoNameGroup = nameGroup.slice(0, -1); - autoNameGroup.push("auto"); - const metaInfo = getAutoThemeInfo(autoNameGroup); + const nameGroup = key.split(".")[0].split("-").slice(0, -1); + nameGroup.push("auto"); + const metaInfo = giteaThemeMetaInfo(nameGroup); this.emitFile({ name: autoName, fileName: autoName,