diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 2664a01f..bcdf80ff 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -47,6 +47,17 @@ module Jekyll converter.convert(input) end + # Slugify a filename or title. + # + # input - The filename or title to slugify. + # + # Returns the given filename or title as a lowercase String, with every + # sequence of spaces and non-alphanumeric characters replaced with a + # hyphen. + def slugify(input) + Utils.slugify(input) + end + # Format a date in short format e.g. "27 Jan 2011". # # date - the Time to format. diff --git a/site/_docs/templates.md b/site/_docs/templates.md index 5ace04fc..84655db8 100644 --- a/site/_docs/templates.md +++ b/site/_docs/templates.md @@ -211,6 +211,17 @@ common tasks easier.

+ + +

Slugify

+

Convert a string into a lowercase URL "slug" by replacing every sequence of spaces and non-alphanumeric characters with a hyphen.

+ + +

+ {% raw %}{{ page.title | slugify }}{% endraw %} +

+ +

Data To JSON

diff --git a/test/test_filters.rb b/test/test_filters.rb index 58841fe8..e4f1adf3 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -217,5 +217,11 @@ class TestFilters < Test::Unit::TestCase end end + context "slugify filter" do + should "return a slugified string" do + assert_equal "q-bert-says", @filter.slugify(" Q*bert says @!#?@!") + end + end + end end