Reduce string allocations from generating doc URLs (#8392)

Merge pull request 8392
This commit is contained in:
Ashwin Maroli 2020-09-18 16:53:08 +05:30 committed by GitHub
parent 07e1eb1f27
commit a401f0387e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 5 deletions

View File

@ -94,7 +94,8 @@ module Jekyll
def generate_url_from_drop(template) def generate_url_from_drop(template)
template.gsub(%r!:([a-z_]+)!) do |match| template.gsub(%r!:([a-z_]+)!) do |match|
pool = possible_keys(match.sub(":", "")) name = Regexp.last_match(1)
pool = name.end_with?("_") ? [name, name.chomp!("_")] : [name]
winner = pool.find { |key| @placeholders.key?(key) } winner = pool.find { |key| @placeholders.key?(key) }
if winner.nil? if winner.nil?
@ -107,15 +108,17 @@ module Jekyll
value = "" if value.nil? value = "" if value.nil?
replacement = self.class.escape_path(value) replacement = self.class.escape_path(value)
match.sub(":#{winner}", replacement) match.sub!(":#{winner}", replacement)
end.squeeze("/") end
end end
# Returns a sanitized String URL, stripping "../../" and multiples of "/", # Returns a sanitized String URL, stripping "../../" and multiples of "/",
# as well as the beginning "/" so we can enforce and ensure it. # as well as the beginning "/" so we can enforce and ensure it.
def sanitize_url(str) def sanitize_url(str)
"/#{str}".gsub("..", "/").gsub("./", "").squeeze("/") "/#{str}".gsub("..", "/").tap do |result|
result.gsub!("./", "")
result.squeeze!("/")
end
end end
# Escapes a path to be a valid URL path segment # Escapes a path to be a valid URL path segment