44require 'date'
55require '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+
715def 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