-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-post.js
46 lines (36 loc) · 1.24 KB
/
create-post.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
For posterity: this was the original npm script I used but eventually got annoyed at it.
"post": "run(){ mkdir -p ./src/posts/$1 && touch ./src/posts/$1/index.mdx && echo \"---\ntitle: $1\ndate: 2020-MM-DD\ntags: []\n---\n\" >> ./src/posts/$1/index.mdx; }; run"
*/
const fs = require('fs')
const path = require('path')
const args = process.argv.slice(2)
if (args.length < 1) {
console.log('Usage: node create-post.js "<title>"')
process.exit(1)
}
// https://www.geeksforgeeks.org/how-to-convert-a-string-into-kebab-case-using-javascript/
const kebabCase = (str) =>
str
.replace(/([a-z])([A-Z])/g, '$1-$2') // replace "camelCase" to "camel-case"
.replace(/[\s_\/]+/g, '-') // Replace spaces, underscore, and slash with - (dash)
.toLowerCase()
const title = args[0]
const titleSlug = kebabCase(title)
const todaysDate = new Date().toISOString().split('T')[0]
// Create content
const content = `---
title: ${title}
date: ${todaysDate}
tags: []
---
intro...
<!-- excerpt-end -->
contents
` // looks weird but its not a mistake
// Create post
const dir = path.join(__dirname, 'src/posts', titleSlug)
fs.mkdirSync(dir)
const index = path.resolve(dir, 'index.md')
fs.writeFileSync(index, content)
console.log(`Created post "${title}" in ${index}`)