Skip to content

Commit 1980476

Browse files
authored
Support for image paths relative to the page's directory (#466)
Merge pull request 466
1 parent 7d1d478 commit 1980476

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

lib/jekyll-seo-tag/image_drop.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ def raw_path
6161

6262
def absolute_url
6363
return unless raw_path
64-
return @absolute_url if defined? @absolute_url
64+
@absolute_url ||= build_absolute_path
65+
end
66+
67+
def build_absolute_path
68+
return raw_path unless raw_path.is_a?(String) && absolute_url?(raw_path) == false
69+
return filters.absolute_url(raw_path) if raw_path.start_with?("/")
70+
71+
page_dir = @page["url"]
72+
page_dir = File.dirname(page_dir) unless page_dir.end_with?("/")
6573

66-
@absolute_url = if raw_path.is_a?(String) && absolute_url?(raw_path) == false
67-
filters.absolute_url raw_path
68-
else
69-
raw_path
70-
end
74+
filters.absolute_url File.join(page_dir, raw_path)
7175
end
7276

7377
def filters

spec/jekyll_seo_tag/drop_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@
415415
end
416416

417417
context "image" do
418-
let(:image) { "foo.png" }
418+
let(:image) { "/foo.png" }
419419
let(:page_meta) { { "image" => image } }
420420

421421
it "returns a Drop" do

spec/jekyll_seo_tag/image_drop_spec.rb

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe Jekyll::SeoTag::ImageDrop do
44
let(:config) { { "title" => "site title" } }
55
let(:image) { nil }
6-
let(:page_meta) { { "image" => image } }
6+
let(:page_meta) { { "image" => image, "dir" => "foo" } }
77
let(:page) { make_page(page_meta) }
88
let(:site) { make_site(config) }
99
let(:context) { make_context(:page => page, :site => site) }
@@ -14,8 +14,34 @@
1414
Jekyll.logger.log_level = :error
1515
end
1616

17-
context "with image as a string" do
17+
context "with a post object" do
1818
let(:image) { "image.png" }
19+
let(:page_meta) { { "image" => image, "date" => "2017-01-01" } }
20+
let(:page) { make_post(page_meta) }
21+
22+
it "returns the image url relative to the post directory" do
23+
expect(subject["path"]).to eql("/2017/01/01/image.png")
24+
end
25+
end
26+
27+
context "with image as a relative path" do
28+
let(:image) { "image.png" }
29+
30+
it "returns the image with the page dir prepended" do
31+
expect(subject["path"]).to eql("/foo/image.png")
32+
end
33+
34+
context "with site.url" do
35+
let(:config) { { "url" => "http://example.com" } }
36+
37+
it "makes the path absolute" do
38+
expect(subject["path"]).to eql("http://example.com/foo/image.png")
39+
end
40+
end
41+
end
42+
43+
context "with image as an absolute path" do
44+
let(:image) { "/image.png" }
1945

2046
it "returns the image" do
2147
expect(subject["path"]).to eql("/image.png")
@@ -30,7 +56,7 @@
3056
end
3157

3258
context "with a URL-escaped path" do
33-
let(:image) { "some image.png" }
59+
let(:image) { "/some image.png" }
3460

3561
it "URL-escapes the image" do
3662
expect(subject["path"]).to eql("/some%20image.png")
@@ -39,24 +65,24 @@
3965
end
4066

4167
context "with image as a hash" do
42-
context "with a path" do
43-
let(:image) { { "path" => "image.png" } }
68+
context "with an absolute path" do
69+
let(:image) { { "path" => "/image.png" } }
4470

4571
it "returns the image" do
4672
expect(subject["path"]).to eql("/image.png")
4773
end
4874
end
4975

5076
context "with facebook" do
51-
let(:image) { { "facebook" => "image.png" } }
77+
let(:image) { { "facebook" => "/image.png" } }
5278

5379
it "returns the image" do
5480
expect(subject["path"]).to eql("/image.png")
5581
end
5682
end
5783

5884
context "with twitter" do
59-
let(:image) { { "twitter" => "image.png" } }
85+
let(:image) { { "twitter" => "/image.png" } }
6086

6187
it "returns the image" do
6288
expect(subject["path"]).to eql("/image.png")

spec/jekyll_seo_tag/json_ld_drop_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113
end
114114

115115
context "image" do
116-
context "with image as a string" do
117-
let(:image) { "image" }
116+
context "with image as an absolute path" do
117+
let(:image) { "/image" }
118118

119119
it "returns the image as a string" do
120120
expect(subject).to have_key("image")
@@ -124,7 +124,7 @@
124124
end
125125

126126
context "with image as a hash" do
127-
let(:image) { { "path" => "image", "height" => 5, "width" => 10 } }
127+
let(:image) { { "path" => "/image", "height" => 5, "width" => 10 } }
128128

129129
it "returns the image as a hash" do
130130
expect(subject).to have_key("image")

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def source_dir
3131
}.freeze
3232

3333
def make_page(options = {})
34-
page = Jekyll::Page.new site, CONFIG_DEFAULTS["source"], "", "page.md"
34+
page = Jekyll::Page.new site, CONFIG_DEFAULTS["source"], options.delete("dir") || "", "page.md"
3535
page.data = options
3636
page
3737
end

0 commit comments

Comments
 (0)