Skip to content

Commit

Permalink
feat: add create-draft markdown script
Browse files Browse the repository at this point in the history
  • Loading branch information
jeferson-sb committed Aug 16, 2024
1 parent 3583b13 commit 18e86a3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"optimize": "vite optimize",
"lint": "eslint ./src --ext .{vue,ts,js} && stylelint 'src/**/*.css'",
"prettier": "prettier 'src/**/*.js' -w",
"deps:up": "taze major -I"
"deps:up": "taze major -I",
"start:draft": "node scripts/create-draft.js"
},
"dependencies": {
"@fingerprintjs/fingerprintjs": "^4.2.2",
Expand Down
62 changes: 62 additions & 0 deletions scripts/create-draft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import readline from 'node:readline/promises'

const defaults = {
id: Math.floor(Math.random() * 1e15).toString(36),
published_at: new Date().toISOString(),
excerpt: '',
crosspostedOn: '',
crosspostLink: '',
}

const slugify = (str) =>
str
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '')

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})

const title = await rl.question("What's the title of your post? ")
const tags = await rl.question(
'Add the tags you would like? (separated by comma) '
)

rl.close()

const slug = slugify(title)

const dir = path.resolve('.')
const filename = `${slug}.md`
const output = path.join(dir, 'content/articles', filename)

console.log('\n✍️ Drafting new doc...\n')

try {
const post = {
...defaults,
title,
tags,
slug,
}
let frontmatter = '---\n'

for (const [k, v] of Object.entries(post)) {
frontmatter += `${k}: ${v}\n`
}

frontmatter += '---\n'

await fs.writeFile(output, frontmatter, {
encoding: 'utf-8',
})
console.log('✅ Open: ', output)
} catch (error) {
console.error(error)
}

0 comments on commit 18e86a3

Please sign in to comment.