diff --git a/lib/perron/resource/publishable.rb b/lib/perron/resource/publishable.rb index bfe5b55..b107aed 100644 --- a/lib/perron/resource/publishable.rb +++ b/lib/perron/resource/publishable.rb @@ -54,7 +54,15 @@ def date_from_filename return @date_from_filename if defined?(@date_from_filename) match = File.basename(file_path).match(DATE_REGEX) - @date_from_filename = match ? Date.new(match[:year].to_i, match[:month].to_i, match[:day].to_i).in_time_zone : nil + + @date_from_filename = + if match + begin + Date.new(match[:year].to_i, match[:month].to_i, match[:day].to_i).in_time_zone + rescue Date::Error + nil + end + end end end end diff --git a/test/perron/resource/resource_publishable_test.rb b/test/perron/resource/resource_publishable_test.rb index fd11d95..6f56cd3 100644 --- a/test/perron/resource/resource_publishable_test.rb +++ b/test/perron/resource/resource_publishable_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require "tmpdir" class Perron::Site::Resource::PublishableTest < ActiveSupport::TestCase include ActiveSupport::Testing::TimeHelpers @@ -74,4 +75,15 @@ class Perron::Site::Resource::PublishableTest < ActiveSupport::TestCase refute public_feature.draft? end + + test "#publication_date ignores a structurally valid but impossible date in the filename" do + Dir.mktmpdir do |dir| + path = File.join(dir, "2024-99-99-typo.md") + File.write(path, "---\ntitle: Typo\n---\nBody") + resource = Content::Post.new(path) + + assert_nil resource.publication_date + assert resource.published? + end + end end