Skip to content

Commit fde8049

Browse files
committed
alllll the compatibility b/w jekyll 3 and jekyll 2
1 parent 3af4ca7 commit fde8049

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

lib/jekyll-redirect-from.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
require "jekyll"
2+
3+
module JekyllRedirectFrom
4+
def self.jekyll_3?
5+
@jekyll_3 ||= (Jekyll::VERSION >= '3.0.0')
6+
end
7+
end
8+
29
require "jekyll-redirect-from/version"
310
require "jekyll-redirect-from/redirect_page"
411
require "jekyll-redirect-from/redirector"

lib/jekyll-redirect-from/redirect_page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def initialize(site, base, dir, name)
2121
site.frontmatter_defaults.find(File.join(dir, name), type, key)
2222
end
2323

24-
Jekyll::Hooks.trigger :pages, :post_init, self if defined?(Jekyll::Hooks)
24+
Jekyll::Hooks.trigger :pages, :post_init, self if JekyllRedirectFrom.jekyll_3?
2525
end
2626

2727
def generate_redirect_content(item_url)

spec/integrations_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe("Integration Tests") do
44
it "writes the redirect pages for collection items which are outputted" do
55
expect(dest_dir("articles", "redirect-me-plz.html")).to exist
6-
expect(dest_dir("articles", "23128432159832", "mary-had-a-little-lamb.html")).to exist
6+
expect(dest_dir("articles", "23128432159832", "mary-had-a-little-lamb#{forced_output_ext}")).to exist
77
end
88

99
it "doesn't write redirect pages for collection items which are not outputted" do

spec/jekyll_redirect_from/redirect_page_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@
3636
let(:redirect_page) { new_redirect_page(permalink_dir) }
3737

3838
it "knows to add the index.html if it's a folder" do
39-
dest = dest_dir("posts", "1914798137981389", "larry-had-a-little-lamb", "index.html").to_s
40-
expect(redirect_page.destination("/")).to eql(dest)
39+
expected = dest_dir("posts", "1914798137981389", "larry-had-a-little-lamb", "index.html").to_s
40+
dest = redirect_page.destination("/")
41+
dest = "#{@site.dest}#{dest}" unless dest.start_with? @site.dest
42+
expect(dest).to eql(expected)
4143
end
4244
end
4345

4446
context "of a redirect page meant to be a file" do
4547
it "knows not to add the index.html if it's not a folder" do
46-
dest = dest_dir("posts", "12435151125", "larry-had-a-little-lamb.html").to_s
47-
expect(redirect_page.destination("/")).to eql(dest)
48+
expected = dest_dir("posts", "12435151125", "larry-had-a-little-lamb#{forced_output_ext}").to_s
49+
dest = redirect_page.destination("/")
50+
dest = "#{@site.dest}#{dest}" unless dest.start_with? @site.dest
51+
expect(dest).to eql(expected)
4852
end
4953
end
5054
end
@@ -58,7 +62,7 @@
5862
end
5963

6064
it "fetches the path properly" do
61-
expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb.html$/
65+
expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb#{forced_output_ext}$/
6266
end
6367

6468
it "is written to the proper location" do

spec/jekyll_redirect_from/redirector_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
let(:page_with_many_redirect_to) { setup_page("multiple_redirect_tos.md") }
1212

1313
it "knows if a page or post is requesting a redirect page" do
14-
if Jekyll::VERSION < '3.0.0'
15-
expect(redirector.has_alt_urls?(post_to_redirect)).to be_truthy
16-
else
14+
if JekyllRedirectFrom.jekyll_3?
1715
skip "Don't need to test posts in Jekyll 3"
16+
else
17+
expect(redirector.has_alt_urls?(post_to_redirect)).to be_truthy
1818
end
1919
end
2020

@@ -52,7 +52,7 @@
5252
end
5353

5454
it "generates the refresh page for the post properly" do
55-
expect(dest_dir("posts/23128432159832/mary-had-a-little-lamb.html")).to exist
55+
expect(dest_dir("posts/23128432159832/mary-had-a-little-lamb#{forced_output_ext}")).to exist
5656
end
5757

5858
it "generates the refresh pages for the page with multiple redirect_from urls" do
@@ -63,7 +63,7 @@
6363
end
6464

6565
it "generates the refresh page for the page with one redirect_from url" do
66-
expect(dest_dir("mencius/was/my/father.html")).to exist
66+
expect(dest_dir("mencius/was/my/father#{forced_output_ext}")).to exist
6767
end
6868

6969
it "generates the refresh page for the collection with one redirect_to url" do

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def new_redirect_page(permalink)
6767
def destination_sitemap
6868
@dest.join("sitemap.xml").read
6969
end
70+
71+
def forced_output_ext
72+
JekyllRedirectFrom.jekyll_3? ? ".html" : ""
73+
end
7074
end
7175

7276
class TestStringContainer

0 commit comments

Comments
 (0)