Revise Utils#slugify with inspiration from Rails

This commit is contained in:
Chris Frederick 2014-09-02 14:32:14 +09:00
parent 702b39dec5
commit 4adc35aaf3
1 changed files with 6 additions and 4 deletions

View File

@ -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