131 lines
3.6 KiB
TypeScript
131 lines
3.6 KiB
TypeScript
/**
|
||
* 主题数据加载器 - 根据 CURRENT_THEME 加载对应主题的内容
|
||
* 新增主题:1. 在 config/site.config.ts 添加 THEME_CONFIGS
|
||
* 2. 在 content/themes/[theme-id]/ 下添加 messages 和 data
|
||
* 3. 在本文件 switch 中添加 case
|
||
*/
|
||
|
||
import { CURRENT_THEME, THEME_IDS, type ThemeId } from "@/config";
|
||
|
||
export type Locale = "zh" | "en";
|
||
|
||
export type ToolsCategory = {
|
||
id: string;
|
||
icon: string;
|
||
title: string;
|
||
color: string;
|
||
bg: string;
|
||
text: string;
|
||
tools: Array<{ name: string; desc: string; href: string }>;
|
||
};
|
||
|
||
/** 加载当前主题的 messages */
|
||
export async function getThemeMessages(
|
||
locale: Locale
|
||
): Promise<Record<string, unknown>> {
|
||
const theme = CURRENT_THEME;
|
||
switch (theme) {
|
||
case "digital-nomad": {
|
||
const mod =
|
||
locale === "zh"
|
||
? await import("@/content/themes/digital-nomad/messages/zh.json")
|
||
: await import("@/content/themes/digital-nomad/messages/en.json");
|
||
return mod.default;
|
||
}
|
||
case "solo-company":
|
||
case "tiktok-ops":
|
||
case "indie-dev":
|
||
// 新主题暂无内容时回退到 digital-nomad
|
||
return getThemeMessagesFallback(locale);
|
||
default:
|
||
return getThemeMessagesFallback(locale);
|
||
}
|
||
}
|
||
|
||
async function getThemeMessagesFallback(
|
||
locale: Locale
|
||
): Promise<Record<string, unknown>> {
|
||
const mod =
|
||
locale === "zh"
|
||
? await import("@/content/themes/digital-nomad/messages/zh.json")
|
||
: await import("@/content/themes/digital-nomad/messages/en.json");
|
||
return mod.default;
|
||
}
|
||
|
||
/** 加载当前主题的 tools 数据 */
|
||
export async function getThemeToolsData(): Promise<{
|
||
categories: ToolsCategory[];
|
||
}> {
|
||
const theme = CURRENT_THEME;
|
||
switch (theme) {
|
||
case "digital-nomad": {
|
||
const mod = await import(
|
||
"@/content/themes/digital-nomad/data/tools.json"
|
||
);
|
||
return mod.default;
|
||
}
|
||
case "solo-company":
|
||
case "tiktok-ops":
|
||
case "indie-dev":
|
||
return getThemeToolsDataFallback();
|
||
default:
|
||
return getThemeToolsDataFallback();
|
||
}
|
||
}
|
||
|
||
async function getThemeToolsDataFallback(): Promise<{
|
||
categories: ToolsCategory[];
|
||
}> {
|
||
const mod = await import(
|
||
"@/content/themes/digital-nomad/data/tools.json"
|
||
);
|
||
return mod.default;
|
||
}
|
||
|
||
export type ResourceData = {
|
||
books: Array<{ cover: string; title: string; href: string }>;
|
||
products: Array<{ image: string; name: string; href: string }>;
|
||
pan: {
|
||
categories: Array<{
|
||
id: string;
|
||
title: string;
|
||
icon: string;
|
||
items: Array<{ name: string; desc: string; href: string }>;
|
||
}>;
|
||
};
|
||
};
|
||
|
||
/** 加载资源导航数据:图书、商业数码、网盘 */
|
||
export async function getResourceData(): Promise<ResourceData> {
|
||
const theme = CURRENT_THEME;
|
||
if (theme === "digital-nomad") {
|
||
const [booksMod, productsMod, panMod] = await Promise.all([
|
||
import("@/content/themes/digital-nomad/data/books.json"),
|
||
import("@/content/themes/digital-nomad/data/products.json"),
|
||
import("@/content/themes/digital-nomad/data/pan.json"),
|
||
]);
|
||
return {
|
||
books: booksMod.default.books,
|
||
products: productsMod.default.products,
|
||
pan: panMod.default,
|
||
};
|
||
}
|
||
return getResourceDataFallback();
|
||
}
|
||
|
||
async function getResourceDataFallback(): Promise<ResourceData> {
|
||
const [booksMod, productsMod, panMod] = await Promise.all([
|
||
import("@/content/themes/digital-nomad/data/books.json"),
|
||
import("@/content/themes/digital-nomad/data/products.json"),
|
||
import("@/content/themes/digital-nomad/data/pan.json"),
|
||
]);
|
||
return {
|
||
books: booksMod.default.books,
|
||
products: productsMod.default.products,
|
||
pan: panMod.default,
|
||
};
|
||
}
|
||
|
||
export { CURRENT_THEME, THEME_IDS };
|
||
export type { ThemeId };
|