From b79fb3bd7ff74867a350a7ebcded34e6a1ec1cb9 Mon Sep 17 00:00:00 2001 From: Fedele Mantuano Date: Tue, 20 Oct 2026 17:54:49 +0200 Subject: [PATCH 1/2] Add shared article cover images for both languages --- README.md | 37 ++++++++++++++++++++++++++++++ public/images/posts/.gitkeep | 0 src/lib/covers.ts | 11 +++++++++ src/pages/[lang]/blog/[slug].astro | 3 +++ src/styles/global.css | 1 + 5 files changed, 52 insertions(+) create mode 100644 public/images/posts/.gitkeep create mode 100644 src/lib/covers.ts diff --git a/README.md b/README.md index da9c5d4..cbd8bbf 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,43 @@ Dates display in the selected language using UTC. Reading time is estimated at 200 words per minute, with a minimum of one minute. Set optional `youtubeId` to an 11-character YouTube video ID to show a player. +### Article covers + +Add a single WebP file to `public/images/posts/`, named after the article's +`translationKey`. The article template automatically displays it above the title +in both languages. No frontmatter image field or Markdown image is needed. +Articles without a matching file display normally, without an empty placeholder. +File names are case-sensitive; use the exact translation key and `.webp` extension. + +For example, both translations of the grill-me article use +`translationKey: grill-me-before-coding`, so their shared cover is: + +```text +public/images/posts/grill-me-before-coding.webp +``` + +Prepare all covers at **1600 × 900 pixels (16:9)** and export as WebP, ideally +under 300 KB. Verify dimensions after AI generation and resize or crop before +adding the file. The template reserves a 16:9 area to prevent layout shifts, +scales it to the article width on every screen, and crops other aspect ratios +to fit. It does not convert files or enforce their source pixel dimensions. +Keep important elements away from the edges. Use a consistent illustration +style and palette, without text, logos, or watermarks, so one image works in +both languages. These covers are decorative introductions to the article; +their empty alternative text avoids repeating the adjacent article title. +Keep any information needed to understand the article in its text. + +Reusable generation prompt (replace the bracketed subjects): + +> Crea una copertina editoriale per un articolo su [ARGOMENTO]. Illustrazione +> vettoriale con contorni netti, forme semplici, palette coerente e composizione +> orizzontale. Rappresenta visivamente [CONCETTO PRINCIPALE]. Nessun testo, +> lettering, logo o watermark. Rapporto 16:9, dimensioni finali 1600 × 900 pixel. + +Run `npm run dev` to inspect the cover, then `npm run verify`. Adding or replacing +a cover on the published site requires a new build and deployment, like other +content changes. Covers appear on individual articles; list cards remain text-only. + ### Article contents Articles with at least one Markdown `##` or `###` heading automatically show an diff --git a/public/images/posts/.gitkeep b/public/images/posts/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/lib/covers.ts b/src/lib/covers.ts new file mode 100644 index 0000000..d6fe9da --- /dev/null +++ b/src/lib/covers.ts @@ -0,0 +1,11 @@ +const covers = import.meta.glob('/public/images/posts/*.webp', { + eager: true, + query: '?url&no-inline', + import: 'default', +}); + +/** Resolve one shared cover for all translations from the build's asset list. */ +export function postCoverUrl(translationKey: string): string | undefined { + const filename = `${translationKey}.webp`; + return covers[`/public/images/posts/${filename}`]; +} diff --git a/src/pages/[lang]/blog/[slug].astro b/src/pages/[lang]/blog/[slug].astro index f04fa38..b193f3a 100644 --- a/src/pages/[lang]/blog/[slug].astro +++ b/src/pages/[lang]/blog/[slug].astro @@ -6,6 +6,7 @@ import TableOfContents from '../../../components/TableOfContents.astro'; import AuthorLink from '../../../components/AuthorLink.astro'; import { visiblePosts, postUrl, readingMinutes, formatDate, type Post } from '../../../lib/posts'; import { tagUrl } from '../../../lib/tags'; +import { postCoverUrl } from '../../../lib/covers'; export async function getStaticPaths() { return (await visiblePosts()).map((post) => ({ params: { lang: post.data.lang, slug: post.data.urlSlug }, @@ -15,6 +16,7 @@ export async function getStaticPaths() { const { post } = Astro.props as { post: Post }; const { Content, headings } = await render(post); const { lang } = post.data; +const coverUrl = postCoverUrl(post.data.translationKey); const related = (await visiblePosts()).filter((item) => item.data.translationKey === post.data.translationKey); const alternatives = related.map((item) => ({ lang: item.data.lang, url: postUrl(item) })); const toc = headings.filter((heading) => heading.depth === 2 || heading.depth === 3); @@ -25,6 +27,7 @@ const tocLabel = lang === 'it' ? 'Indice' : 'Contents';
← {lang === 'it' ? 'Tutti gli articoli' : 'All posts'}
+ {coverUrl && }

{post.data.title}

{post.data.description}