-
-
Save fahrinh/1366833 to your computer and use it in GitHub Desktop.
A category list tag for jekyll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# place this file in your plugins directory and add the tag to your sidebar | |
#$ cat source/_includes/custom/asides/categories.html | |
#<section> | |
# <h1>Categories</h1> | |
# <ul id="categories"> | |
# {% category_list %} | |
# </ul> | |
#</section> | |
module Jekyll | |
class CategoryListTag < Liquid::Tag | |
def render(context) | |
html = "" | |
categories = context.registers[:site].categories.keys | |
categories.sort.each do |category| | |
posts_in_category = context.registers[:site].categories[category].size | |
category_dir = context.registers[:site].config['category_dir'] | |
category_url = File.join(category_dir, category.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase) | |
html << "<li class='category'><a href='/#{category_url}/'>#{category} (#{posts_in_category})</a></li>\n" | |
end | |
html | |
end | |
end | |
end | |
Liquid::Template.register_tag('category_list', Jekyll::CategoryListTag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It does not support non English category names, How can I change it to support other Unicode languages?