Skip to content

Instantly share code, notes, and snippets.

@becker1701
Created November 19, 2015 18:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save becker1701/fd575b5f471b410568fe to your computer and use it in GitHub Desktop.
Save becker1701/fd575b5f471b410568fe to your computer and use it in GitHub Desktop.
Category slugify plugin for Jekyll 3.0.0: This plugin will slugify the categories listed in the YML frontmatter. Categories with multiple words will have the spaces replaced with dashes. Put this file in the _plugins directory.
module Jekyll
class Document
#Updated for Jekyll 3.0.0
# override post method in order to return categories names as slug
# instead of strings
#
# An url for a post with category "category with space" will be in
# slugified form : /category-with-space
# instead of url encoded form : /category%20with%20space
#
# @see utils.slugify
def url_placeholders
{
collection: collection.label,
path: cleaned_relative_path,
output_ext: output_ext,
name: Utils.slugify(basename_without_ext),
title: Utils.slugify(data['slug']) || Utils.slugify(basename_without_ext),
year: date.strftime("%Y"),
month: date.strftime("%m"),
day: date.strftime("%d"),
hour: date.strftime("%H"),
minute: date.strftime("%M"),
second: date.strftime("%S"),
i_day: date.strftime("%-d"),
i_month: date.strftime("%-m"),
categories: (data['categories'] || []).map { |c| Utils.slugify(c) }.uniq.join('/'),
short_month: date.strftime("%b"),
short_year: date.strftime("%y"),
y_day: date.strftime("%j"),
}
end
end
end
@marijnkortstra
Copy link

You are amazing. Thank you so much for this update!

@blogtips
Copy link

blogtips commented Apr 1, 2017

I have problem with chinese characters in category url. Can you show me how to strip them?
example: when i create a category in a post like 'abc-糯米'
->by default: mysite/catgory/abc-糯米
->expect: mysite/category/abc
Thanks

@erata
Copy link

erata commented Feb 25, 2018

hi, i tried post.rb but can not run it. i am newly at jekyll. i add post.rb under _plugins directory at master and wait to action but nothing occured. what i missed, could you tell me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment