Skip to content

Commit

Permalink
Default to site.url
Browse files Browse the repository at this point in the history
Close #81.
  • Loading branch information
dentarg committed Aug 19, 2016
1 parent 7d0770d commit 973f4b9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -100,10 +100,10 @@ redirect_from: /post/123456798/
```

### Prefix
If `site.baseurl` is set, its value is used as a prefix for the redirect url automatically.
This is useful for scenarios where a site isn't available from the domain root, so the redirects point to the correct path.

**_Note_**: If you are hosting your Jekyll site on [GitHub Pages](https://pages.github.com/), the prefix is set to the pages domain name i.e. `http://example.github.io/project` or a custom `CNAME`.
If `site.url` is set, its value, together with `site.baseurl`, is used as a prefix for the redirect url automatically. This is useful for scenarios where a site isn't available from the domain root, so the redirects point to the correct path. If `site.url` is not set, only `site.baseurl` is used, if set.

**_Note_**: If you are hosting your Jekyll site on [GitHub Pages](https://pages.github.com/), and `site.url` is not set, the prefix is set to the pages domain name i.e. http://example.github.io/project or a custom CNAME.

### Redirect To

Expand Down
16 changes: 12 additions & 4 deletions lib/jekyll-redirect-from/redirector.rb
Expand Up @@ -71,7 +71,13 @@ def redirect_url(site, item)
end

def redirect_prefix(site)
config_github_url(site) || config_url(site)
config_url_and_baseurl(site) || config_github_url(site) || config_baseurl(site) || ""
end

def config_url_and_baseurl(site)
return nil unless config_url(site)

File.join(config_url(site), config_baseurl(site).to_s)
end

def config_github_url(site)
Expand All @@ -82,9 +88,11 @@ def config_github_url(site)
end

def config_url(site)
url = site.config.fetch('url', nil) || ""
baseurl = site.config.fetch('baseurl', nil) || ""
File.join url, baseurl
site.config.fetch('url', nil)
end

def config_baseurl(site)
site.config.fetch('baseurl', nil)
end
end
end
34 changes: 28 additions & 6 deletions spec/jekyll_redirect_from/redirector_spec.rb
Expand Up @@ -106,13 +106,17 @@
end

context "prefix" do
it "uses site.url as the redirect prefix when site.github.url is not set" do
@site.config['url'] = "http://notgithub.io"
@site.config['baseurl'] = nil
expect(redirector.redirect_url(@site, page_with_one)).to eql("http://notgithub.io/one_redirect_url.html")
it "uses site.url as the redirect prefix" do
expect(redirector.redirect_url(@site, page_with_one)).to eql("http://jekyllrb.com/one_redirect_url.html")
end

it "uses site.baseurl as the redirect prefix when site.github.url is not set" do
it "uses site.github.url as the redirect prefix when site.url is not set" do
@site.config['url'] = nil
@site.config['github'] = { "url" => "http://example.github.io/test" }
expect(redirector.redirect_url(@site, page_with_one)).to eql("http://example.github.io/test/one_redirect_url.html")
end

it "uses site.baseurl as the redirect prefix when both site.url and site.github.url is not set" do
@site.config['url'] = nil
@site.config['baseurl'] = "/fancy/prefix"
expect(redirector.redirect_url(@site, page_with_one)).to eql("/fancy/prefix/one_redirect_url.html")
Expand All @@ -124,14 +128,22 @@
expect(redirector.redirect_url(@site, page_with_one)).to eql("http://notgithub.io/fancy/prefix/one_redirect_url.html")
end

it "prefers site.github.url over site.url or site.baseurl" do
it "prefers site.url + site.baseurl over site.github.url" do
@site.config['url'] = "http://notgithub.io"
@site.config['baseurl'] = "/fancy/prefix"
@site.config['github'] = { "url" => "http://example.github.io/test" }
expect(redirector.redirect_url(@site, page_with_one)).to eql("http://notgithub.io/fancy/prefix/one_redirect_url.html")
end

it "prefers site.github.url over site.baseurl when site.url is not set" do
@site.config['url'] = nil
@site.config['github'] = { "url" => "http://example.github.io/test" }
@site.config['baseurl'] = "/fancy/baseurl"
expect(redirector.redirect_url(@site, page_with_one)).to eql("http://example.github.io/test/one_redirect_url.html")
end

it "converts non-string values in site.github.url to strings" do
@site.config['url'] = nil
@site.config['github'] = { "url" => TestStringContainer.new("http://example.github.io/test") }
expect(redirector.redirect_url(@site, page_with_one)).to eql("http://example.github.io/test/one_redirect_url.html")
end
Expand All @@ -145,5 +157,15 @@
@site.config['url'] = nil
expect(redirector.redirect_url(@site, page_with_one)).to eql("/one_redirect_url.html")
end

it "should handle site.baseurl being nil" do
@site.config['baseurl'] = nil
expect(redirector.redirect_url(@site, page_with_one)).to eql("http://jekyllrb.com/one_redirect_url.html")
end

it "no-ops when site.url is empty" do
@site.config['url'] = ""
expect(redirector.redirect_url(@site, page_with_one)).to eql("/one_redirect_url.html")
end
end
end

0 comments on commit 973f4b9

Please sign in to comment.