-
-
Save CodeZombie/533edc6d7708151ecc61 to your computer and use it in GitHub Desktop.
Ruby Slug Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def createSlug(title) | |
maxlength = 35 | |
title = title.gsub(' ', '-').gsub(/[^\w-]/, '').gsub(/(-){2,}/, '-').downcase | |
return title[0, title.length > maxlength ? (title[0, maxlength].rindex('-') or maxlength) : maxlength] | |
end |
it's enough return title[0, maxlength]
the sample:
2.4.1 :012 > "qwertyuiopasdfghjkl"[0,10]
# => "qwertyuiop"
2.4.1 :013 > "qwertyuiopasdfghjkl"[0,100]
# => "qwertyuiopasdfghjkl"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a little slug generator for ruby.
features
1 removes all non alphanumeric and dash characters
2 removes any repeating dashes
3 strips special characters, but does so in a way that slugs will always be maxlength if they can be