module Jekyll::Utils
def slugify(string, mode: nil, cased: false)
# => "the-config-yml-file"
slugify("The _config.yml file", "latin")
# => "the-config-yml-file"
slugify("The _config.yml file", "ascii")
# => "The-_config.yml file"
slugify("The _config.yml file", "pretty", true)
# => "the-_config.yml-file"
slugify("The _config.yml file", "pretty")
# => "the-config-yml-file"
slugify("The _config.yml file")
Examples:
replaced with their lowercase counterparts.
If cased is true, all uppercase letters in the result string are
it follows the "default" mode of operation.
any letters with accents are replaced with the plain letter. Afterwards,
When mode is "latin", the input string is first preprocessed so that
a-z (lowercase), A-Z (uppercase) and 0-9 (numbers) are not replaced with hyphen.
When mode is "ascii", some everything else except ASCII characters
are not replaced with hyphen.
When mode is "pretty", some non-alphabetic characters (._~!$&'()+,;=@)
replaced with a hyphen too.
When mode is "default" or nil, non-alphabetic characters are
with every sequence of spaces characters replaced with a hyphen.
When mode is "raw", return the given string,
When mode is "none", return the given string.
lowercase counterparts
cased - whether to replace all uppercase letters with their
mode - how string is slugified
string - the filename or title to slugify
Slugify a filename or title.
def slugify(string, mode: nil, cased: false) mode ||= "default" return nil if string.nil? unless SLUGIFY_MODES.include?(mode) return cased ? string : string.downcase end # Drop accent marks from latin characters. Everything else turns to ? if mode == "latin" I18n.config.available_locales = :en if I18n.config.available_locales.empty? string = I18n.transliterate(string) end slug = replace_character_sequence_with_hyphen(string, :mode => mode) # Remove leading/trailing hyphen slug.gsub!(%r!^-|-$!i, "") slug.downcase! unless cased Jekyll.logger.warn("Warning:", "Empty `slug` generated for '#{string}'.") if slug.empty? slug end