Skip to content

Commit

Permalink
feat: use generator for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed May 20, 2024
1 parent b9c0e9b commit ec48a1b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
6 changes: 6 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ exclude:
- vendor/cache/
- vendor/gems/
- vendor/ruby/
- docker-compose.yml
- package.json
- package-lock.json
- tailwind.config.js
- postcss.config.js
- LICENSE

permalink: /:year/:month/:title

Expand Down
6 changes: 2 additions & 4 deletions _layouts/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ <h1 class="font-light text-3xl border-b border-gray-200 mb-2">{{ page.title }}</
</div>

<div class="grid grid-cols-2 gap-4">
{% for post in site.posts %}
{% if post.tags contains page.tag-name %}
{% include post.html post=post %}
{% endif %}
{% for post in page.tagged_posts %}
{% include post.html post=post %}
{% endfor %}
</div>
40 changes: 24 additions & 16 deletions _plugins/tags.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
Jekyll::Hooks.register :posts, :post_write do |post|
all_existing_tags = Dir.entries("tag")
.map { |t| t.match(/(.*).md/) }
.compact.map { |m| m[1] }
module TagsPlugin
class TagPageGenerator < Jekyll::Generator
safe true

tags = post['tags'].reject { |t| t.empty? }
tags.each do |tag|
generate_tag_file(tag) if !all_existing_tags.include?(tag)
def generate(site)
site.tags.each do |tag|
site.pages << TagPage.new(site, tag[0], tag[1])
end
end
end
end

def generate_tag_file(tag)
# generate tag file
File.open("tag/#{tag}.md", "wb") do |file|
file << "---\nlayout: tags\ntag-name: #{tag}\nslug: /tag/#{tag.downcase}\ntitle: Posts Tagged #{tag}\n---\n"
class TagPage < Jekyll::Page
def initialize(site, tag, posts)
@site = site
@base = site.source
@dir = "tag/#{tag}"

@basename = 'index'
@ext = ".html"
@name = 'index.html'

@data = {
'layout' => 'tags',
'tagged_posts' => posts,
'title' => "Posts Tagged #{tag}"
}
end
end
# generate feed file
#File.open("feeds/#{tag}.xml", "wb") do |file|
#file << "---\nlayout: feed\ntag-name: #{tag}\n---\n"
#end
end
1 change: 1 addition & 0 deletions blog.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: default
title: Blog
permalink: /blog/
---
<div class="grid grid-cols-2 gap-2 mb-2">
{% for post in site.posts %}
Expand Down
Empty file removed tag/.gitkeep
Empty file.

0 comments on commit ec48a1b

Please sign in to comment.