refactor: ♻️ move data up in the data cascade

In order for og-image generation to not generate broken previews the flow of data needs to be handled differently. This also makes metadata generation more predictable.
This commit is contained in:
Sebin Nyshkim 2025-05-18 17:12:11 +02:00
parent ddbad1d1a2
commit d2a9ec7936
6 changed files with 26 additions and 40 deletions

View file

@ -29,8 +29,7 @@ const urlFormat = ({ src, width, format }) => {
const imgUuid = src.match(/\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/); const imgUuid = src.match(/\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/);
const imgFormat = format === 'jpeg' ? 'jpg' : format; const imgFormat = format === 'jpeg' ? 'jpg' : format;
if (src.startsWith(baseUrl)) if (src.startsWith(baseUrl)) return `${baseUrl}/${imgUuid[1]}.${imgFormat}?width=${width}`;
return `${baseUrl}/${imgUuid[1]}.${imgFormat}?width=${width}`;
return src; return src;
}; };
@ -56,6 +55,17 @@ export default async function (eleventyConfig) {
: collection.getFilteredByGlob('./src/{posts,drafts}/*.md') : collection.getFilteredByGlob('./src/{posts,drafts}/*.md')
); );
eleventyConfig.addGlobalData('site_name', "Sebin's Blog");
eleventyConfig.addGlobalData('type', 'article');
eleventyConfig.addGlobalData('image', { width: 1200, height: 630, src: '', alt: '' });
eleventyConfig.addGlobalData('author', {
name: 'Sebin Nyshkim',
href: 'https://blog.sebin-nyshkim.net',
image: 'https://img.sebin-nyshkim.net/i/b6629b72-ab77-4a6c-bf97-b1a615cc2454'
});
eleventyConfig.addGlobalData('twitter', { cardType: 'summary_large_image' });
eleventyConfig.addGlobalData('mastodon', { fediverseCreator: '@SebinNyshkim@meow.social' });
eleventyConfig.setFrontMatterParsingOptions({ eleventyConfig.setFrontMatterParsingOptions({
excerpt: (file) => { excerpt: (file) => {
if (!file.data.tags) return; // immediately return if not a blog post with tags if (!file.data.tags) return; // immediately return if not a blog post with tags
@ -158,6 +168,7 @@ export const config = {
input: 'src', input: 'src',
output: 'public', output: 'public',
layouts: 'layouts', layouts: 'layouts',
includes: 'includes' includes: 'includes',
data: 'data'
} }
}; };

View file

@ -0,0 +1,4 @@
export default {
page_title: (data) => (data.title ? `${data.title} - ${data.site_name}` : data.site_name),
og_title: (data) => (data.title ? data.title : data.site_name)
};

View file

@ -1,18 +1,5 @@
{ {
"layout": "blogpost.njk", "layout": "blogpost.njk",
"permalink": "/drafts/{{ title | slugify }}/", "permalink": "/drafts/{{ title | slugify }}/",
"date": "git Created", "date": "git Created"
"type": "article",
"author": {
"name": "Sebin Nyshkim",
"href": "https://blog.sebin-nyshkim.net",
"image": "https://img.sebin-nyshkim.net/i/b6629b72-ab77-4a6c-bf97-b1a615cc2454"
},
"twitter": {
"cardType": "summary_large_image",
"account": "SebinNyshkim"
},
"mastodon": {
"fediverseCreator": "@SebinNyshkim@meow.social"
}
} }

View file

@ -1,5 +1,4 @@
--- ---
title: Home
layout: page.njk layout: page.njk
eleventyNavigation: eleventyNavigation:
key: home key: home

View file

@ -2,16 +2,17 @@
<html lang="en"> <html lang="en">
<head> <head>
{% metagen {% metagen
title = title + ' - Sebin\'s Blog', title = page_title,
desc = page.excerpt | toPlain, desc = page.excerpt | toPlain,
url = 'https://blog.sebin-nyshkim.net' + page.url, url = 'https://blog.sebin-nyshkim.net' + page.url,
type = type, type = type,
site_name = 'Sebin\'s Blog', site_name = site_name,
og_title = title, og_title = og_title,
og_image_width = image.width, og_image_width = image.width,
og_image_height = image.height, og_image_height = image.height,
og_image_alt = image.alt, og_image_alt = image.alt,
og_image_type = 'image/webp', og_image_type = 'image/webp',
twitter_title = og_title,
twitter_card_type = twitter.cardType, twitter_card_type = twitter.cardType,
name = author.name, name = author.name,
generator = eleventy.generator, generator = eleventy.generator,
@ -23,7 +24,7 @@
'/css/style.css', '/css/style.css',
'/css/prism.css'] '/css/prism.css']
%} %}
{% ogImage "og-image.og.njk", { title: title, author: author, image: image } %} {% ogImage "og-image.og.njk", { title: title or site_name, author: author, image: image } %}
{% if mastodon.fediverseCreator and mastodon.fediverseCreator != '' %} {% if mastodon.fediverseCreator and mastodon.fediverseCreator != '' %}
<meta name="fediverse:creator" content="{{ mastodon.fediverseCreator }}" /> <meta name="fediverse:creator" content="{{ mastodon.fediverseCreator }}" />
{% endif %} {% endif %}
@ -36,7 +37,7 @@
<div class="flex min-h-16 max-w-(--breakpoint-xl) items-center justify-center bg-sky-600 shadow-xl sm:m-4 sm:mx-auto sm:justify-between sm:rounded-xl dark:bg-sky-950"> <div class="flex min-h-16 max-w-(--breakpoint-xl) items-center justify-center bg-sky-600 shadow-xl sm:m-4 sm:mx-auto sm:justify-between sm:rounded-xl dark:bg-sky-950">
<div class="hidden sm:flex sm:items-center"> <div class="hidden sm:flex sm:items-center">
<img src="https://img.sebin-nyshkim.net/i/b6629b72-ab77-4a6c-bf97-b1a615cc2454" alt="" class="m-4 max-w-12 rounded-full border-4 border-white shadow-2xl lg:m-5 lg:max-w-14"> <img src="https://img.sebin-nyshkim.net/i/b6629b72-ab77-4a6c-bf97-b1a615cc2454" alt="" class="m-4 max-w-12 rounded-full border-4 border-white shadow-2xl lg:m-5 lg:max-w-14">
<h1 class="text-2xl text-white lg:text-3xl">Sebin's Blog</h1> <h1 class="text-2xl text-white lg:text-3xl">{{ site_name }}</h1>
</div> </div>
<nav class="eleventy-navigation" aria-label="Main"> <nav class="eleventy-navigation" aria-label="Main">
{{ {{

View file

@ -1,21 +1,5 @@
{ {
"layout": "blogpost.njk", "layout": "blogpost.njk",
"permalink": "/posts/{{ title | slugify }}/", "permalink": "/posts/{{ title | slugify }}/",
"date": "git Created", "date": "git Created"
"type": "article",
"image": {
"width": 1200,
"height": 630
},
"author": {
"name": "Sebin Nyshkim",
"href": "https://blog.sebin-nyshkim.net",
"image": "https://img.sebin-nyshkim.net/i/b6629b72-ab77-4a6c-bf97-b1a615cc2454"
},
"twitter": {
"cardType": "summary_large_image"
},
"mastodon": {
"fediverseCreator": "@SebinNyshkim@meow.social"
}
} }