fix(og): match Eleventy's page.fileSlug by stripping date prefix

Eleventy v3 parses YYYY-MM-DD- from filenames and removes it from
page.fileSlug. The OG generator was using the full filename (with
date prefix) causing a slug mismatch — hasOgImage filter checked
for 'slug.png' while the file was 'YYYY-MM-DD-slug.png'.

Also removes debug logging from hasOgImage filter.
This commit is contained in:
Ricardo
2026-02-18 09:42:10 +01:00
parent 719ce101b7
commit 8503237d4d
2 changed files with 10 additions and 6 deletions
+1 -5
View File
@@ -344,11 +344,7 @@ export default function (eleventyConfig) {
eleventyConfig.addFilter("hasOgImage", (fileSlug) => { eleventyConfig.addFilter("hasOgImage", (fileSlug) => {
if (!fileSlug) return false; if (!fileSlug) return false;
const ogPath = resolve(__dirname, ".cache", "og", `${fileSlug}.png`); const ogPath = resolve(__dirname, ".cache", "og", `${fileSlug}.png`);
const exists = existsSync(ogPath); return existsSync(ogPath);
if (!exists && fileSlug.includes("quand-un-oui")) {
console.log(`[og-debug] fileSlug=${fileSlug}, path=${ogPath}, exists=${exists}`);
}
return exists;
}); });
// Current timestamp filter (for client-side JS buildtime) // Current timestamp filter (for client-side JS buildtime)
+9 -1
View File
@@ -105,6 +105,14 @@ function formatDate(dateStr) {
} }
} }
/**
* Strip YYYY-MM-DD- date prefix from filename to match Eleventy's page.fileSlug.
* Eleventy v3 parses dates from filenames and removes the prefix from fileSlug.
*/
function toEleventySlug(filename) {
return filename.replace(/^\d{4}-\d{2}-\d{2}-/, "");
}
function truncateTitle(title, max = 120) { function truncateTitle(title, max = 120) {
if (!title || title.length <= max) return title || "Untitled"; if (!title || title.length <= max) return title || "Untitled";
return title.slice(0, max).trim() + "\u2026"; return title.slice(0, max).trim() + "\u2026";
@@ -299,7 +307,7 @@ export async function generateOgImages(contentDir, cacheDir, siteName) {
continue; continue;
} }
const slug = basename(filePath, ".md"); const slug = toEleventySlug(basename(filePath, ".md"));
const title = fm.title || fm.name || extractBodyText(raw); const title = fm.title || fm.name || extractBodyText(raw);
const date = fm.published || fm.date || ""; const date = fm.published || fm.date || "";
const postType = detectPostType(filePath); const postType = detectPostType(filePath);