secret
Last active

Tiered Archives plugin

  • Download Gist
tiered_archives.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# 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

I cannot output post date as day of month; like here http://mnmlist.com/

Can you help me?

I Found solution:

{% for year in site.years %}
    <h2>{{ year.first.first.date | date: "%Y" }}</h2>
    {% for month in year %}
        <h3>{{ month.first.date | date: "%B" }}</h3>
        <dl>
        {% for post in month %}
            <dt>{{post.date | date: "%d"}}</dt>         
            <dd>
                <a href="{{ post.url}}">{{ post.title }}</a>
            </dd>
        {% endfor %}
        </dl>
    {% endfor %}
{% endfor %}

How can I sort the months? In my website, the output of the original suggested code is:

2011
 Maio 
 Novembro
 Junho
 Dezembro
 Fevereiro
 Agosto
 Março
 Setembro
 Abril
 Outubro
2012
 Maio 
 Novembro
 Junho
 Janeiro
 Julho 
 Fevereiro 
 Agosto
 Março
 Setembro 
 Abril 
 Outubro

I am using

        <ul class="list-type2">
          {% for year in site.years %}                                                                                    
          <li>{{ year.first.first.date | date: "%Y" }}
        <ul>
          {% for month in year %}                                                                                       
          <li><a href="/{{ year.first.first.date | localize: '%Y'}}/{{ month.first.date | localize: '%m'}}/">
              {{ month.first.date | localize: "%B" }}</a></li>
          {% endfor %}                                                                                                
        </ul>
              </li>
          {% endfor %}
        </ul>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.