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 # sequence of spaces and non-alphanumeric characters replaced with a
# hyphen. # hyphen.
def slugify(string) def slugify(string)
if string.nil? unless string.nil?
nil # Replace each non-alphanumeric character sequence with a hyphen
else slug = string.gsub(/[^a-z0-9]+/i, '-')
string.downcase.gsub(/[^\w]/, " ").strip.gsub(/\s+/, '-') # Remove leading/trailing hyphen
slug.gsub!(/^\-|\-$/i, '')
slug.downcase
end end
end end