-
-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Add absolute_url and relative_url filters. #5399
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Jekyll | ||
module Filters | ||
module URLFilters | ||
# Produces an absolute URL based on site.url and site.baseurl. | ||
# | ||
# input - the URL to make absolute. | ||
# | ||
# Returns the absolute URL as a String. | ||
def absolute_url(input) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
|
||
# Produces a URL relative to the domain root based on site.baseurl. | ||
# | ||
# input - the URL to make relative to the domain root | ||
# | ||
# Returns a URL relative to the domain root as a String. | ||
def relative_url(input) | ||
return if input.nil? | ||
site = @context.registers[:site] | ||
return ensure_leading_slash(input.to_s) if site.config["baseurl"].nil? | ||
URI( | ||
ensure_leading_slash(site.config["baseurl"]) + ensure_leading_slash(input.to_s) | ||
).to_s | ||
end | ||
|
||
private | ||
def ensure_leading_slash(input) | ||
return input if input.nil? || input.empty? || input.start_with?("/") | ||
"/#{input}" | ||
end | ||
|
||
end | ||
end | ||
end |
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.
❤️