Add strip_index filter (#6075)

Merge pull request 6075
This commit is contained in:
Ben Balter 2017-06-15 14:28:41 -04:00 committed by jekyllbot
parent 401e20cfa6
commit 69e97fa06f
2 changed files with 34 additions and 2 deletions

View File

@ -28,6 +28,16 @@ module Jekyll
).normalize.to_s ).normalize.to_s
end end
# Strips trailing `/index.html` from URLs to create pretty permalinks
#
# input - the URL with a possible `/index.html`
#
# Returns a URL with the trailing `/index.html` removed
def strip_index(input)
return if input.nil? || input.to_s.empty?
input.sub(%r!/index\.html?$!, "/")
end
private private
def site def site

View File

@ -493,6 +493,28 @@ class TestFilters < JekyllUnitTest
end end
end end
context "strip_index filter" do
should "strip trailing /index.html" do
assert_equal "/foo/", @filter.strip_index("/foo/index.html")
end
should "strip trailing /index.htm" do
assert_equal "/foo/", @filter.strip_index("/foo/index.htm")
end
should "not strip HTML in the middle of URLs" do
assert_equal "/index.html/foo", @filter.strip_index("/index.html/foo")
end
should "not raise an error on nil strings" do
assert_nil @filter.strip_index(nil)
end
should "not mangle other URLs" do
assert_equal "/foo/", @filter.strip_index("/foo/")
end
end
context "jsonify filter" do context "jsonify filter" do
should "convert hash to json" do should "convert hash to json" do
assert_equal "{\"age\":18}", @filter.jsonify({ :age => 18 }) assert_equal "{\"age\":18}", @filter.jsonify({ :age => 18 })
@ -742,7 +764,7 @@ class TestFilters < JekyllUnitTest
assert_equal 4.7, results[0]["rating"] assert_equal 4.7, results[0]["rating"]
end end
should "always return an array if the object responds to `select`" do should "always return an array if the object responds to 'select'" do
results = @filter.where(SelectDummy.new, "obj", "1 == 1") results = @filter.where(SelectDummy.new, "obj", "1 == 1")
assert_equal [], results assert_equal [], results
end end
@ -819,7 +841,7 @@ class TestFilters < JekyllUnitTest
assert_equal site.posts.find { |p| p.title == "Foo Bar" }, results.first assert_equal site.posts.find { |p| p.title == "Foo Bar" }, results.first
end end
should "always return an array if the object responds to `select`" do should "always return an array if the object responds to 'select'" do
results = @filter.where_exp(SelectDummy.new, "obj", "1 == 1") results = @filter.where_exp(SelectDummy.new, "obj", "1 == 1")
assert_equal [], results assert_equal [], results
end end