-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Add absolute_url and relative_url filters. #5399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
return if input.nil? | ||
site = @context.registers[:site] | ||
return relative_url(input).to_s if site.config["url"].nil? | ||
URI(site.config["url"] + relative_url(input)).to_s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be URI.join
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URI.join
causes more problems than it solves. relative_url
will always start with a /
so this is safe. An example of what it breaks is if site.url = "example.com"
:
Error:
TestFilters#test_: filters absolute_url filter should be ok with a blank but present 'url'. :
URI::BadURIError: both URI are relative
/Users/parkr/.rbenv/versions/2.3.1/lib/ruby/2.3.0/uri/generic.rb:1102:in `rescue in merge'
/Users/parkr/.rbenv/versions/2.3.1/lib/ruby/2.3.0/uri/generic.rb:1099:in `merge'
/Users/parkr/.rbenv/versions/2.3.1/lib/ruby/2.3.0/uri/rfc3986_parser.rb:89:in `each'
/Users/parkr/.rbenv/versions/2.3.1/lib/ruby/2.3.0/uri/rfc3986_parser.rb:89:in `inject'
/Users/parkr/.rbenv/versions/2.3.1/lib/ruby/2.3.0/uri/rfc3986_parser.rb:89:in `join'
/Users/parkr/.rbenv/versions/2.3.1/lib/ruby/2.3.0/uri/common.rb:265:in `join'
/Users/parkr/jekyll/jekyll/lib/jekyll/filters/url_filters.rb:13:in `absolute_url'
/Users/parkr/jekyll/jekyll/test/test_filters.rb:341:in `block (3 levels) in <class:TestFilters>'
/Users/parkr/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/shoulda-context-1.2.1/lib/shoulda/context/context.rb:413:in `instance_exec'
/Users/parkr/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/shoulda-context-1.2.1/lib/shoulda/context/context.rb:413:in `block in create_test_from_should_hash'
private | ||
def ensure_leading_slash(input) | ||
return input if input.nil? || input.empty? | ||
if input.start_with?("/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this to the line above to make this a two line method?
@benbalter Would appreciate another review when you have a minute. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love the idea of breaking out filters by categories instead of just putting everything in filters.rb
.
Just one question about relative_url
@@ -3,8 +3,11 @@ | |||
require "date" | |||
require "liquid" | |||
|
|||
require_all "jekyll/filters" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
return ensure_leading_slash(input.to_s) if site.config["baseurl"].nil? | ||
ensure_leading_slash( # in case the baseurl doesn't have a leading slash | ||
URI( | ||
site.config["baseurl"] + ensure_leading_slash(input.to_s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
URI(
ensure_leading_slash(site.config["baseurl"]) + ensure_leading_slash(input.to_s)
).to_s
This seems more clear to me, but I may be missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in fa96843.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
@jekyllbot: merge +minor |
When creating sites and themes, there's a lot of confusion around
site.url
andsite.baseurl
. These new filters attempt to unify the way we calculate relative and absolute URL's. The new filters are:relative_url
- takes a URL and returns a url withsite.baseurl
properly prepended. This is useful when you want to be host-agnostic.absolute_url
- takes a URL and returns a url withsite.baseurl
andsite.url
properly appended.Examples, assuming
site.baseurl = "/project"
andsite.url = "http://example.com"
:If your
site.baseurl
orsite.url
values aren't set, it will just append what it can dependeing on the desired output of the filter. Read the tests in the diff for more on this./cc @jekyll/ecosystem @jekyll/core @jekyll/stability