Skip to content

Instantly share code, notes, and snippets.

@johnthethird
Created March 9, 2018 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnthethird/83e13392caa566779ff8c150349f2288 to your computer and use it in GitHub Desktop.
Save johnthethird/83e13392caa566779ff8c150349f2288 to your computer and use it in GitHub Desktop.
Convert directory of Markdown files with front matter to JSON
const fs = require('fs')
const path = require('path')
const glob = require('glob-fs')()
const moment = require('moment')
const matter = require('gray-matter')
const processFile = filename => {
const fileObj = matter(fs.readFileSync(filename, 'utf8'))
const dataObj = {
content: fileObj.content,
filename: path.basename(filename),
slug: fileObj.data.title
.toLowerCase()
.replace(/ /g, '-')
.replace(/[^\w-]+/g, ''),
iso8601Date: moment(fileObj.data.date).format(),
...fileObj.data
}
return dataObj
}
module.exports.parse = dir => glob.readdirSync(dir).map(processFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment