Created
October 10, 2023 20:04
-
-
Save eyssette/e41232a490ea8cbdc8709088d2f1cb18 to your computer and use it in GitHub Desktop.
Une fonction pour convertir du Markdown en CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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