File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ # Get the current date in "Month Day, Year" format (e.g., March 23, 2025)
11+ CURRENT_DATE=$( date -u +" %B %d, %Y" )
12+
13+ # Function to update frontmatter
14+ update_frontmatter () {
15+ local file=$1
16+
17+ # Ensure the file contains frontmatter
18+ if [[ $( head -n 1 " $file " ) == " ---" ]]; then
19+ if grep -q " ^updated_at:" " $file " ; then
20+ # Update the existing "updated_at" field
21+ sed -i " " -E " s/^updated_at:.*/updated_at: $CURRENT_DATE /" " $file "
22+ else
23+ # Insert "updated_at" on the second line of the file
24+ sed -i " " " 2i\\
25+ updated_at: $CURRENT_DATE
26+ " " $file "
27+ fi
28+ fi
29+ }
30+
31+ # Find all staged Markdown files
32+ for file in $( git diff --cached --name-only -- ' *.mdx' ) ; do
33+ if [[ -f " $file " ]]; then
34+ update_frontmatter " $file "
35+ # Re-add the file to staging
36+ git add " $file "
37+ fi
38+ done
39+
40+
You can’t perform that action at this time.
0 commit comments