8000 Add absolute_url and relative_url filters. by parkr · Pull Request #5399 · jekyll/jekyll · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Merged
merged 3 commits into from
Sep 23, 2016

Conversation

parkr
Copy link
Member
@parkr parkr commented Sep 22, 2016

When creating sites and themes, there's a lot of confusion around site.url and site.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 with site.baseurl properly prepended. This is useful when you want to be host-agnostic.
  • absolute_url - takes a URL and returns a url with site.baseurl and site.url properly appended.

Examples, assuming site.baseurl = "/project" and site.url = "http://example.com":

{{ "/css/main.scss" | relative_url }}
<!-- "/project/css/main.scss" -->

{{ "/css/main.scss" | absolute_url }}
<!-- "http://example.com/project/css/main.scss" -->

If your site.baseurl or site.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

@parkr parkr added the feature label Sep 22, 2016
@parkr parkr added this to the 3.3 milestone Sep 22, 2016
@parkr parkr assigned pathawks and unassigned parkr Sep 22, 2016
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
Copy link
Contributor

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?

Copy link
Member Author

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?("/")
Copy link
Contributor

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?

@parkr parkr mentioned this pull request Sep 22, 2016
9 tasks
@parkr
Copy link
Member Author
parkr commented Sep 22, 2016

@benbalter Would appreciate another review when you have a minute.

Copy link
Member
@pathawks pathawks left a 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"
Copy link
Member

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)
Copy link
Member
@pathawks pathawks Sep 23, 2016

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in fa96843.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@parkr
Copy link
Member Author
parkr commented Sep 23, 2016

@jekyllbot: merge +minor

@jekyllbot jekyllbot merged commit 8197e17 into master Sep 23, 2016
@jekyllbot jekyllbot deleted the relative_url_and_absolute_url branch September 23, 2016 21:18
jekyllbot added a commit that referenced this pull request Sep 23, 2016
@parkr parkr restored the relative_url_and_absolute_url branch September 23, 2016 21:18
@pathawks pathawks mentioned this pull request Oct 25, 2016
4 tasks
@jekyll jekyll locked and limited conversation to collaborators Jul 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0