Skip to content

Commit

Permalink
updates for script to update last modified
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHammond committed Jul 1, 2024
1 parent 2379588 commit 06a5c6e
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
2 changes: 1 addition & 1 deletion _posts/2022-07-10-First-Blog-Post.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: post
title: First Blog Post
description: "A sample blog post for JekyllExample.com"
date: '2022-07-10T23:00:15+00:00'
last_modified_at: '2024-05-16T08:05:00+00:00'
last_modified_at: '2024-07-01T15:58:25+00:00'
permalink: first-blog-post
image: BlogPost-1.jpg
categories: [ Sample ]
Expand Down
1 change: 1 addition & 0 deletions _posts/2022-11-01-Non-featured-blog-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ image: BlogPost-2.jpg
categories: [ Sample ]
featured: false
comments: false
last_modified_at: '2024-07-01T15:58:25+00:00'
---

This is the second blog post, the difference is that this one is not featured.
Expand Down
2 changes: 1 addition & 1 deletion _posts/2023-01-31-Getting-started-with-Jekyll.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: post
title: Getting started with Jekyll
description: A simple list of steps to get started with Jekyll.
date: '2023-01-31T10:00:15+00:00'
last_modified_at: '2024-05-16T08:05:00+00:00'
last_modified_at: '2024-07-01T15:58:25+00:00'
permalink: getting-started-with-jekyll
image: BlogPost-3.jpg
categories: [ Jekyll, Tutorial ]
Expand Down
2 changes: 1 addition & 1 deletion _posts/2023-02-01-Easy-instructions-to-install-jekyll.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: post
title: Easy instructions for installing Jekyll
date: '2023-02-01T10:00:15+00:00'
last_modified_at: '2024-05-16T08:05:00+00:00'
last_modified_at: '2024-07-01T15:58:25+00:00'
permalink: easy-installation-instructions-for-jekyll
description: A blog post that provides easy instructions for installing Jekyll.
image: BlogPost-4.jpg
Expand Down
2 changes: 1 addition & 1 deletion _posts/2024-01-17-Sample-Jekyll-Sites.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: post
title: Example Jekyll Sites based on this Repository
description: A list of sites built on this template
date: '2023-02-01T10:00:15+00:00'
last_modified_at: '2024-05-16T08:05:00+00:00'
last_modified_at: '2024-07-01T15:58:25+00:00'
permalink: sample-jekyll-sites
image:
categories: [ Jekyll, Sample, Example ]
Expand Down
1 change: 1 addition & 0 deletions _posts/2024-04-18-Install-and-run-jekyll-on-ubuntu.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ image: BlogPost-4.jpg
categories: [ Jekyll, Tutorial ]
featured: true
comments: false
last_modified_at: '2024-07-01T15:58:25+00:00'
---
## *This post is still a work in progress as of 05/17/2024*

Expand Down
2 changes: 1 addition & 1 deletion _posts/2024-06-21-structured-content-in-jekyll.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: post
title: Structured Content in Jekyll
date: '2024-06-21T10:00:15+00:00'
last_modified_at: '2024-06-24T14:44:00+00:00'
last_modified_at: '2024-07-01T15:58:25+00:00'
permalink: structured-content-in-jekyll
description: A blog post showing how Structured Content can be leveraged using this JekyllExample project to help get your content out to the masses.
image: BlogPost-5.jpg
Expand Down
86 changes: 86 additions & 0 deletions _scripts/update_last_modified_at.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash

# This script is used to update all your _posts last_modified_at attribute to the current date/time.
# You typically want to do this when you update the structure on your site, not every time you create a new post
# To do this, you need to be in a prompt do the following to things
# Make the script executable: chmod +x ../_scripts/update_last_modified.sh.
# Run the script from the _scripts directory: ../_scripts/update_last_modified.sh.


# Set the directory containing your Jekyll posts
POSTS_DIR="../_posts"

# Set the new last modified date (current date and time in ISO 8601 format)
NEW_DATE=$(date -u +"%Y-%m-%dT%H:%M:%S+00:00")

# Function to remove BOM from a file
remove_bom() {
local file="$1"
if [ "$(head -c 3 "$file")" = $'\xef\xbb\xbf' ]; then
tail -c +4 "$file" > "$file.nobom" && mv "$file.nobom" "$file"
echo "Removed BOM from $file"
fi
}

# Loop through each markdown file in the posts directory
for file in "$POSTS_DIR"/*.md; do
echo "Processing file: $file"

# Remove BOM if present
remove_bom "$file"

temp_file=$(mktemp)
inside_front_matter=false
last_modified_at_present=false
last_modified_at_line="last_modified_at: '$NEW_DATE'"
front_matter=()
content=()

while IFS= read -r line; do
if [[ "$line" == "---" ]]; then
if $inside_front_matter; then
# Closing front matter
inside_front_matter=false
if ! $last_modified_at_present; then
front_matter+=("$last_modified_at_line")
fi
front_matter+=("$line")
else
# Opening front matter
inside_front_matter=true
front_matter+=("$line")
fi
elif $inside_front_matter; then
# Check for existing last_modified_at and remove it
if [[ "$line" == "last_modified_at:"* ]]; then
last_modified_at_present=true
# Update the line to the new date
front_matter+=("$last_modified_at_line")
else
front_matter+=("$line")
fi
else
content+=("$line")
fi
done < "$file"

# Ensure last_modified_at is in the front matter if it wasn't already present
if ! $front_matter_complete; then
if ! $last_modified_at_present; then
front_matter+=("$last_modified_at_line")
fi
front_matter+=("---")
fi

# Write the front matter and content back to the file
{
printf "%s\n" "${front_matter[@]}"
printf "%s\n" "${content[@]}"
} > "$temp_file"

# Overwrite the original file with the fixed content
mv "$temp_file" "$file"
echo "Updated last_modified_at date to '$NEW_DATE' in $file"
done

echo "All files have been updated."

0 comments on commit 06a5c6e

Please sign in to comment.