Quickfixing the Jekyll SEO plugin (to ingest page description)


Problem statement

For a while now1 I’ve been using perex front matter key on my blog to input a short summary of my posts:

---
layout: post
title:  "Quickfixing the Jekyll SEO plugin"
date:   2024-05-26 16:57:00 +0200
tags: ruby meta jekyll quickie
perex: "A quick fix for Jekyll SEO plugin to populate the description tag
from page's perex."
---

I use this mainly on the front page, to display a short snippet under the article’s name.

Imagine my surprise when I found out that Jekyll SEO plugin is not impressed by that:

[...]
<meta property="og:title" content="Quickfixing the Jekyll SEO plugin" />
<meta name="author" content="Michal Jirků" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="Problem statement" />
<meta property="og:description" content="Problem statement" />
[...]

Now, I’m not sure it has ultimately some bearing on things, but in the spirit of disliking all the things that are broken, let’s fix this.

Solution

Turns out, where the description comes from is rather clear (excerpt from the drop.rb#L90-L95):

[...]
      def description
        @description ||= begin
          value = format_string(page["description"] || page["excerpt"]) || site_description
          snippet(value, description_max_words)
        end
      end
[...]

So I whipped up a few-liner that copies the perex to description, which fixes this, issue:

# frozen_string_literal: true

=begin
Allows auto-copy `perex` to page's `description`.

That way Jekyll SEO plugin gets proper `description` and `og:description` tags.
=end

require "jekyll"

$LOAD_PATH.unshift(File.dirname(__FILE__))

Jekyll::Hooks.register([:posts, :pages, :documents], :pre_render) do |doc, _|
  unless doc.data['description']
    doc.data['description'] = doc.data['perex'] if doc.data['perex']
  end
end

All one needs to do is to stick these few lines into a file under _plugins (for example, _plugins/perex_to_description.rb).

And all is well:

[...]
<meta property="og:title" content="Quickfixing the Jekyll SEO plugin" />
<meta name="author" content="Michal Jirků" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="A quick fix for Jekyll SEO plugin to populate the description tag from page’s perex." />
<meta property="og:description" content="A quick fix for Jekyll SEO plugin to populate the description tag from page’s perex." />
[...]
  1. Since 2021-01-31, but who’s grepping, eh?