From 1980476395c6fc57e1013469606aabe3b747b621 Mon Sep 17 00:00:00 2001 From: Scott Perry Date: Sun, 4 Sep 2022 07:19:44 -0700 Subject: [PATCH] Support for image paths relative to the page's directory (#466) Merge pull request 466 --- lib/jekyll-seo-tag/image_drop.rb | 16 ++++++---- spec/jekyll_seo_tag/drop_spec.rb | 2 +- spec/jekyll_seo_tag/image_drop_spec.rb | 40 +++++++++++++++++++----- spec/jekyll_seo_tag/json_ld_drop_spec.rb | 6 ++-- spec/spec_helper.rb | 2 +- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/lib/jekyll-seo-tag/image_drop.rb b/lib/jekyll-seo-tag/image_drop.rb index 43367a4..647ce1f 100644 --- a/lib/jekyll-seo-tag/image_drop.rb +++ b/lib/jekyll-seo-tag/image_drop.rb @@ -61,13 +61,17 @@ module Jekyll def absolute_url return unless raw_path - return @absolute_url if defined? @absolute_url + @absolute_url ||= build_absolute_path + end - @absolute_url = if raw_path.is_a?(String) && absolute_url?(raw_path) == false - filters.absolute_url raw_path - else - raw_path - end + def build_absolute_path + return raw_path unless raw_path.is_a?(String) && absolute_url?(raw_path) == false + return filters.absolute_url(raw_path) if raw_path.start_with?("/") + + page_dir = @page["url"] + page_dir = File.dirname(page_dir) unless page_dir.end_with?("/") + + filters.absolute_url File.join(page_dir, raw_path) end def filters diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index d2f30b5..574213f 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -415,7 +415,7 @@ RSpec.describe Jekyll::SeoTag::Drop do end context "image" do - let(:image) { "foo.png" } + let(:image) { "/foo.png" } let(:page_meta) { { "image" => image } } it "returns a Drop" do diff --git a/spec/jekyll_seo_tag/image_drop_spec.rb b/spec/jekyll_seo_tag/image_drop_spec.rb index 50c79c6..7de87df 100644 --- a/spec/jekyll_seo_tag/image_drop_spec.rb +++ b/spec/jekyll_seo_tag/image_drop_spec.rb @@ -3,7 +3,7 @@ RSpec.describe Jekyll::SeoTag::ImageDrop do let(:config) { { "title" => "site title" } } let(:image) { nil } - let(:page_meta) { { "image" => image } } + let(:page_meta) { { "image" => image, "dir" => "foo" } } let(:page) { make_page(page_meta) } let(:site) { make_site(config) } let(:context) { make_context(:page => page, :site => site) } @@ -14,8 +14,34 @@ RSpec.describe Jekyll::SeoTag::ImageDrop do Jekyll.logger.log_level = :error end - context "with image as a string" do + context "with a post object" do let(:image) { "image.png" } + let(:page_meta) { { "image" => image, "date" => "2017-01-01" } } + let(:page) { make_post(page_meta) } + + it "returns the image url relative to the post directory" do + expect(subject["path"]).to eql("/2017/01/01/image.png") + end + end + + context "with image as a relative path" do + let(:image) { "image.png" } + + it "returns the image with the page dir prepended" do + expect(subject["path"]).to eql("/foo/image.png") + end + + context "with site.url" do + let(:config) { { "url" => "http://example.com" } } + + it "makes the path absolute" do + expect(subject["path"]).to eql("http://example.com/foo/image.png") + end + end + end + + context "with image as an absolute path" do + let(:image) { "/image.png" } it "returns the image" do expect(subject["path"]).to eql("/image.png") @@ -30,7 +56,7 @@ RSpec.describe Jekyll::SeoTag::ImageDrop do end context "with a URL-escaped path" do - let(:image) { "some image.png" } + let(:image) { "/some image.png" } it "URL-escapes the image" do expect(subject["path"]).to eql("/some%20image.png") @@ -39,8 +65,8 @@ RSpec.describe Jekyll::SeoTag::ImageDrop do end context "with image as a hash" do - context "with a path" do - let(:image) { { "path" => "image.png" } } + context "with an absolute path" do + let(:image) { { "path" => "/image.png" } } it "returns the image" do expect(subject["path"]).to eql("/image.png") @@ -48,7 +74,7 @@ RSpec.describe Jekyll::SeoTag::ImageDrop do end context "with facebook" do - let(:image) { { "facebook" => "image.png" } } + let(:image) { { "facebook" => "/image.png" } } it "returns the image" do expect(subject["path"]).to eql("/image.png") @@ -56,7 +82,7 @@ RSpec.describe Jekyll::SeoTag::ImageDrop do end context "with twitter" do - let(:image) { { "twitter" => "image.png" } } + let(:image) { { "twitter" => "/image.png" } } it "returns the image" do expect(subject["path"]).to eql("/image.png") diff --git a/spec/jekyll_seo_tag/json_ld_drop_spec.rb b/spec/jekyll_seo_tag/json_ld_drop_spec.rb index 3f1b5ff..1094b05 100644 --- a/spec/jekyll_seo_tag/json_ld_drop_spec.rb +++ b/spec/jekyll_seo_tag/json_ld_drop_spec.rb @@ -113,8 +113,8 @@ RSpec.describe Jekyll::SeoTag::JSONLDDrop do end context "image" do - context "with image as a string" do - let(:image) { "image" } + context "with image as an absolute path" do + let(:image) { "/image" } it "returns the image as a string" do expect(subject).to have_key("image") @@ -124,7 +124,7 @@ RSpec.describe Jekyll::SeoTag::JSONLDDrop do end context "with image as a hash" do - let(:image) { { "path" => "image", "height" => 5, "width" => 10 } } + let(:image) { { "path" => "/image", "height" => 5, "width" => 10 } } it "returns the image as a hash" do expect(subject).to have_key("image") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 554ba2f..ef2d82e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -31,7 +31,7 @@ CONFIG_DEFAULTS = { }.freeze def make_page(options = {}) - page = Jekyll::Page.new site, CONFIG_DEFAULTS["source"], "", "page.md" + page = Jekyll::Page.new site, CONFIG_DEFAULTS["source"], options.delete("dir") || "", "page.md" page.data = options page end