Skip to content

Instantly share code, notes, and snippets.

@RyanCCollins
Forked from somic/category_plugin.rb
Created October 21, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanCCollins/e8cf9c14a02ea9cec51c to your computer and use it in GitHub Desktop.
Save RyanCCollins/e8cf9c14a02ea9cec51c 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 %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment