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
2 changes: 1 addition & 1 deletion lib/perron/html_processor/absolute_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def process

next if src.blank? || absolute_url?(src)

image["src"] = base_url + src
image["src"] = "#{base_url}/#{src.delete_prefix("/")}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/perron/resource/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def absolute_url(path)
return path if path.blank?
return path if path.start_with?("http://", "https://", "//")

Perron.configuration.url.delete_suffix("/") + path
"#{Perron.configuration.url.delete_suffix("/")}/#{path.delete_prefix("/")}"
end

def site_data
Expand Down
9 changes: 9 additions & 0 deletions test/perron/html_processor/absolute_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ def process_html(html)

assert_dom_equal html, processed
end

test 'inserts a slash for a relative src without a leading slash' do
html = '<img src="photo.jpg"><img src="nested/photo.jpg">'
processed = process_html(html)

assert_dom_equal '<img src="http://localhost:3000/photo.jpg">' \
'<img src="http://localhost:3000/nested/photo.jpg">',
processed
end
end
11 changes: 11 additions & 0 deletions test/perron/resource/metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ def setup
assert_equal 'Kendall', metadata.author, 'Frontmatter author should take highest precedence'
end

test 'makes a relative frontmatter image absolute with a slash separator' do
metadata = Perron::Resource::Metadata.new(
resource: @post,
frontmatter: { image: 'cover.jpg' },
collection: @posts_collection
).data

assert_equal 'http://localhost:3000/cover.jpg', metadata.image
assert_equal 'http://localhost:3000/cover.jpg', metadata.og_image
end

test 'removes nil values from final data after processing' do
metadata = Perron::Resource::Metadata.new(
resource: @about_page,
Expand Down