From 420e822c8f52370bc063c8ab9df417a9a3d211bf Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Sun, 20 Oct 2019 20:44:43 +0200 Subject: Added meta tags into the default layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also includes a small Jekyll plugin that automatically guesses the OG image to be used depending on the availability of a thumbnail image. Signed-off-by: Miquel Sabaté Solà --- _includes/header.html | 11 ++++++ _layouts/default.html | 34 +++++++++++++++--- _plugins/twitter.rb | 52 ++++++++++++++++++++++++++++ images/default.png | Bin 0 -> 9980 bytes images/empty.png | Bin 0 -> 1627 bytes images/posts/la-recollida-porta-a-porta.png | Bin 0 -> 55650 bytes stylesheets/partials/minima/_layout.scss | 1 + 7 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 _plugins/twitter.rb create mode 100644 images/default.png create mode 100644 images/empty.png create mode 100644 images/posts/la-recollida-porta-a-porta.png diff --git a/_includes/header.html b/_includes/header.html index ca0eeca..3609584 100644 --- a/_includes/header.html +++ b/_includes/header.html @@ -8,6 +8,16 @@ diff --git a/_layouts/default.html b/_layouts/default.html index 0adbf0d..2b452db 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -14,16 +14,42 @@ {% endcomment %} {% if page.title %} {% if page.title contains "titles." %} - {{ site.title }} - {% t page.title %} + {% capture title %}{{ site.title }} - {% t page.title %}{% endcapture %} + {% capture raw_title %}{% t page.title %}{% endcapture %} {% else %} - {{ site.title }} - {{ page.title }} + {% capture title %}{{ site.title }} - {{ page.title }}{% endcapture %} + {% capture raw_title %}{{ page.title }}{% endcapture %} {% endif %} {% else %} - {{ site.title }} + {% capture title %}{{ site.title }}{% endcapture %} + {% capture raw_title %}{% endcapture %} {% endif %} + {{ title }} + - + {% capture description %}{% if page.excerpt %}{{ page.excerpt | strip_html | strip_newlines | truncate: 160 }}{% else %}{{ site.description }}{% endif %}{% endcapture %} + + + {% comment %}{% endcomment %} + + + + + {% comment %}{% endcomment %} + {% if site.lang == "ca" %} + + + + {% else %} + + + + {% endif %} + + + + {% render_og_image %} {% comment %}{% endcomment %} diff --git a/_plugins/twitter.rb b/_plugins/twitter.rb new file mode 100644 index 0000000..bc4c99b --- /dev/null +++ b/_plugins/twitter.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +# Copyright (C) 2019 Miquel Sabaté Solà +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +module Jekyll + # Render the OG image meta HTML tag for the currently rendered page. + class RenderOGImageTag < Liquid::Tag + # The path to be used when the current page does not have a specific image. + DEFAULT_PATH = "/images/default.png" + + def render(context) + img = normalize(context["raw_title"]) + ".png" + path = image_exists?(img) ? "/images/posts/#{img}" : DEFAULT_PATH + render_meta("og:image", "http://jo.mssola.com#{path}") + end + + protected + + # Returns true if the given image exists inside of /images/posts. + def image_exists?(image) + return false if image == ".png" + + path = File.expand_path(File.join(File.dirname(__FILE__), "..", "images", "posts", image)) + File.file?(path) + end + + # Downcases the given string and replaces space characters with "-" + def normalize(str) + str.gsub(/\s/, "-").downcase + end + + # Render an HTML meta tag with the given property and content attributes. + def render_meta(property, content) + "" + end + end +end + +Liquid::Template.register_tag("render_og_image", Jekyll::RenderOGImageTag) diff --git a/images/default.png b/images/default.png new file mode 100644 index 0000000..8a85866 Binary files /dev/null and b/images/default.png differ diff --git a/images/empty.png b/images/empty.png new file mode 100644 index 0000000..bc21f1c Binary files /dev/null and b/images/empty.png differ diff --git a/images/posts/la-recollida-porta-a-porta.png b/images/posts/la-recollida-porta-a-porta.png new file mode 100644 index 0000000..06371da Binary files /dev/null and b/images/posts/la-recollida-porta-a-porta.png differ diff --git a/stylesheets/partials/minima/_layout.scss b/stylesheets/partials/minima/_layout.scss index dcfa539..654cbd9 100644 --- a/stylesheets/partials/minima/_layout.scss +++ b/stylesheets/partials/minima/_layout.scss @@ -91,6 +91,7 @@ input:checked ~ .trigger { display: block; padding-bottom: 5px; + padding-right: 10px; } .page-link { -- cgit v1.2.3