feat: ai's attempt

This commit is contained in:
2026-02-26 13:35:09 +08:00
parent 990888a204
commit 8d7f745df2
7 changed files with 245 additions and 14 deletions
+24 -1
View File
@@ -1,5 +1,6 @@
let dataIndex: Record<string, string> | null = null;
let isDevIndexLoaded = false;
let isCliIndexLoaded = false;
/**
* 设置全局数据索引(CLI 环境使用)
@@ -42,13 +43,35 @@ async function loadDevIndex(): Promise<void> {
}
}
/**
* 在 CLI 环境从 /__CONTENT_INDEX.json 加载索引
*/
async function loadCliIndex(): Promise<void> {
if (isCliIndexLoaded) return;
isCliIndexLoaded = true;
try {
const response = await fetch('/__CONTENT_INDEX.json');
if (!response.ok) {
throw new Error('Failed to fetch content index');
}
const index = await response.json();
dataIndex = { ...dataIndex, ...index };
} catch (e) {
// CLI 索引不可用时忽略(可能不在 CLI 环境)
}
}
/**
* 异步加载数据
* @param path 数据路径
* @returns 数据内容
*/
export async function fetchData(path: string): Promise<string> {
// dev 环境:先加载 glob 索引
// CLI 环境:先从 /__CONTENT_INDEX.json 加载索引
await loadCliIndex();
// dev 环境:加载 glob 索引
await loadDevIndex();
// 首先尝试从索引获取