Skip to content

Commit

Permalink
Merge pull request #199 from jekyll/nil-image-path-fix
Browse files Browse the repository at this point in the history
Guard against invalid or missing URLs
  • Loading branch information
benbalter authored May 1, 2017
2 parents f998cbd + 696b361 commit a28cff0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/jekyll-seo-tag/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def image

image = { "path" => image } if image.is_a?(String)
image["path"] ||= image["facebook"] || image["twitter"]
return @image = nil unless image["path"]

unless absolute_url? image["path"]
image["path"] = filters.absolute_url image["path"]
Expand All @@ -177,7 +178,9 @@ def page_lang
end

def canonical_url
@canonical_url ||= filters.absolute_url(page["url"]).gsub(%r!/index\.html$!, "/")
@canonical_url ||= begin
filters.absolute_url(page["url"]).to_s.gsub(%r!/index\.html$!, "/")
end
end

private
Expand Down Expand Up @@ -205,7 +208,10 @@ def fallback_data
end

def absolute_url?(string)
return unless string
Addressable::URI.parse(string).absolute?
rescue Addressable::URI::InvalidURIError
nil
end

def format_string(string)
Expand Down
16 changes: 16 additions & 0 deletions spec/jekyll_seo_tag/drop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,22 @@
end
end

context "with some random hash" do
let(:image) { { "foo" => "bar" } }

it "returns nil" do
expect(subject.image).to be_nil
end
end

context "with an invalid path" do
let(:image) { ":" }

it "returns nil" do
expect(subject.image["path"]).to eql("/:")
end
end

context "with height and width" do
let(:image) { { "path" => "image.png", "height" => 5, "width" => 10 } }

Expand Down

0 comments on commit a28cff0

Please sign in to comment.