filter relative_url should keep absolute urls with scheme/authority (#6490)

Merge pull request 6490
This commit is contained in:
Johannes Müller 2017-11-05 16:17:51 +01:00 committed by jekyllbot
parent beed5513e4
commit a66c4780cc
2 changed files with 19 additions and 1 deletions

View File

@ -20,13 +20,16 @@ module Jekyll
).normalize.to_s
end
# Produces a URL relative to the domain root based on site.baseurl.
# Produces a URL relative to the domain root based on site.baseurl
# unless it is already an absolute url with an authority (host).
#
# 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?
return input if Addressable::URI.parse(input.to_s).absolute?
parts = [sanitized_baseurl, input]
Addressable::URI.parse(
parts.compact.map { |part| ensure_leading_slash(part.to_s) }.join

View File

@ -526,6 +526,21 @@ class TestFilters < JekyllUnitTest
filter = make_filter_mock({ "baseurl" => Value.new(proc { "/baseurl/" }) })
assert_equal "/baseurl#{page_url}", filter.relative_url(page_url)
end
should "transform protocol-relative url" do
url = "//example.com/"
assert_equal "/base//example.com/", @filter.relative_url(url)
end
should "not modify an absolute url with scheme" do
url = "file:///file.html"
assert_equal url, @filter.relative_url(url)
end
should "not normalize absolute international URLs" do
url = "https://example.com/错误"
assert_equal "https://example.com/错误", @filter.relative_url(url)
end
end
context "strip_index filter" do