diff --git a/package.json b/package.json index 794d1aa..09e0793 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "start": "astro dev", "build": "astro build", "preview": "astro preview", - "astro": "astro" + "astro": "astro", + "new-post": "node scripts/new-post.js" }, "dependencies": { "@astrojs/check": "^0.2.0", diff --git a/public/images/demo-banner.jpg b/public/images/demo-banner.jpg new file mode 100644 index 0000000..f8c0310 Binary files /dev/null and b/public/images/demo-banner.jpg differ diff --git a/scripts/new-post.js b/scripts/new-post.js new file mode 100644 index 0000000..431de66 --- /dev/null +++ b/scripts/new-post.js @@ -0,0 +1,52 @@ +import fs from 'fs'; +import path from 'path'; + +function getDate() { + const today = new Date(); + const year = today.getFullYear(); + const month = String(today.getMonth() + 1).padStart(2, '0'); //月份从0开始,所以要加1 + const day = String(today.getDate()).padStart(2, '0'); + + return `${year}-${month}-${day}`; +} + +const args = process.argv.slice(2); + +if (args.length === 0) { + console.error(`Error: No filename argument provided +Usage: npm run new-post -- `); + process.exit(1); // Terminate the script and return error code 1 +} + +let fileName = args[0]; + +// Add .md extension if not present +const fileExtensionRegex = /\.(md|mdx)$/i; +if (!fileExtensionRegex.test(fileName)) { + fileName += '.md'; +} + +const targetDir = './src/content/posts/'; +const fullPath = path.join(targetDir, fileName); + +if (fs.existsSync(fullPath)) { + console.error(`Error:File ${fullPath} already exists `); + process.exit(1); +} + +const content = +`--- +title: ${args[0]} +published: ${getDate()} +description: +cover: + url: + alt: +tags: [] +categories: [] +--- +`; + +fs.writeFileSync(path.join(targetDir, fileName), content); + +console.log(`Post ${fullPath} created`); diff --git a/src/components/ArchivePanel.astro b/src/components/ArchivePanel.astro index eeab882..30749e1 100644 --- a/src/components/ArchivePanel.astro +++ b/src/components/ArchivePanel.astro @@ -26,7 +26,7 @@ if (Array.isArray(categories) && categories.length > 0) { const groups = function () { const groupedPosts = posts.reduce((grouped, post) => { - const year = post.data.pubDate.getFullYear() + const year = post.data.published.getFullYear() if (!grouped[year]) { grouped[year] = [] } @@ -73,7 +73,7 @@ function formatDate(date: Date) {