This commit is contained in:
Aron Petau 2025-05-05 18:16:58 +02:00
parent 37da020523
commit 178586c47a
12 changed files with 36 additions and 50 deletions

View file

@ -1,41 +1,28 @@
#!/bin/bash
# Directory to search for markdown files (content folder)
search_dir="../content"
# Current date for the 'updated' field
current_date=$(date +%F)
# 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
# Recursive search for all markdown files in the content folder
find ../content -type f -name "*.md" | while read file; do
# Check if the file has frontmatter enclosed in +++
if [ "$(head -n 1 "$file")" = "+++" ]; then
# Check if the 'date' field exists in the frontmatter
if grep -q '^date\s*=' "$file"; then
# Check if the 'updated' field is already present
if grep -q '^updated\s*=' "$file"; then
echo "✘ Skipped: 'updated' field already present in $file"
return
else
# Insert updated field directly after the date line without quotes
sed -i "" "/^date\s*=.*/a\\
updated = ${current_date}
" "$file"
echo "✔ Inserted 'updated' after date in $file"
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