Skip to content

Commit

Permalink
Trying years with month links
Browse files Browse the repository at this point in the history
  • Loading branch information
janosrusiczki committed Oct 3, 2023
1 parent 2a74b04 commit 8da7b4d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions _plugins/year_utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# PARAMETER can be:
# year_links (returns a list of links to all years that have posts in them)
# years_with_month_links (returns a list of links to all years that have posts in them, with links to the months in each year)

module Jekyll
class YearUtilities < Liquid::Tag
Expand All @@ -20,19 +21,27 @@ def render(context)
the_result = nil

all_posts = context.registers[:site].posts.docs
years = all_posts.map { |post|
puts post.date.strftime("%Y")
post.date.strftime("%Y")
}.uniq
years = all_posts.map { |post| post.date.strftime("%Y") }.uniq

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

puts the_result
if @text.start_with? "years_with_month_links"
the_result = years.map do |year|
year_with_month_links = "<a href='/#{year}/' class='year-link'>#{year}</a> ("
year_with_month_links += months_in_year(all_posts, year).map { |month| "<a href='/#{year}/#{month}/' class='month-link'>#{month}</a>" }.join(', ')
year_with_month_links += ")"
return year_with_month_links
end.join(', ')
end

return the_result
end

def months_in_year(posts, year)
posts.select { |post| post.date.strftime("%Y") == year }.map { |post| post.date.strftime("%m") }.uniq
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion archives.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ title: Archives
---

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

0 comments on commit 8da7b4d

Please sign in to comment.