Created
September 5, 2011 21:49
-
-
Save enaeher/88cda643aa7e3b0ca1e5 to your computer and use it in GitHub Desktop.
Tiered Archives plugin
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
# A quick and dirty plugin for Jekyll by Eli Naeher | |
# | |
# This plugin creates a site.years template variable which allow you to group archive links by year and month. | |
# The structure of site.years is: | |
# site.years = 2001=>[[post1, post2...], [...]], 2002=>[...] | |
# | |
# Usage should look something like this: | |
# {% for year in site.years %} | |
# <h2>Year {{ year.first.first.date | date: "%Y" }}</h2> | |
# {% for month in year %} | |
# <h3>Month {{ month.first.date | date: "%B" }}</h3> | |
# {% for post in month %} | |
# <a href="{{ post.url">{{ post.title }}</a> | |
# {% endfor %} | |
# {% endfor %} | |
# {% endfor %} | |
class Jekyll::Site | |
alias :site_payload_without_tiered_archives :site_payload | |
def site_payload | |
data = site_payload_without_tiered_archives | |
data['site']['years'] = TieredArchives::find_years(self.posts.reverse) | |
data | |
end | |
end | |
module TieredArchives | |
def self.find_years(posts) | |
posts.group_by {|post| post.date.year}.values.map {|year| year.group_by {|post| post.date.month}.values}; | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I sort the months? In my website, the output of the original suggested code is:
I am using