The frontmatter of astro-minimax blog posts includes pubDatetime (publication date) and modDatetime (modification date) fields. Manually maintaining these dates is tedious and easy to forget. This article explains how to handle this automatically with Git hooks.
Important Note
The hook only auto-fills empty date fields. It will NOT overwrite values you’ve manually specified.
If you’ve already set
pubDatetimeormodDatetimein your frontmatter, the hook preserves your values.
Option 1: One-Click Install via CLI (Recommended)
The astro-minimax CLI provides a hooks command that automatically installs Husky and configures the pre-commit hook:
# Run from anywhere in your blog project (supports subdirectories)
astro-minimax hooks installbashThis will:
- Detect project type (single project / Monorepo)
- Install Husky as a dev dependency
- Create
.husky/pre-commithook script - Configure the
preparescript
After installation, every git commit will auto-fill empty date fields:
| Scenario | Condition | Behavior |
|---|---|---|
| New post | pubDatetime is empty | Auto-fill with current time |
| New post | pubDatetime has value | Skip, keep original value |
| Modified post | draft: false + modDatetime empty | Auto-fill with current time |
| Modified post | draft: false + modDatetime has value | Skip, keep original value |
| First publish | draft: first | Change to draft: false, clear modDatetime |
Other commands:
astro-minimax hooks status # Show current status
astro-minimax hooks uninstall # Remove hooksbashOption 2: Manual Configuration
If you prefer to configure it yourself, follow these steps.
Step 1: Install Husky
Husky is a Git hook management tool:
pnpm add -D husky
npx husky initbashStep 2: Create the pre-commit Hook
Edit .husky/pre-commit:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# Only auto-fill empty date fields, never overwrite existing values
git diff --cached --name-status | while read -r status file; do
case "$file" in
src/data/blog/*.md|src/data/blog/**/*.md)
case "$status" in
M)
filecontent=$(cat "$file" 2>/dev/null) || continue
frontmatter=$(echo "$filecontent" | awk -v RS='---' 'NR==2{print}')
draft=$(echo "$frontmatter" | awk '/^draft: /{print $2}')
if [ "$draft" = "false" ]; then
modDatetime=$(echo "$frontmatter" | awk '/^modDatetime: /{print $2}')
if [ -z "$modDatetime" ] || [ "$modDatetime" = "" ]; then
echo "Auto-filled modDatetime: $file"
sed -i.bak "/---.*/,/---.*/s/^modDatetime:.*$/modDatetime: $(date -u "+%Y-%m-%dT%H:%M:%SZ")/" "$file" && rm -f "$file.bak"
git add "$file"
fi
fi
if [ "$draft" = "first" ]; then
echo "First release: $file"
sed -i.bak -e "/---.*/,/---.*/s/^modDatetime:.*$/modDatetime:/" -e "/---.*/,/---.*/s/^draft:.*$/draft: false/" "$file" && rm -f "$file.bak"
git add "$file"
fi
;;
A)
filecontent=$(cat "$file" 2>/dev/null) || continue
frontmatter=$(echo "$filecontent" | awk -v RS='---' 'NR==2{print}')
pubDatetime=$(echo "$frontmatter" | awk '/^pubDatetime: /{print $2}')
if [ -z "$pubDatetime" ] || [ "$pubDatetime" = "" ]; then
echo "Auto-filled pubDatetime: $file"
sed -i.bak "/---.*/,/---.*/s/^pubDatetime:.*$/pubDatetime: $(date -u "+%Y-%m-%dT%H:%M:%SZ")/" "$file" && rm -f "$file.bak"
git add "$file"
fi
;;
esac
;;
esac
doneshellHook Logic Explained
New files (A):
- Check if
pubDatetimeis empty - If empty, fill with current time; if has value, skip
Modified files (M):
- Check
draftstatus - If
draft: false: check ifmodDatetimeis empty, fill if empty - If
draft: first: change todraft: false, clearmodDatetime
First Publish Workflow
Use draft: first for automated first-time publishing:
---
title: "New Post"
pubDatetime: # Leave empty, hook will auto-fill
modDatetime: # Leave empty
draft: first # First publish marker
---yamlOn commit, the hook will:
- Auto-fill
pubDatetime - Change
drafttofalse - Future modifications will auto-update
modDatetime
Notes
- Git hooks are local only — Team members need to run
astro-minimax hooks installindividually - Files must be staged first — The hook runs on
git commitand processesgit added files - Monorepo support — CLI automatically detects git root and installs hooks in the correct location
- Manually specified dates are preserved — The hook only fills empty values
评论区
文明评论,共建和谐社区