2024-02-18 18:13:43 +08:00
|
|
|
|
/* This is a script to create a new post markdown file with front-matter */
|
|
|
|
|
|
2024-01-21 12:54:41 +08:00
|
|
|
|
import fs from "fs"
|
|
|
|
|
import path from "path"
|
2023-10-18 17:31:11 +08:00
|
|
|
|
|
|
|
|
|
function getDate() {
|
2024-01-21 12:54:41 +08:00
|
|
|
|
const today = new Date()
|
|
|
|
|
const year = today.getFullYear()
|
2024-02-18 18:13:43 +08:00
|
|
|
|
const month = String(today.getMonth() + 1).padStart(2, "0")
|
2024-01-21 12:54:41 +08:00
|
|
|
|
const day = String(today.getDate()).padStart(2, "0")
|
2023-10-18 17:31:11 +08:00
|
|
|
|
|
2024-01-21 12:54:41 +08:00
|
|
|
|
return `${year}-${month}-${day}`
|
2023-10-18 17:31:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-21 12:54:41 +08:00
|
|
|
|
const args = process.argv.slice(2)
|
2023-10-18 17:31:11 +08:00
|
|
|
|
|
|
|
|
|
if (args.length === 0) {
|
2024-01-21 12:54:41 +08:00
|
|
|
|
console.error(`Error: No filename argument provided
|
|
|
|
|
Usage: npm run new-post -- <filename>`)
|
|
|
|
|
process.exit(1) // Terminate the script and return error code 1
|
2023-10-18 17:31:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-21 12:54:41 +08:00
|
|
|
|
let fileName = args[0]
|
2023-10-18 17:31:11 +08:00
|
|
|
|
|
|
|
|
|
// Add .md extension if not present
|
2024-01-21 12:54:41 +08:00
|
|
|
|
const fileExtensionRegex = /\.(md|mdx)$/i
|
2023-10-18 17:31:11 +08:00
|
|
|
|
if (!fileExtensionRegex.test(fileName)) {
|
2024-01-21 12:54:41 +08:00
|
|
|
|
fileName += ".md"
|
2023-10-18 17:31:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-21 12:54:41 +08:00
|
|
|
|
const targetDir = "./src/content/posts/"
|
|
|
|
|
const fullPath = path.join(targetDir, fileName)
|
2023-10-18 17:31:11 +08:00
|
|
|
|
|
|
|
|
|
if (fs.existsSync(fullPath)) {
|
2024-01-21 12:54:41 +08:00
|
|
|
|
console.error(`Error:File ${fullPath} already exists `)
|
|
|
|
|
process.exit(1)
|
2023-10-18 17:31:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-21 12:54:41 +08:00
|
|
|
|
const content = `---
|
2023-10-18 17:31:11 +08:00
|
|
|
|
title: ${args[0]}
|
|
|
|
|
published: ${getDate()}
|
2024-01-21 20:19:34 +08:00
|
|
|
|
description: ''
|
|
|
|
|
image: ''
|
2023-10-18 17:31:11 +08:00
|
|
|
|
tags: []
|
2024-01-21 20:19:34 +08:00
|
|
|
|
category: ''
|
|
|
|
|
draft: false
|
2024-08-27 23:52:30 +08:00
|
|
|
|
language: ''
|
2023-10-18 17:31:11 +08:00
|
|
|
|
---
|
2024-01-21 12:54:41 +08:00
|
|
|
|
`
|
2023-10-18 17:31:11 +08:00
|
|
|
|
|
2024-01-21 12:54:41 +08:00
|
|
|
|
fs.writeFileSync(path.join(targetDir, fileName), content)
|
2023-10-18 17:31:11 +08:00
|
|
|
|
|
2024-01-21 12:54:41 +08:00
|
|
|
|
console.log(`Post ${fullPath} created`)
|