From ec48a1bf48fa0ed1d4db367454e7c4a9b010cf0a Mon Sep 17 00:00:00 2001 From: Adam Laycock Date: Mon, 20 May 2024 20:08:53 +0100 Subject: [PATCH] feat: use generator for tags --- _config.yml | 6 ++++++ _layouts/tags.html | 6 ++---- _plugins/tags.rb | 40 ++++++++++++++++++++++++---------------- blog.html | 1 + tag/.gitkeep | 0 5 files changed, 33 insertions(+), 20 deletions(-) delete mode 100644 tag/.gitkeep diff --git a/_config.yml b/_config.yml index 010b5fe..6ffbb4f 100644 --- a/_config.yml +++ b/_config.yml @@ -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 diff --git a/_layouts/tags.html b/_layouts/tags.html index 291c2d5..249b2fd 100644 --- a/_layouts/tags.html +++ b/_layouts/tags.html @@ -6,9 +6,7 @@

{{ page.title }}
- {% 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 %}
\ No newline at end of file diff --git a/_plugins/tags.rb b/_plugins/tags.rb index c52f4aa..dda8454 100644 --- a/_plugins/tags.rb +++ b/_plugins/tags.rb @@ -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 \ No newline at end of file diff --git a/blog.html b/blog.html index 958d848..7cdba24 100644 --- a/blog.html +++ b/blog.html @@ -1,6 +1,7 @@ --- layout: default title: Blog +permalink: /blog/ ---
{% for post in site.posts %} diff --git a/tag/.gitkeep b/tag/.gitkeep deleted file mode 100644 index e69de29..0000000