Skip to content

Commit d62a1bf

Browse files
committed
Add updated_at date hook script
1 parent 7da8e2a commit d62a1bf

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

.githooks/pre-commit

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
# This pre-commit hook updates (or adds) the updated_at frontmatter in .mdx files with the current date.
3+
#
4+
# To install it, run the following from the top level of the repo:
5+
#
6+
# mkdir -p .git/hooks
7+
# cp .githooks/pre-commit .git/hooks/pre-commit
8+
# chmod +x .git/hooks/pre-commit
9+
10+
11+
# Get the current date in "Month Day, Year" format (e.g., March 23, 2025)
12+
CURRENT_DATE=$(date -u +"%B %d, %Y")
13+
14+
# Function to update frontmatter
15+
update_frontmatter() {
16+
local file=$1
17+
if grep -q "^updated_at:" "$file"; then
18+
# Update the existing updated_at field (macOS and Linux compatible)
19+
sed -i "" -E "s/^updated_at:.*/updated_at: $CURRENT_DATE/" "$file"
20+
else
21+
# Add updated_at field if it doesn't exist (macOS and Linux compatible)
22+
sed -i "" -E "/^---$/a\\
23+
updated_at: $CURRENT_DATE
24+
" "$file"
25+
fi
26+
}
27+
28+
# Find all staged Markdown files
29+
for file in $(git diff --cached --name-only -- '*.mdx'); do
30+
if [[ -f "$file" ]]; then
31+
update_frontmatter "$file"
32+
# Re-add the file to staging
33+
git add "$file"
34+
fi
35+
done

0 commit comments

Comments
 (0)