fix: csv loading

This commit is contained in:
hypercross 2026-03-27 15:26:20 +08:00
parent ea57cf8d2b
commit 831955e16e
1 changed files with 3 additions and 23 deletions

View File

@ -82,7 +82,8 @@ export function parseCSVString<T = Record<string, string>>(csvString: string, so
columns: true,
comment: '#',
trim: true,
skipEmptyLines: true
skipEmptyLines: true,
bom: true
});
const result = records as Record<string, string>[];
@ -112,28 +113,7 @@ export async function loadCSV<T = Record<string, string>>(pathOrContent: string)
// 从索引获取文件内容
const content = await getIndexedData(pathOrContent);
// 解析 front matter
const { frontmatter, remainingContent } = parseFrontMatter(content);
const records = parse(remainingContent, {
columns: true,
comment: '#',
trim: true,
skipEmptyLines: true
});
const result = records as Record<string, string>[];
// 添加 front matter 到结果中
const csvResult = result as CSV<T>;
if (frontmatter) {
csvResult.frontmatter = frontmatter;
for(const each of result){
Object.assign(each, frontmatter);
}
}
csvResult.sourcePath = pathOrContent;
return csvResult;
return parseCSVString<T>(content, pathOrContent);
}
type JSONData = JSONArray | JSONObject | string | number | boolean | null;