From d62a1bf02b7360234f66a0cab134255abdf81853 Mon Sep 17 00:00:00 2001 From: Carl Tashian Date: Tue, 25 Mar 2025 10:28:31 -0700 Subject: [PATCH 1/2] Add updated_at date hook script --- .githooks/pre-commit | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 00000000..04618dd2 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,35 @@ +#!/bin/bash +# This pre-commit hook updates (or adds) the updated_at frontmatter in .mdx files with the current date. +# +# To install it, run the following from the top level of the repo: +# +# mkdir -p .git/hooks +# cp .githooks/pre-commit .git/hooks/pre-commit +# chmod +x .git/hooks/pre-commit + + +# Get the current date in "Month Day, Year" format (e.g., March 23, 2025) +CURRENT_DATE=$(date -u +"%B %d, %Y") + +# Function to update frontmatter +update_frontmatter() { + local file=$1 + if grep -q "^updated_at:" "$file"; then + # Update the existing updated_at field (macOS and Linux compatible) + sed -i "" -E "s/^updated_at:.*/updated_at: $CURRENT_DATE/" "$file" + else + # Add updated_at field if it doesn't exist (macOS and Linux compatible) + sed -i "" -E "/^---$/a\\ +updated_at: $CURRENT_DATE +" "$file" + fi +} + +# Find all staged Markdown files +for file in $(git diff --cached --name-only -- '*.mdx'); do + if [[ -f "$file" ]]; then + update_frontmatter "$file" + # Re-add the file to staging + git add "$file" + fi +done From a5542c010eb601d6366087265d2327b969c2cf00 Mon Sep 17 00:00:00 2001 From: Carl Tashian Date: Tue, 25 Mar 2025 11:02:54 -0700 Subject: [PATCH 2/2] Twekas to sed commands --- .githooks/pre-commit | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 04618dd2..d3f3b148 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -7,21 +7,24 @@ # cp .githooks/pre-commit .git/hooks/pre-commit # chmod +x .git/hooks/pre-commit - # Get the current date in "Month Day, Year" format (e.g., March 23, 2025) CURRENT_DATE=$(date -u +"%B %d, %Y") # Function to update frontmatter update_frontmatter() { local file=$1 - if grep -q "^updated_at:" "$file"; then - # Update the existing updated_at field (macOS and Linux compatible) - sed -i "" -E "s/^updated_at:.*/updated_at: $CURRENT_DATE/" "$file" - else - # Add updated_at field if it doesn't exist (macOS and Linux compatible) - sed -i "" -E "/^---$/a\\ + + # Ensure the file contains frontmatter + if [[ $(head -n 1 "$file") == "---" ]]; then + if grep -q "^updated_at:" "$file"; then + # Update the existing "updated_at" field + sed -i "" -E "s/^updated_at:.*/updated_at: $CURRENT_DATE/" "$file" + else + # Insert "updated_at" on the second line of the file + sed -i "" "2i\\ updated_at: $CURRENT_DATE " "$file" + fi fi } @@ -33,3 +36,5 @@ for file in $(git diff --cached --name-only -- '*.mdx'); do git add "$file" fi done + +