Skip to content

Commit

Permalink
Archives page with year links
Browse files Browse the repository at this point in the history
  • Loading branch information
janosrusiczki committed Oct 3, 2023
1 parent ae171e7 commit 0173783
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
34 changes: 34 additions & 0 deletions _plugins/year_utilities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# {% year_utilities %}
# Returns a list of links to all years that have posts in them
#
# To install, copy this file into your Jekyll/Octopress plugins folder
# To use: {% year_utilities PARAMETER %}
#
# PARAMETER can be:
# year_links (returns a list of links to all years that have posts in them)

module Jekyll
class YearUtilities < Liquid::Tag
def initialize(tag, text, tokens)
super
@tag = tag
@text = text.strip
@tokens = tokens
end

def render(context)
the_result = nil

all_posts = context.registers[:site].posts.docs
years = all_posts.map { |post| post.date.year }.uniq

if @text.start_with? "year_links"
years.map { |year| "<a href='/#{year}/' class='year-link'>#{year}</a>" }.join(', ')
end

return the_result
end
end
end

Liquid::Template.register_tag('year_utilities', Jekyll::YearUtilities)
7 changes: 7 additions & 0 deletions archives.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
layout: page
title: Archives
---

<h2>Yearly archives</h2>
{% year_utilities year_links %}

0 comments on commit 0173783

Please sign in to comment.