Created
March 9, 2018 23:53
-
-
Save johnthethird/83e13392caa566779ff8c150349f2288 to your computer and use it in GitHub Desktop.
Convert directory of Markdown files with front matter to JSON
This file contains 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
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