Created
September 30, 2024 09:29
-
-
Save omgmog/a6de66c8ae1e2c763fe5ad536938e7c1 to your computer and use it in GitHub Desktop.
Migrate Jekyll filename-based post dates to Astro pubDate frontmatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
BLOG_DIR="src/content/blog" | |
# Loop through each .md file in the directory | |
for file in "$BLOG_DIR"/*.md; do | |
# Extract the filename without the directory path | |
filename=$(basename "$file") | |
# Extract the date portion from the filename (yyyy-mm-dd) | |
date=$(echo "$filename" | grep -Eo '^[0-9]{4}-[0-9]{2}-[0-9]{2}') | |
# Check if the date was extracted successfully | |
if [[ -n "$date" ]]; then | |
# Check if the file already contains a pubDate | |
if grep -q "^pubDate:" "$file"; then | |
echo "pubDate already exists in $file, skipping pubDate insertion..." | |
else | |
# Insert the pubDate into the frontmatter section | |
awk -v date="$date" ' | |
BEGIN { found = 0 } | |
/^---$/ { found++ } | |
found == 2 && !pubDateInserted { | |
print "pubDate: " date | |
pubDateInserted = 1 | |
} | |
{ print } | |
' "$file" > "$file.tmp" && mv "$file.tmp" "$file" | |
echo "Updated pubDate in $file" | |
fi | |
# Rename the file to omit the date in the filename | |
new_filename=$(echo "$filename" | sed -E 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}-//') | |
mv "$file" "$BLOG_DIR/$new_filename" | |
echo "Renamed $file to $new_filename" | |
else | |
echo "No date found in filename $filename" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment