mirror of
https://github.com/mara-li/obsidian-my-thesaurus.git
synced 2026-07-22 05:38:22 +00:00
fix(csv): enhance CSV parsing with separator regex and column count validation
This commit is contained in:
parent
86fcbc23d0
commit
5abff9cf66
1 changed files with 9 additions and 2 deletions
|
|
@ -1,11 +1,17 @@
|
|||
import type { ColumnName, Separator, Thesaurus, Translation } from "../interfaces";
|
||||
|
||||
const separatorRegex = /[,;\t\|]/;
|
||||
|
||||
function verifySeparator(header: string, sep: Separator): boolean {
|
||||
return header.includes(sep);
|
||||
}
|
||||
|
||||
function searchSeparator(header: string): Separator {
|
||||
return header.match(/[,;\t\|]/)?.[0] as Separator;
|
||||
return header.match(separatorRegex)?.[0] as Separator;
|
||||
}
|
||||
|
||||
function countColumn(header: string) {
|
||||
return header.split(separatorRegex).length;
|
||||
}
|
||||
|
||||
export function getColumn(
|
||||
|
|
@ -43,10 +49,11 @@ export function getThesaurus(
|
|||
const isMd = separator === "md";
|
||||
separator = isMd ? "|" : separator;
|
||||
const lines = fileContent.split("\n");
|
||||
if (countColumn(lines[0]) <= 1) throw new Error(ln("error.csv.malformed"));
|
||||
if (!verifySeparator(lines[0], separator))
|
||||
throw new Error(ln("error.csv.separator", { sep: searchSeparator(lines[0]) }));
|
||||
|
||||
const header = getHeader(lines, separator);
|
||||
if (header.length <= 1) throw new Error(ln("error.csv.malformed"));
|
||||
const { indexKey, indexSynonyms } = getColumn(header, columnNames, ln);
|
||||
const thesaurus: Thesaurus = {};
|
||||
const lineSlice = isMd ? 2 : 1;
|
||||
|
|
|
|||
Loading…
Reference in a new issue