Skip to content

Commit d2f48c8

Browse files
committed
update update_modified_dates logic
1 parent 73f9fac commit d2f48c8

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

.github/workflows/deploy_pages.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ jobs:
3131
ruby-version: '2.7.8'
3232
bundler-cache: true
3333

34+
- name: Refresh post modified dates
35+
run: ruby update_modified_dates.rb
36+
3437
- name: Build site
3538
run: bundle exec jekyll build --destination _site
3639

README_MODIFIED_DATES.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ ruby update_modified_dates.rb
2727

2828
1. **Scan all posts** in the `_posts` directory
2929
2. **Check Git history** to find the last modification date for each file
30-
3. **Compare dates** - only add `last_modified_at` if Git modification date is **after** the published date
31-
4. **Skip files** that already have `last_modified_at` field
32-
5. **Add the field** to YAML front matter automatically
30+
3. **Compare dates** - only add or update `last_modified_at` if Git modification date is **after** the published date
31+
4. **Preserve newer YAML values** - skip files whose existing `last_modified_at` is already newer than or equal to Git history
32+
5. **Add or update the field** in YAML front matter automatically
3333

3434
## Example
3535

@@ -65,9 +65,9 @@ last_modified_at: 2022-07-03
6565

6666
## Safety
6767

68-
- **Safe to re-run** - skips files that already have `last_modified_at`
68+
- **Safe to re-run** - only changes files when Git history provides a newer modified date
6969
- **Preview changes** with `git diff` before committing
70-
- **Only adds dates** when Git modification is after publication date
70+
- **Only adds or updates dates** when Git modification is after publication date
7171

7272
## Manual Alternative
7373

update_modified_dates.rb

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
require 'date'
55
require 'time'
66

7+
def parse_date(value)
8+
return nil if value.nil? || value.to_s.strip.empty?
9+
10+
Date.parse(value.to_s)
11+
rescue
12+
nil
13+
end
14+
715
def extract_front_matter(content)
816
# Check if file starts with YAML front matter
917
return nil, content unless content.start_with?('---')
@@ -18,7 +26,11 @@ def extract_front_matter(content)
1826
content_lines = lines[(end_index + 2)..-1] || []
1927

2028
begin
21-
front_matter = YAML.load(front_matter_lines.join)
29+
front_matter = YAML.safe_load(
30+
front_matter_lines.join,
31+
permitted_classes: [Date, Time],
32+
aliases: true
33+
)
2234
content_body = content_lines.join
2335
return front_matter, content_body
2436
rescue => e
@@ -65,16 +77,10 @@ def process_post(file_path)
6577
return
6678
end
6779

68-
# Skip if last_modified_at already exists
69-
if front_matter['last_modified_at']
70-
puts " Skipping: last_modified_at already exists (#{front_matter['last_modified_at']})"
71-
return
72-
end
73-
7480
# Get published date from front matter or filename
7581
published_date = nil
7682
if front_matter['date']
77-
published_date = Date.parse(front_matter['date'].to_s) rescue nil
83+
published_date = parse_date(front_matter['date'])
7884
end
7985

8086
unless published_date
@@ -95,14 +101,20 @@ def process_post(file_path)
95101
end
96102

97103
git_modified_date = git_modified.to_date
104+
current_modified_date = parse_date(front_matter['last_modified_at'])
98105

99-
# Only add last_modified_at if Git date is after published date
106+
# Only add or update last_modified_at if Git date is after published date
100107
if git_modified_date <= published_date
101108
puts " Skipping: Git modified date (#{git_modified_date}) is not after published date (#{published_date})"
102109
return
103110
end
111+
112+
if current_modified_date && git_modified_date <= current_modified_date
113+
puts " Skipping: Existing last_modified_at (#{current_modified_date}) is newer or equal"
114+
return
115+
end
104116

105-
# Add last_modified_at to front matter
117+
# Add or update last_modified_at in front matter
106118
front_matter['last_modified_at'] = git_modified_date
107119

108120
# Reconstruct the file content
@@ -114,7 +126,8 @@ def process_post(file_path)
114126
# Write back to file
115127
begin
116128
File.write(file_path, new_content)
117-
puts " ✓ Added last_modified_at: #{git_modified_date}"
129+
action = current_modified_date ? "Updated" : "Added"
130+
puts " ✓ #{action} last_modified_at: #{git_modified_date}"
118131
rescue => e
119132
puts " Error writing file: #{e.message}"
120133
end

0 commit comments

Comments
 (0)