Generate tags and categories dynamically in Jekyll

2023-12-09 · christian · jekyll, ruby

When a Jekyll Page is getting more complex, it’s helpful to add custom meta data to pages and posts. For example, add a auto generated category to only show the primary language version of a multi language post on the index page but also let single posts in a secondary language show up.

Frontmatter of the German post:

title: Static Binaries
locale: de
ref: static-binaries

Frontmatter of the English post:

title: Static Binaries
locale: en
ref: static-binaries

The ref Frontmatter field connects identical posts in different languages with each other.

The plugin is placed into the _plugins/ directory. The generate() function iterates though all existing pages and posts and modifies the Frontmatter data.

# _plugins/fontpage.rb
module FrontPagePlugin
    class FrontPageGenerator < Jekyll::Generator

        safe true

        def generate(site)

            # group by ref (or id if no ref defined)
            byref = { |p|['ref'] || }

            byref.each do |key, posts|

                # ensure frontmatter fields
                posts.each do |post|
              ['locale'] = 'de'

                # get list of locales
                locales = { |post|['locale'] }

                # sort posts by locale in reverse order, so that 'en' wins against 'de'
                posts.sort_by { |post|['locale'] }.reverse.each_with_index do |post, postindex|
                    if postindex <= 0
                        # add frontpage category to first post in every ref group

                    # add locales list and language category to all posts
          ['available_locales'] = locales




In Jekyll Paginate v2 we can now filter the index page by the auto generated categories. Also creating a main index with all postings and an index for each language.

layout: localizedindex
title: Latest Posts
permalink: /index
  enabled: true
  category: frontpage

More BG by Carl Lender (CC BY 2.0) Imprint & Privacy
9d41264a 2023-12-28 19:26
Mastodon via Mastodon via