From 63a171aefd04518fc41eb81d88da8ab4b8ff07dc Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Mon, 16 Nov 2026 12:15:05 +0000 Subject: [PATCH] fix: match production URL format and fix OG URL - Use front-matter title for post slug (not filename) - Remove /posts/ prefix from URLs to match production - Remove /pages/ prefix from page URLs to match production - Fix OG URL on index page (no longer shows .html extension) - Remove dots from slugs (fixes T.I.L. posts being files not dirs) - Collapse consecutive dashes in slugs - Remove / from slug character class (prevents URL paths in slugs) - Skip index.html in permalink plugin (already has correct path from pagination) Production URL format: /YYYY-MM-DD-full-title-slug/ --- app.js | 21 ++++++++++----------- plugins/permalinks.js | 17 +++-------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/app.js b/app.js index 7920654..00854c8 100644 --- a/app.js +++ b/app.js @@ -377,7 +377,7 @@ const ms = Metalsmith(__dirname) .use(function (files, metalsmith, done) { // Pre-compute paths for layouts rendering // This mirrors the permalinks logic but doesn't rename files - const slugRe = /[<>:"'\|?*]|[^\w\s$_+~.()!\-@\/]+/g; + const slugRe = /[<>:"'\|?*]|[^\w\s$_+~.()!\-@]+/g; const linksets = [ { match: { collection: "pages" }, pattern: ":title" }, { match: { collection: "drafts" }, pattern: "drafts/:title" }, @@ -396,6 +396,12 @@ const ms = Metalsmith(__dirname) // Skip layout files (layout matches filename) const baseName = path.basename(file, ".html"); + // Don't skip index.html - it already has permalink from pagination + if (file === "index.html") { + // Set path for index page (root URL) + data.path = ""; + continue; + } if (data.layout === baseName || data.layout === `${baseName}.html`) continue; // Determine linkset @@ -426,14 +432,11 @@ const ms = Metalsmith(__dirname) const d = data._filenameDate; return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0"); } - if (key === "title" && data._filenameTitle) { - return data._filenameTitle.replace(/\s+/g, "-").replace(slugRe, ""); - } if (value instanceof Date) { const d = value; return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0"); } - return String(value).toLowerCase().replace(/\s+/g, "-").replace(slugRe, ""); + return String(value).toLowerCase().replace(/\s+/g, "-").replace(slugRe, "").replace(/-+/g, "-").replace(/\./g, ""); }) .replace(/\/+$/, ""); @@ -444,12 +447,8 @@ const ms = Metalsmith(__dirname) } } - // Prepend source directory prefix - let sourcePrefix = ""; - if (file.startsWith("posts/")) sourcePrefix = "posts/"; - else if (file.startsWith("pages/")) sourcePrefix = "pages/"; - - data.permalink = sourcePrefix + result; + // Post files: no prefix - URLs match production format /YYYY-MM-DD-slug/ + data.permalink = result; // Set path for templates (permalink without index.html) if (data.permalink.endsWith("/index.html")) { diff --git a/plugins/permalinks.js b/plugins/permalinks.js index cbef6c5..decb474 100644 --- a/plugins/permalinks.js +++ b/plugins/permalinks.js @@ -69,14 +69,10 @@ function permalinks(options) { if (key === 'date' && data._filenameDate) { return formatDate(data._filenameDate); } - // For title in permalink, use _filenameTitle if available (to match source filenames) - if (key === 'title' && data._filenameTitle) { - return slugify(data._filenameTitle); - } if (value instanceof Date) { return formatDate(value); } - return slugify(value.toString()); + return slugify(value.toString()).replace(/-+/, "-"); }); } @@ -131,14 +127,8 @@ function permalinks(options) { continue; } - // For posts: preserve the "posts/" prefix - // For other files: use the pattern as-is + let sourcePrefix = ""; - if (file.startsWith("posts/")) { - sourcePrefix = "posts/"; - } else if (file.startsWith("pages/")) { - sourcePrefix = "pages/"; - } // Replace pattern placeholders let result = replacePattern(pattern, data); @@ -155,8 +145,7 @@ function permalinks(options) { } } - // Prepend source directory prefix - data.permalink = sourcePrefix + result; + data.permalink = result; // Set path metadata for templates (permalink without index.html) if (data.permalink.endsWith("/index.html")) {