diff --git a/features/permalinks.feature b/features/permalinks.feature index bc13de88..74f2e409 100644 --- a/features/permalinks.feature +++ b/features/permalinks.feature @@ -96,3 +96,27 @@ Feature: Fancy permalinks Then the _site directory should exist And the _site/custom/posts directory should exist And I should see "bla bla" in "_site/custom/posts/some.html" + + Scenario: Use pretty permalink schema with cased file name + Given I have a _posts directory + And I have an "_posts/2009-03-27-Pretty-Permalink-Schema.md" page that contains "Totally wordpress" + And I have a configuration file with "permalink" set to "pretty" + When I run jekyll build + Then the _site directory should exist + And I should see "Totally wordpress." in "_site/2009/03/27/Pretty-Permalink-Schema/index.html" + + Scenario: Use custom permalink schema with cased file name + Given I have a _posts directory + And I have an "_posts/2009-03-27-Custom-Schema.md" page with title "Custom Schema" that contains "Totally awesome" + And I have a configuration file with "permalink" set to "/:year/:month/:day/:slug/" + When I run jekyll build + Then the _site directory should exist + And I should see "Totally awesome" in "_site/2009/03/27/custom-schema/index.html" + + Scenario: Use pretty permalink schema with title containing underscore + Given I have a _posts directory + And I have an "_posts/2009-03-27-Custom_Schema.md" page with title "Custom Schema" that contains "Totally awesome" + And I have a configuration file with "permalink" set to "pretty" + When I run jekyll build + Then the _site directory should exist + And I should see "Totally awesome" in "_site/2009/03/27/Custom_Schema/index.html" diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 5af8d610..528fed68 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -186,7 +186,9 @@ module Jekyll path: cleaned_relative_path, output_ext: output_ext, name: Utils.slugify(basename_without_ext), - title: Utils.slugify(data['slug']) || Utils.slugify(basename_without_ext), + title: Utils.slugify(data['slug'], mode: "pretty", cased: true) || Utils + .slugify(basename_without_ext, mode: "pretty", cased: true), + slug: Utils.slugify(data['slug']) || Utils.slugify(basename_without_ext), year: date.strftime("%Y"), month: date.strftime("%m"), day: date.strftime("%d"), diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 72c332ad..7e2d30f3 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -45,7 +45,7 @@ module Jekyll # Returns the given filename or title as a lowercase URL String. # See Utils.slugify for more detail. def slugify(input, mode=nil) - Utils.slugify(input, mode) + Utils.slugify(input, mode: mode) end # Format a date in short format e.g. "27 Jan 2011". diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index c30a986a..7d2490a6 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -121,10 +121,12 @@ module Jekyll # # 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 in lowercase. + # When mode is "none", return the given string. # - # When mode is "raw", return the given string in lowercase, + # 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 @@ -133,6 +135,9 @@ module Jekyll # 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" @@ -140,11 +145,17 @@ module Jekyll # 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. - def slugify(string, mode=nil) + def slugify(string, mode: nil, cased: false) mode ||= 'default' return nil if string.nil? - return string.downcase unless SLUGIFY_MODES.include?(mode) + + unless SLUGIFY_MODES.include?(mode) + return cased ? string : string.downcase + end # Replace each character sequence with a hyphen re = case mode @@ -158,13 +169,13 @@ module Jekyll SLUGIFY_PRETTY_REGEXP end - string. + slug = string. # Strip according to the mode gsub(re, '-'). # Remove leading/trailing hyphen - gsub(/^\-|\-$/i, ''). - # Downcase - downcase + gsub(/^\-|\-$/i, '') + + cased ? slug : slug.downcase end # Add an appropriate suffix to template so that it matches the specified diff --git a/site/_docs/permalinks.md b/site/_docs/permalinks.md index e82ecf6d..5aa0449a 100644 --- a/site/_docs/permalinks.md +++ b/site/_docs/permalinks.md @@ -109,8 +109,20 @@ permalink is defined according to the format `/:categories/:year/:month/:day/:ti
- Title from the document’s filename. May be overridden via the
- document’s slug
YAML front matter.
+ Title from the document’s filename. May be overridden via
+ the document’s slug
YAML front matter.
+
slug
+ Slugified title from the document’s filename ( any character
+ except numbers and letters is replaced as hyphen ). May be
+ overridden via the document’s slug
YAML front matter.