Skip to content

Instantly share code, notes, and snippets.

@somic
Created March 2, 2011 23:37
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save somic/852008 to your computer and use it in GitHub Desktop.
Save somic/852008 to your computer and use it in GitHub Desktop.
How I organize posts in Jekyll (code snippets for blog post - see comments)
module Jekyll
Page.class_eval {
def clone
Page.new(@site, @base, @dir, @name)
end
}
class CategoryPageGenerator < Generator
safe true
priority :high
def generate(site)
main_cat_page = site.pages.select { |p| p.name == "category.html" }.first
site.categories.each do |cat|
cat_page = main_cat_page.clone
cat_name = cat.first.gsub(/\s+/, '-')
cat_page.data.merge!(
"title" => "somic.org category: #{cat_name}",
"permalink" => "/category/#{cat_name}/",
"category_name" => cat_name)
cat_page.render(site.layouts, site.site_payload)
site.pages << cat_page
end
end
end
end
module Jekyll
class SortedCategoriesBuilder < Generator
safe true
priority :high
def generate(site)
site.config['sorted_categories'] = site.categories.map { |cat, posts|
[ cat, posts.size, posts ] }.sort { |a,b| b[1] <=> a[1] }
end
end
end
{% for category in site.sorted_categories %}
here is a category name - {{ category | first }}
here is the number of posts in this category - {{ category | last | size }}
{% for post in category.last %}
posts in this category - {{ post.url }} {{ post.date | date_to_string }}
{% endfor %}
{% endfor %}
@somic
Copy link
Author

somic commented Mar 4, 2011

This gist contains code snippets for my blog post at http://www.somic.org/2011/03/04/how-i-organize-posts-in-jekyll/

@li2
Copy link

li2 commented Jan 24, 2016

It seems no key words site.sorted_categories in Jekyll now. I don't know how to make it works :(

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