add convenience compose script

This commit is contained in:
Timothy 2021-01-17 11:16:25 +08:00
parent 0403eb24a8
commit 977a50adc1

29
scripts/compose.js Normal file
View File

@ -0,0 +1,29 @@
const fs = require('fs')
const args = process.argv.slice(2)
const title = args[0]
const ext = typeof args[1] !== 'undefined' ? args[1] : 'mdx'
// Remove special characters and replace space with -
const fileName = title
.toLowerCase()
.replace(/[^a-zA-Z0-9 ]/g, '')
.replace(' ', '-')
let d = new Date()
const date = [
d.getFullYear(),
('0' + (d.getMonth() + 1)).slice(-2),
('0' + d.getDate()).slice(-2),
].join('-')
const frontMatter = `---
title: ${title}
date: '${date}'
tags: []
draft: true
summary:
---
`
fs.writeFile(`data/blog/${date}-${fileName}.${ext}`, frontMatter, (err) => {
if (err) throw err
})