feat: csv front matter

This commit is contained in:
2026-02-28 12:16:58 +08:00
parent 1dca41b36a
commit a2ac902129
2 changed files with 76 additions and 20 deletions
+73 -4
View File
@@ -1,22 +1,61 @@
import { parse } from 'csv-parse/browser/esm/sync';
import yaml from 'js-yaml';
/**
* 全局缓存已加载的 CSV 内容
*/
const csvCache = new Map<string, Record<string, string>[]>();
/**
* 解析 front matter
* @param content 包含 front matter 的内容
* @returns 解析结果,包含 front matter 和剩余内容
*/
function parseFrontMatter(content: string): { frontmatter?: JSONObject; remainingContent: string } {
// 检查是否以 --- 开头
if (!content.trim().startsWith('---')) {
return { remainingContent: content };
}
// 分割内容
const parts = content.split(/(?:^|\n)---\s*\n/);
// 至少需要三个部分:空字符串、front matter、剩余内容
if (parts.length < 3) {
return { remainingContent: content };
}
try {
// 解析 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);
return { remainingContent: content };
}
}
/**
* 加载 CSV 文件
* @template T 返回数据的类型,默认为 Record<string, string>
*/
export async function loadCSV<T = Record<string, string>>(path: string): Promise<T[]> {
export async function loadCSV<T = Record<string, string>>(path: string): Promise<CSV<T>> {
if (csvCache.has(path)) {
return csvCache.get(path)! as T[];
return csvCache.get(path)! as CSV<T>;
}
const response = await fetch(path);
const content = await response.text();
const records = parse(content, {
// 解析 front matter
const { frontmatter, remainingContent } = parseFrontMatter(content);
const records = parse(remainingContent, {
columns: true,
comment: '#',
trim: true,
@@ -24,6 +63,36 @@ export async function loadCSV<T = Record<string, string>>(path: string): Promise
});
const result = records as Record<string, string>[];
// 添加 front matter 到结果中
const csvResult = result as CSV<T>;
if (frontmatter) {
csvResult.frontmatter = frontmatter;
}
csvCache.set(path, result);
return result as T[];
return csvResult;
}
type JSONData = JSONArray | JSONObject | string | number | boolean | null;
interface JSONArray extends Array<JSONData> {}
interface JSONObject extends Record<string, JSONData> {}
export type CSV<T> = T[] & {
frontmatter?: JSONObject;
}
export function processVariables<T extends JSONObject> (body: string, currentRow: T, csv: CSV<T>, filtered?: T[], remix?: boolean): string {
const rolled = filtered || csv;
function replaceProp(key: string) {
const row = remix ?
rolled[Math.floor(Math.random() * rolled.length)] :
currentRow;
const frontMatter = csv.frontmatter;
if(key in row) return row[key];
if(frontMatter && key in frontMatter) return frontMatter[key];
return '';
}
return body.replace(/\{\{(\w+)\}\}/g, (_, key) => `${replaceProp(key)}`);
}