Slightly speed up url sanitization and handle multiples of ///.

This commit is contained in:
Jordon Bedwell 2015-11-19 18:46:03 -06:00
parent b01b089f69
commit 487d9ffc21
1 changed files with 4 additions and 13 deletions

View File

@ -70,20 +70,11 @@ module Jekyll
end
end
# Returns a sanitized String URL
def sanitize_url(in_url)
url = in_url \
# Remove all double slashes
.gsub(/\/\//, '/') \
# Remove every URL segment that consists solely of dots
.split('/').reject{ |part| part =~ /^\.+$/ }.join('/') \
# Always add a leading slash
.gsub(/\A([^\/])/, '/\1')
# Returns a sanitized String URL, stripping "../../" and multiples of "/",
# as well as the beginning "/" so we can enforce and ensure it.
# Append a trailing slash to the URL if the unsanitized URL had one
url << "/" if in_url.end_with?("/")
url
def sanitize_url(str)
"/" + str.gsub(/\/{2,}/, "/").gsub(%r!\.+\/|\A/+!, "")
end
# Escapes a path to be a valid URL path segment