Skip to content
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 strip_index filter #6075

Merged
merged 5 commits into from Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/jekyll/filters/url_filters.rb
Expand Up @@ -30,6 +30,16 @@ def relative_url(input)
).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 ensure_leading_slash(input)
return input if input.nil? || input.empty? || input.start_with?("/")
Expand Down
26 changes: 24 additions & 2 deletions test/test_filters.rb
Expand Up @@ -450,6 +450,28 @@ def select; 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
should "convert hash to json" do
assert_equal "{\"age\":18}", @filter.jsonify({ :age => 18 })
Expand Down Expand Up @@ -699,7 +721,7 @@ def to_liquid
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
Expand Down Expand Up @@ -776,7 +798,7 @@ def to_liquid
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
Expand Down