Creating New Posts (Script)
Relevant source files
The following files were used as context for generating this wiki page:The new-post.js script is a command-line utility designed to streamline the creation of new blog posts. It automates the generation of a new Markdown file, pre-populated with a standard frontmatter template. This ensures consistency across all posts and reduces the manual setup required for authors.
The script is intended to be run via a package manager command, which is defined in the project's package.json file. Its primary function is to create a file in the correct content directory with a valid structure, ready for content to be added.
Sources: scripts/new-post.js, README.md:32-35, package.json:10
Script Execution and Usage
The script is executed from the command line using pnpm. It requires a single argument, which is the desired filename for the new post.
pnpm new-post <filename>
Sources: README.md:35
This command is an alias defined in package.json which executes the Node.js script.
"scripts": {
"new-post": "node scripts/new-post.js"
}
Sources: package.json:10
The following diagram illustrates the user-facing execution flow.
Sources: package.json:10, scripts/new-post.js
Core Logic and Flow
The script follows a clear, sequential process to validate input and create the new post file. It includes checks to prevent errors, such as overwriting existing files or failing to provide a filename.
The internal logic of the new-post.js script is detailed in the flowchart below.
This diagram shows the step-by-step process from argument parsing to file creation.
Sources: scripts/new-post.js:14-56
Key Steps:
- Argument Parsing: The script retrieves the filename from the command-line arguments. If no filename is provided, it logs an error and exits.
Sources: scripts/new-post.js:14-19 - Filename Normalization: It checks if the filename already has a
.mdor.mdxextension. If not, it appends.md.
Sources: scripts/new-post.js:22-25 - Path and Existence Check: The script constructs the full path to the target file within
src/content/posts/. It then checks if a file at this path already exists. If it does, the script logs an error and exits to prevent accidental overwrites.
Sources: scripts/new-post.js:27-33 - Directory Creation: It ensures that the target directory for the post exists. If the filename includes a path (e.g.,
2023-01-01/my-post), it will create the necessary subdirectories recursively.
Sources: scripts/new-post.js:36-39 - File Generation: The script creates the file content, which consists of a YAML frontmatter block. The
titleis derived from the filename argument, and thepublisheddate is set to the current date.
Sources: scripts/new-post.js:44-54 - File Writing: Finally, the generated content is written to the new file on the filesystem.
Sources: scripts/new-post.js:56
Generated File Structure
File Location
All new posts are created within the ./src/content/posts/ directory. The script supports creating posts in subdirectories, which is a common pattern for organizing posts and their associated assets.
src/content/posts/
├── post-1.md
└── post-2/
├── cover.png
└── index.md
Sources: scripts/new-post.js:27, src/content/posts/2018-10-30/Charlotte-Markdown.md:29-34
Frontmatter Content
The script generates a Markdown file with a predefined frontmatter block. This structure is essential for how the Astro content collection processes and renders posts.
const content = `---
title: ${args[0]}
published: ${getDate()}
description: ''
image: ''
tags: []
category: ''
draft: false
lang: ''
---
`;
Sources: scripts/new-post.js:44-54
The generated fields correspond directly to the post schema defined in the project.
| Field | Default Value (from script) | Description | Schema Type |
|---|---|---|---|
title | <filename> | The title of the post. | string |
published | Current date (YYYY-MM-DD) | The publication date. | date |
description | '' (empty string) | A short summary of the post. | string (optional) |
image | '' (empty string) | Path to the post's cover image. | string (optional) |
tags | [] (empty array) | A list of tags associated with the post. | array(string) (optional) |
category | '' (empty string) | The category of the post. | string (optional) |
draft | false | If true, the post will not be published in production. | boolean (optional) |
lang | '' (empty string) | The language of the post. | string (optional) |
This structure is validated against the postsCollection schema defined in src/content/config.ts.
Sources: scripts/new-post.js:44-54, src/content/config.ts:4-19, frontmatter.json:28-86
Dependencies
The script relies exclusively on built-in Node.js modules for its functionality, requiring no external packages.
node:fs: Used for filesystem operations like checking for file existence (fs.existsSync), creating directories (fs.mkdirSync), and writing files (fs.writeFileSync).node:path: Used for handling and transforming file paths, such as joining path segments (path.join) and getting the directory name (path.dirname).
Sources: scripts/new-post.js:3-4