From 593d09aacc6f5c6beccdb16877bfa85d08e6f8f5 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 26 Apr 2017 12:43:10 -0400 Subject: [PATCH 1/3] failing test for nil image paths --- spec/jekyll_seo_tag/drop_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index f73fdc6..1b32709 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -465,6 +465,14 @@ RSpec.describe Jekyll::SeoTag::Drop do end end + context "with some random hash" do + let(:image) { { "foo" => "bar"} } + + it "returns the image" do + expect(subject.image["path"]).to eql("/image.png") + end + end + context "with height and width" do let(:image) { { "path" => "image.png", "height" => 5, "width" => 10 } } From d00191c1e3e7828a95b7599521cf0ad0073406fe Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 26 Apr 2017 12:48:00 -0400 Subject: [PATCH 2/3] update failing tests for bad URLs --- spec/jekyll_seo_tag/drop_spec.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index 1b32709..0530c88 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -466,10 +466,18 @@ RSpec.describe Jekyll::SeoTag::Drop do end context "with some random hash" do - let(:image) { { "foo" => "bar"} } + let(:image) { { "foo" => "bar" } } - it "returns the image" do - expect(subject.image["path"]).to eql("/image.png") + 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 From 13bc107b3fe097301981b19248d804072efab69e Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 26 Apr 2017 12:48:08 -0400 Subject: [PATCH 3/3] guard against invalid or missing URLs --- lib/jekyll-seo-tag/drop.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index 03702dd..a0c426a 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -162,6 +162,7 @@ module Jekyll 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"] @@ -177,7 +178,9 @@ module Jekyll 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 @@ -205,7 +208,10 @@ module Jekyll end def absolute_url?(string) + return unless string Addressable::URI.parse(string).absolute? + rescue Addressable::URI::InvalidURIError + nil end def format_string(string)