refactor: cached and reloaded file-index
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
import { parse } from 'csv-parse/browser/esm/sync';
|
||||
import yaml from 'js-yaml';
|
||||
|
||||
/**
|
||||
* 全局缓存已加载的 CSV 内容
|
||||
*/
|
||||
const csvCache = new Map<string, Record<string, string>[]>();
|
||||
import { getIndexedData } from '../../data-loader/file-index';
|
||||
|
||||
/**
|
||||
* 解析 front matter
|
||||
@@ -19,7 +15,7 @@ function parseFrontMatter(content: string): { frontmatter?: JSONObject; remainin
|
||||
|
||||
// 分割内容
|
||||
const parts = content.split(/(?:^|\n)---\s*\n/);
|
||||
|
||||
|
||||
// 至少需要三个部分:空字符串、front matter、剩余内容
|
||||
if (parts.length < 3) {
|
||||
return { remainingContent: content };
|
||||
@@ -29,10 +25,10 @@ function parseFrontMatter(content: string): { frontmatter?: JSONObject; remainin
|
||||
// 解析 YAML front matter
|
||||
const frontmatterStr = parts[1].trim();
|
||||
const frontmatter = yaml.load(frontmatterStr) as JSONObject;
|
||||
|
||||
|
||||
// 剩余内容是第三部分及之后的所有内容
|
||||
const remainingContent = parts.slice(2).join('---\n').trimStart();
|
||||
|
||||
|
||||
return { frontmatter, remainingContent };
|
||||
} catch (error) {
|
||||
console.warn('Failed to parse front matter:', error);
|
||||
@@ -45,16 +41,12 @@ function parseFrontMatter(content: string): { frontmatter?: JSONObject; remainin
|
||||
* @template T 返回数据的类型,默认为 Record<string, string>
|
||||
*/
|
||||
export async function loadCSV<T = Record<string, string>>(path: string): Promise<CSV<T>> {
|
||||
if (csvCache.has(path)) {
|
||||
return csvCache.get(path)! as CSV<T>;
|
||||
}
|
||||
// 尝试从索引获取
|
||||
const content = await getIndexedData(path);
|
||||
|
||||
const response = await fetch(path);
|
||||
const content = await response.text();
|
||||
|
||||
// 解析 front matter
|
||||
const { frontmatter, remainingContent } = parseFrontMatter(content);
|
||||
|
||||
|
||||
const records = parse(remainingContent, {
|
||||
columns: true,
|
||||
comment: '#',
|
||||
@@ -72,8 +64,6 @@ export async function loadCSV<T = Record<string, string>>(path: string): Promise
|
||||
}
|
||||
}
|
||||
csvResult.sourcePath = path;
|
||||
|
||||
csvCache.set(path, result);
|
||||
return csvResult;
|
||||
}
|
||||
|
||||
@@ -100,4 +90,4 @@ export function processVariables<T extends JSONObject> (body: string, currentRow
|
||||
}
|
||||
|
||||
return body?.replace(/\{\{(\w+)\}\}/g, (_, key) => `${replaceProp(key)}`) || '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user