Module: Jekyll::Utils

Defined in:
lib/jekyll/slugify_underscore.rb

Instance Method Summary collapse

Instance Method Details

#slugify(string, mode: nil, cased: false) ⇒ Object

Slugify a filename or title.

string - the filename or title to slugify mode - how string is slugified cased - whether to replace all uppercase letters with their lowercase counterparts

When mode is “none”, return the given string.

When mode is “raw”, return the given string, with every sequence of spaces characters replaced with a hyphen.

When mode is “default” or nil, non-alphabetic characters are replaced with a hyphen too.

When mode is “pretty”, some non-alphabetic characters (._~!$&‘()+,;=@) are not replaced with hyphen.

If cased is true, all uppercase letters in the result string are replaced with their lowercase counterparts.

Examples:

slugify("The _config.yml file")
# => "the-config-yml-file"

slugify("The _config.yml file", "pretty")
# => "the-_config.yml-file"

slugify("The _config.yml file", "pretty", true)
# => "The-_config.yml file"

Returns the slugified string.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll/slugify_underscore.rb', line 37

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

  # Replace each character sequence with a hyphen
  re = case mode
  when 'raw'
    SLUGIFY_RAW_REGEXP
  when 'default'
    SLUGIFY_DEFAULT_REGEXP
  when 'pretty'
    # "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
    # and is allowed in both extN and NTFS.
    SLUGIFY_PRETTY_REGEXP
  end

  slug = string.
    # Strip according to the mode
    gsub(re, '_').
    # Remove leading/trailing hyphen
    gsub(/^\_|\_$/i, '')

  cased ? slug : slug.downcase
end