Support for image paths relative to the page's directory (#466)

Merge pull request 466
This commit is contained in:
Scott Perry 2022-09-04 07:19:44 -07:00 committed by GitHub
parent 7d1d478c0b
commit 1980476395
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 18 deletions

View File

@ -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

View File

@ -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

View File

@ -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")

View File

@ -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")

View File

@ -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