Slightly speed up url sanitization and handle multiples of ///.
This commit is contained in:
parent
b01b089f69
commit
487d9ffc21
|
@ -70,20 +70,11 @@ module Jekyll
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a sanitized String URL
|
# Returns a sanitized String URL, stripping "../../" and multiples of "/",
|
||||||
def sanitize_url(in_url)
|
# as well as the beginning "/" so we can enforce and ensure it.
|
||||||
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')
|
|
||||||
|
|
||||||
# Append a trailing slash to the URL if the unsanitized URL had one
|
def sanitize_url(str)
|
||||||
url << "/" if in_url.end_with?("/")
|
"/" + str.gsub(/\/{2,}/, "/").gsub(%r!\.+\/|\A/+!, "")
|
||||||
|
|
||||||
url
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Escapes a path to be a valid URL path segment
|
# Escapes a path to be a valid URL path segment
|
||||||
|
|
Loading…
Reference in New Issue