Last active
April 9, 2019 13:25
-
-
Save hrb-suzuki/4a61e99f1a65f545ac6a3568dc15688d to your computer and use it in GitHub Desktop.
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 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