Last active
May 6, 2018 11:48
-
-
Save li2/894008f0f07acfc2eb2564270e07d5ad to your computer and use it in GitHub Desktop.
[How I organize posts in Jekyll (code snippets for blog post - see comments)]
#tags: tool
This file contains hidden or 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
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 | |
This file contains hidden or 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
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 | |
This file contains hidden or 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
{% 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