Skip to content

Instantly share code, notes, and snippets.

@talves
Created April 18, 2018 18:08
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 talves/3bc72cb35d4e633b1ef94a66a0f26493 to your computer and use it in GitHub Desktop.
Save talves/3bc72cb35d4e633b1ef94a66a0f26493 to your computer and use it in GitHub Desktop.
Markdown pre-build script
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const matter = require('gray-matter')
const rootPath = path.resolve(__dirname, '../')
const dataPath = path.resolve(rootPath, './src/data/blog')
/* Documentation Data */
const dirPath = path.resolve(rootPath, './posts')
const documentation = fs.readdirSync(dirPath)
.filter(file => fs.statSync(path.resolve(dirPath, file)).isFile())
.map(file => {
const { data, content } = matter(fs.readFileSync(path.resolve(dirPath, file), 'utf8'))
console.log(file)
data.slug = (data.slug) ? data.slug : data.slug = file.split('.').slice(0, file.split('.').length - 1).join('.')
return { data, content }
})
const postsFile = path.join(dataPath, './posts.json')
fs.writeFileSync(postsFile, JSON.stringify(documentation), { encoding: 'utf8', flag: 'w' })
/* * * * * * * * * * */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment