awebsite/scripts/add_update_frontmatter.sh
2025-05-05 18:01:25 +02:00

41 lines
1.4 KiB
Bash
Executable file

#!/bin/bash
# Directory to search for markdown files (content folder)
search_dir="../content"
# Function to check and update frontmatter with the latest date
update_frontmatter() {
local file="$1"
# Check if the file contains frontmatter with +++ delimiters
if grep -qE '^\+\+\+' "$file"; then
# Extract frontmatter from the file (between +++ markers)
frontmatter=$(sed -n '/^\+\+\+/,/^\+\+\+/p' "$file")
# Check if there's a 'date' field in the frontmatter
if echo "$frontmatter" | grep -qE 'date\s*='; then
# Check if there's already an 'updated' field in the frontmatter
if echo "$frontmatter" | grep -qE 'updated\s*='; then
echo "✘ Skipped: 'updated' field already present in $file"
return
fi
# Get the current modification time of the file (formatted as YYYY-MM-DD)
last_modified=$(date -r "$file" "+%Y-%m-%d")
# Update the frontmatter by adding the updated date below the 'date = ' line
sed -i "/^\+\+\+/ {n;s/$/\nupdated = $last_modified/}" "$file"
echo "✔ Updated frontmatter in $file"
else
echo "✘ No date field found in the frontmatter of $file"
fi
else
echo "✘ No frontmatter found in $file"
fi
}
# Recursively find all markdown files in the content folder and apply the update
find "$search_dir" -type f -name "*.md" | while read file; do
update_frontmatter "$file"
done