Allow URL filters to work directly with documents (#6478)

Merge pull request 6478
This commit is contained in:
Pat Hawks 2017-11-10 10:35:44 -06:00 committed by jekyllbot
parent b26dbaddd2
commit 582165897d
2 changed files with 19 additions and 0 deletions

View File

@ -12,6 +12,7 @@ module Jekyll
# Returns the absolute URL as a String. # Returns the absolute URL as a String.
def absolute_url(input) def absolute_url(input)
return if input.nil? return if input.nil?
input = input.url if input.respond_to?(:url)
return input if Addressable::URI.parse(input.to_s).absolute? return input if Addressable::URI.parse(input.to_s).absolute?
site = @context.registers[:site] site = @context.registers[:site]
return relative_url(input) if site.config["url"].nil? return relative_url(input) if site.config["url"].nil?
@ -28,6 +29,7 @@ module Jekyll
# Returns a URL relative to the domain root as a String. # Returns a URL relative to the domain root as a String.
def relative_url(input) def relative_url(input)
return if input.nil? return if input.nil?
input = input.url if input.respond_to?(:url)
return input if Addressable::URI.parse(input.to_s).absolute? return input if Addressable::URI.parse(input.to_s).absolute?
parts = [sanitized_baseurl, input] parts = [sanitized_baseurl, input]

View File

@ -443,6 +443,23 @@ class TestFilters < JekyllUnitTest
should "not raise a TypeError when passed a hash" do should "not raise a TypeError when passed a hash" do
assert @filter.absolute_url({ "foo" => "bar" }) assert @filter.absolute_url({ "foo" => "bar" })
end end
context "with a document" do
setup do
@site = fixture_site({
"collections" => ["methods"],
})
@site.process
@document = @site.collections["methods"].docs.detect do |d|
d.relative_path == "_methods/configuration.md"
end
end
should "make a url" do
expected = "http://example.com/base/methods/configuration.html"
assert_equal expected, @filter.absolute_url(@document)
end
end
end end
context "relative_url filter" do context "relative_url filter" do