How to read a folder of markdown and convert them as JSON list
Posted on January 13, 2023
This is a neat Node.js javascript code that reads a folder full of markdown files and return a JSON file with an array representing that folder.
First, we need some packages: glob
and @github-docs/frontmatter
.
Here the script with comments along the lines:
import glob from 'glob'
import frontmatter from '`@github-docs/frontmatter'
const files = glob.sync('**/*.md', { cwd: 'myfolder' })
const list = files.map(d => {
const md = fs.readFileSync(d)
return frontmatter(md)
})
fs.writeFileSync('output/list.json', JSON.stringify(list))