diff --git a/lib/jekyll/filters/url_filters.rb b/lib/jekyll/filters/url_filters.rb index 40cacc22..fa2dc16a 100644 --- a/lib/jekyll/filters/url_filters.rb +++ b/lib/jekyll/filters/url_filters.rb @@ -28,6 +28,16 @@ module Jekyll ).normalize.to_s 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 def site diff --git a/test/test_filters.rb b/test/test_filters.rb index e0af37a5..91ac386a 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -493,6 +493,28 @@ class TestFilters < JekyllUnitTest 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 should "convert hash to json" do assert_equal "{\"age\":18}", @filter.jsonify({ :age => 18 }) @@ -742,7 +764,7 @@ class TestFilters < JekyllUnitTest assert_equal 4.7, results[0]["rating"] 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") assert_equal [], results end @@ -819,7 +841,7 @@ class TestFilters < JekyllUnitTest assert_equal site.posts.find { |p| p.title == "Foo Bar" }, results.first 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") assert_equal [], results end