From 4adc35aaf36ee30a7fef708950bbb2958d2a7681 Mon Sep 17 00:00:00 2001 From: Chris Frederick Date: Tue, 2 Sep 2014 14:32:14 +0900 Subject: [PATCH] Revise Utils#slugify with inspiration from Rails --- lib/jekyll/utils.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index 67fc6637..d528f22d 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -110,10 +110,12 @@ module Jekyll # sequence of spaces and non-alphanumeric characters replaced with a # hyphen. def slugify(string) - if string.nil? - nil - else - string.downcase.gsub(/[^\w]/, " ").strip.gsub(/\s+/, '-') + unless string.nil? + # Replace each non-alphanumeric character sequence with a hyphen + slug = string.gsub(/[^a-z0-9]+/i, '-') + # Remove leading/trailing hyphen + slug.gsub!(/^\-|\-$/i, '') + slug.downcase end end