fix: normalize permalinks and page updated dates

This commit is contained in:
svemagie
2026-03-08 02:45:11 +01:00
parent cb921f076b
commit 98a1898fd1
6 changed files with 75 additions and 16 deletions
+22 -3
View File
@@ -15,14 +15,33 @@
* See: https://github.com/11ty/eleventy/issues/3183 * See: https://github.com/11ty/eleventy/issues/3183
*/ */
const normalizeOutputPermalink = (permalink) => {
if (typeof permalink !== "string") return permalink;
// Convert accidental absolute URLs to Eleventy output paths.
if (/^https?:\/\//i.test(permalink)) {
try {
const { pathname } = new URL(permalink);
if (!pathname) return "/";
return pathname.endsWith("/") ? pathname : `${pathname}/`;
} catch {
return permalink;
}
}
return permalink;
};
export default { export default {
eleventyComputed: { eleventyComputed: {
// Compute permalink from file path for posts without explicit frontmatter permalink. // Compute permalink from file path for posts without explicit frontmatter permalink.
// Pattern: content/{type}/{yyyy}-{MM}-{dd}-{slug}.md → /{type}/{yyyy}/{MM}/{dd}/{slug}/ // Pattern: content/{type}/{yyyy}-{MM}-{dd}-{slug}.md → /{type}/{yyyy}/{MM}/{dd}/{slug}/
permalink: (data) => { permalink: (data) => {
// Convert stale /content/ permalinks from pre-beta.37 posts to canonical format // Convert stale /content/ permalinks from pre-beta.37 posts to canonical format
if (data.permalink && typeof data.permalink === "string") { const explicitPermalink = normalizeOutputPermalink(data.permalink);
const contentMatch = data.permalink.match(
if (explicitPermalink && typeof explicitPermalink === "string") {
const contentMatch = explicitPermalink.match(
/^\/content\/([^/]+)\/(\d{4})-(\d{2})-(\d{2})-(.+?)\/?$/ /^\/content\/([^/]+)\/(\d{4})-(\d{2})-(\d{2})-(.+?)\/?$/
); );
if (contentMatch) { if (contentMatch) {
@@ -30,7 +49,7 @@ export default {
return `/${type}/${year}/${month}/${day}/${slug}/`; return `/${type}/${year}/${month}/${day}/${slug}/`;
} }
// Valid non-/content/ permalink — use as-is // Valid non-/content/ permalink — use as-is
return data.permalink; return explicitPermalink;
} }
// No frontmatter permalink — compute from file path // No frontmatter permalink — compute from file path
+3 -2
View File
@@ -15,9 +15,10 @@ withSidebar: true
{{ summary }} {{ summary }}
</p> </p>
{% endif %} {% endif %}
{% if updated %} {% set lastUpdated = updated or page.date %}
{% if lastUpdated %}
<p class="text-sm text-surface-600 dark:text-surface-400 mt-2"> <p class="text-sm text-surface-600 dark:text-surface-400 mt-2">
Last updated: <time class="dt-updated font-mono text-sm" datetime="{{ updated | isoDate }}">{{ updated | dateDisplay }}</time> Last updated: <time class="dt-updated font-mono text-sm" datetime="{{ lastUpdated | isoDate }}">{{ lastUpdated | dateDisplay }}</time>
</p> </p>
{% endif %} {% endif %}
</header> </header>
+20 -1
View File
@@ -1,3 +1,22 @@
const normalizePermalink = (permalink) => {
if (typeof permalink !== "string") return permalink;
if (/^https?:\/\//i.test(permalink)) {
try {
const { pathname } = new URL(permalink);
if (!pathname) return "/";
return pathname.endsWith("/") ? pathname : `${pathname}/`;
} catch {
return permalink;
}
}
return permalink;
};
export default { export default {
layout: "layouts/post.njk", layout: "layouts/post.njk",
}; eleventyComputed: {
permalink: (data) => normalizePermalink(data.permalink),
},
};
+4 -4
View File
@@ -5,10 +5,10 @@ summary: a page that tells you what I'm focused on at this point in my life
category: category:
- now - now
- slashpage - slashpage
permalink: https://blog.giersig.eu/now/ permalink: /now/
--- ---
This page is a now page, you should consider adding one if you have a website as well, inspiration comes from here This page is a now page, you should consider adding one if you have a website as well, inspiration comes from here
## personal ## personal
what a wild ride what a wild ride
+22 -3
View File
@@ -15,14 +15,33 @@
* See: https://github.com/11ty/eleventy/issues/3183 * See: https://github.com/11ty/eleventy/issues/3183
*/ */
const normalizeOutputPermalink = (permalink) => {
if (typeof permalink !== "string") return permalink;
// Convert accidental absolute URLs to Eleventy output paths.
if (/^https?:\/\//i.test(permalink)) {
try {
const { pathname } = new URL(permalink);
if (!pathname) return "/";
return pathname.endsWith("/") ? pathname : `${pathname}/`;
} catch {
return permalink;
}
}
return permalink;
};
export default { export default {
eleventyComputed: { eleventyComputed: {
// Compute permalink from file path for posts without explicit frontmatter permalink. // Compute permalink from file path for posts without explicit frontmatter permalink.
// Pattern: content/{type}/{yyyy}-{MM}-{dd}-{slug}.md → /{type}/{yyyy}/{MM}/{dd}/{slug}/ // Pattern: content/{type}/{yyyy}-{MM}-{dd}-{slug}.md → /{type}/{yyyy}/{MM}/{dd}/{slug}/
permalink: (data) => { permalink: (data) => {
// Convert stale /content/ permalinks from pre-beta.37 posts to canonical format // Convert stale /content/ permalinks from pre-beta.37 posts to canonical format
if (data.permalink && typeof data.permalink === "string") { const explicitPermalink = normalizeOutputPermalink(data.permalink);
const contentMatch = data.permalink.match(
if (explicitPermalink && typeof explicitPermalink === "string") {
const contentMatch = explicitPermalink.match(
/^\/content\/([^/]+)\/(\d{4})-(\d{2})-(\d{2})-(.+?)\/?$/ /^\/content\/([^/]+)\/(\d{4})-(\d{2})-(\d{2})-(.+?)\/?$/
); );
if (contentMatch) { if (contentMatch) {
@@ -30,7 +49,7 @@ export default {
return `/${type}/${year}/${month}/${day}/${slug}/`; return `/${type}/${year}/${month}/${day}/${slug}/`;
} }
// Valid non-/content/ permalink — use as-is // Valid non-/content/ permalink — use as-is
return data.permalink; return explicitPermalink;
} }
// No frontmatter permalink — compute from file path // No frontmatter permalink — compute from file path
+4 -3
View File
@@ -15,9 +15,10 @@ withSidebar: true
{{ summary }} {{ summary }}
</p> </p>
{% endif %} {% endif %}
{% if updated %} {% set lastUpdated = updated or page.date %}
<p class="text-sm text-surface-500 dark:text-surface-400 mt-2"> {% if lastUpdated %}
Last updated: <time class="dt-updated" datetime="{{ updated | isoDate }}">{{ updated | dateDisplay }}</time> <p class="text-sm text-surface-600 dark:text-surface-400 mt-2">
Last updated: <time class="dt-updated font-mono text-sm" datetime="{{ lastUpdated | isoDate }}">{{ lastUpdated | dateDisplay }}</time>
</p> </p>
{% endif %} {% endif %}
</header> </header>