diff --git a/_includes/components/widgets/post-navigation.njk b/_includes/components/widgets/post-navigation.njk index 3bd08a8..c67f896 100644 --- a/_includes/components/widgets/post-navigation.njk +++ b/_includes/components/widgets/post-navigation.njk @@ -1,22 +1,26 @@ {# Post Navigation Widget - Previous/Next #} -{% if previousPost or nextPost %} +{# Uses previousInCollection/nextInCollection filters to find adjacent posts #} +{% set _prevPost = collections.posts | previousInCollection(page) %} +{% set _nextPost = collections.posts | nextInCollection(page) %} + +{% if _prevPost or _nextPost %}
diff --git a/eleventy.config.js b/eleventy.config.js index 6c8b15d..bbe41b2 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -318,6 +318,22 @@ export default function (eleventyConfig) { return mentions.filter((m) => m["wm-property"] === wmProperty); }); + // Post navigation — find previous/next post in a collection + // (Nunjucks {% set %} inside {% for %} doesn't propagate, so we need filters) + eleventyConfig.addFilter("previousInCollection", function (collection, page) { + if (!collection || !page) return null; + const index = collection.findIndex((p) => p.url === page.url); + return index > 0 ? collection[index - 1] : null; + }); + + eleventyConfig.addFilter("nextInCollection", function (collection, page) { + if (!collection || !page) return null; + const index = collection.findIndex((p) => p.url === page.url); + return index >= 0 && index < collection.length - 1 + ? collection[index + 1] + : null; + }); + // Collections for different post types // Note: content path is content/ due to symlink structure // "posts" shows ALL content types combined