From a401f0387e2a12920f77fbaf43213ccccc028633 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Fri, 18 Sep 2020 16:53:08 +0530 Subject: [PATCH] Reduce string allocations from generating doc URLs (#8392) Merge pull request 8392 --- lib/jekyll/url.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index 6b812a2e..3c4a0091 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -94,7 +94,8 @@ module Jekyll def generate_url_from_drop(template) 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) } if winner.nil? @@ -107,15 +108,17 @@ module Jekyll value = "" if value.nil? replacement = self.class.escape_path(value) - match.sub(":#{winner}", replacement) - end.squeeze("/") + match.sub!(":#{winner}", replacement) + end end # Returns a sanitized String URL, stripping "../../" and multiples of "/", # as well as the beginning "/" so we can enforce and ensure it. - def sanitize_url(str) - "/#{str}".gsub("..", "/").gsub("./", "").squeeze("/") + "/#{str}".gsub("..", "/").tap do |result| + result.gsub!("./", "") + result.squeeze!("/") + end end # Escapes a path to be a valid URL path segment