Skip to content

Instantly share code, notes, and snippets.

@hrb-suzuki
Last active April 9, 2019 13:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hrb-suzuki/4a61e99f1a65f545ac6a3568dc15688d to your computer and use it in GitHub Desktop.
const fs = require('fs')
const marked = require('marked')
const glob = require('glob')
const fm = require('front-matter')
const path = require('path')
const renderer = new marked.Renderer()
const fileNames = glob.sync(path.resolve('./contents/posts/*.md'))
const data = fileNames
.map(fileName => {
// Fileの中身を取得
const contents = fs.readFileSync(fileName, 'utf8')
// Front MatterでHeader部とBody部に分割
const { attributes, body } = fm(contents)
// BodyをMarkdown形式からHTMLに変換
const parsedBody = marked(body, { renderer })
return {
...attributes,
body: parsedBody
}
})
fs.writeFileSync('./dist/posts.json', JSON.stringify(data, null, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment