Add tags filter to blog posts

This commit is contained in:
ajuvercr 2020-10-12 19:40:15 +02:00
parent fab45a4e57
commit e72014b40f
8 changed files with 79 additions and 9 deletions

2
Rules
View file

@ -24,6 +24,8 @@ preprocess do
update_blog_attributes
create_yearly_items('Blog')
create_yearly_items('Events')
create_tagly_items('Blog')
create_tagly_items('Events')
convert_event_time_to_timestamps
all_events.each do |event|

View file

@ -3,6 +3,8 @@ title: "Zeus en Corona"
created_at: 11-03-2020
description: Corona en de invloed ervan op Zeus WPI
author: Robbe Van Herck
tags:
- Corona
---
Beste Zeussers en Zeusinnen,

View file

@ -4,6 +4,8 @@ title: "Het vervolg op het avontuur van de Vleor"
created_at: 07-07-2020
description: Eindelijk, de Zeuskelder heeft weer een nieuwe vloer!
toc: true
tags:
- vloer
---
**vleor** (_de; m;_ meervoud: _vleoren_)

View file

@ -3,6 +3,8 @@ title: "Zeusbestuur 2020-2021"
created_at: 6-7-2020
description: "Het gloriZeuse nieuw bestuur"
author: "Robbe Van Herck"
tags:
- Bestuur
---
> Hallo mijn naam is Jasper en ik zou nog steeds sysadmin willen blijven
@ -25,4 +27,3 @@ Voor iedereen die niet op de verkiezingen was of de uitslag vergeten is, hier ee
| Evenementen | Ruben De Facq |
Veel succes en veel plezier van jullie oud bestuur. Het gaat gegarandeerd onvergetelijk worden! 🧡

View file

@ -3,6 +3,9 @@ title: "Wij coden voort"
created_at: 21-7-2020
description: "We mogen de kelder opnieuw openen"
author: "Jasper Devreker"
tags:
- Corona
- General update
---
We hebben via het WVK[^wvk] doorgekregen dat we onze geliefde kelder terug mogen openen.

View file

@ -19,24 +19,41 @@
<% end %>
<% end %>
</ul>
<p class="menu-label">
Tag
</p>
<ul class="menu-list">
<% tag_blog_items.each do |tag, item| %>
<% if @item_rep && @item_rep.path == item.path %>
<li>
<a href="#" class="is-active">
<%= tag %>
</a>
</li>
<% else %>
<li>
<%= link_to tag, item %>
</li>
<% end %>
<% end %>
</ul>
</aside>
</div>
<div class="column">
<div class="columns is-multiline">
<div class="column">
<% posts_in_year(item[:academic_year]).each_with_index.select {|e, i| i.even?}.map(&:first).each do |post| %>
<% posts_in_year_or_with_tag(item).each_with_index.select {|e, i| i.even?}.map(&:first).each do |post| %>
<%= render '/partials/_blog_preview.*', post: post %>
<% end %>
</div>
<div class="column">
<% posts_in_year(item[:academic_year]).each_with_index.reject {|e, i| i.even?}.map(&:first).each do |post| %>
<% posts_in_year_or_with_tag(item).each_with_index.reject {|e, i| i.even?}.map(&:first).each do |post| %>
<%= render '/partials/_blog_preview.*', post: post %>
<% end %>
</div>
</div>
<% if posts_in_year(@item[:academic_year]).empty? %>
<% if posts_in_year_or_with_tag(item).empty? %>
<div class="content has-text-centered">
<h1>Sorry.</h1>
<h2>No blog posts yet this year.</h2>

View file

@ -3,8 +3,8 @@ module ArchiveHelper
# Set.to_a to prevent duplicates
Set.new(items
.find_all('/blog/*/*')
.map { |i| i.identifier.to_s[/\d\d-\d\d/] })
.to_a
.map { |i| i.identifier.to_s.split('/')[-2] })
.to_a
.sort
.push(@config[:academic_year])
.uniq
@ -14,6 +14,20 @@ module ArchiveHelper
academic_years.reverse.map { |y| [y, items["/blog/#{y}.html"]] }
end
def tags
# Set.to_a to prevent duplicates
Set.new(items
.find_all('/blog/*/*')
.flat_map { |i| i[:tags] || [] })
.to_a
.sort
.uniq
end
def tag_blog_items
tags.map { |y| [y, items["/blog/#{y}.html"]]}
end
def pretty_year(year)
year = year.scan(/\d\d/)
"'#{year[0]} - '#{year[1]}"
@ -22,4 +36,18 @@ module ArchiveHelper
def posts_in_year(y)
items.find_all("/blog/#{y}/*").sort_by { |x| x[:created_at] }.reverse
end
def posts_with_tag(tag)
items
.find_all('/blog/*/*')
.filter{|i| (i[:tags] || []).include? tag }
end
def posts_in_year_or_with_tag(item)
if item[:is_yearly]
posts_in_year(item[:academic_year])
else
posts_with_tag(item[:tag])
end
end
end

View file

@ -56,7 +56,7 @@ module PreprocessHelper
i.update_attributes(
# Tag all posts with article (for Blogging helper)
kind: 'article',
academic_year: i.identifier.to_s[/\d\d-\d\d/],
academic_year: i.identifier.to_s[/\d-\d/],
created_at: Date.parse(i[:created_at])
)
end
@ -72,7 +72,7 @@ module PreprocessHelper
years.each do |year|
@items.create(
'',
{ academic_year: year, title: type },
{ academic_year: year, title: type, is_yearly: true },
"/#{type.downcase}/#{year}.html"
)
end
@ -84,6 +84,21 @@ module PreprocessHelper
)
end
def create_tagly_items(type)
type = type.to_s
tags = @items.find_all("/#{type.downcase}/*/*")
.flat_map { |i| i[:tags] || [] }
.uniq
tags.each do |tag|
@items.create(
'',
{ tag: tag, title: type, is_yearly: false },
"/#{type.downcase}/#{tag}.html"
)
end
end
def convert_event_time_to_timestamps
@items.find_all('/events/*/*.md').each do |event|
# HACK: Strings in a format like "2017-10-05T20:45:00+0200" automatically get converted to Time