Skip to content

Instantly share code, notes, and snippets.

@eyssette
Created October 10, 2023 20:04
Show Gist options
  • Save eyssette/e41232a490ea8cbdc8709088d2f1cb18 to your computer and use it in GitHub Desktop.
Save eyssette/e41232a490ea8cbdc8709088d2f1cb18 to your computer and use it in GitHub Desktop.
Une fonction pour convertir du Markdown en CSV
function markdownToCSV(markdown) {
const lines = markdown.split("\n");
const csvRows = [];
const currentTitles = [];
const regex = /^(\d+)\.\s/;
for (const line of lines) {
line = line.replace(/^[\s\t]+/, "");
const match = line.match(/^(#+\s)(.+)/);
if (match && !line.startsWith("# ")) {
const title = match[2];
const level = match[1].length - 3;
currentTitles[level] = title;
for (let i = level + 1; i < currentTitles.length; i++) {
currentTitles.pop();
}
} else if (
line.startsWith("- ") ||
line.startsWith("* ") ||
regex.test(line)
) {
const data = line.replace("- ", "").replace("* ", "").replace(regex, "");
const rowData = currentTitles.concat(data);
csvRows.push(rowData.join(","));
}
}
return csvRows.join("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment