Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/perron/resource/publishable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/perron/resource/resource_publishable_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "test_helper"
require "tmpdir"

class Perron::Site::Resource::PublishableTest < ActiveSupport::TestCase
include ActiveSupport::Testing::TimeHelpers
Expand Down Expand Up @@ -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