From f39f5ee2ff6e5598fd1be47b96b565a06f3d9695 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt <coryd@hey.com> Date: Mon, 18 Mar 2024 18:44:35 -0700 Subject: [PATCH] chore: improve nav active state checking --- config/filters/index.js | 6 ++++++ package.json | 2 +- src/_includes/partials/nav/link.liquid | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/config/filters/index.js b/config/filters/index.js index 573ebd91..4793cd39 100644 --- a/config/filters/index.js +++ b/config/filters/index.js @@ -37,6 +37,12 @@ export default { }, replaceQuotes: (string) => string.replace(/"/g, "'"), + // navigation + isLinkActive: (category, page) => { + const normalizedPage = page.includes('.html') ? page.replace('.html', '/') : page + return !!normalizedPage && normalizedPage.includes(category) && !/\d+/.test(normalizedPage); + }, + // analytics getPopularPosts: (posts, analytics) => { return posts diff --git a/package.json b/package.json index 48967d6b..630066ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "8.1.4", + "version": "8.2.4", "description": "The source for my personal site. Built using 11ty.", "type": "module", "scripts": { diff --git a/src/_includes/partials/nav/link.liquid b/src/_includes/partials/nav/link.liquid index 955592b6..148274e1 100644 --- a/src/_includes/partials/nav/link.liquid +++ b/src/_includes/partials/nav/link.liquid @@ -1,5 +1,5 @@ -{%- capture pageUrl %}/{{ link | downcase }}/{% endcapture -%} -{% if pageUrl contains page.fileSlug and page.fileSlug %} +{%- capture categoryUrl %}/{{ link | downcase }}/{% endcapture -%} +{% if categoryUrl | isLinkActive: page.url %} <span class="active {{ class }}" aria-current="page"> {% if icon %} {% tablericon icon link %}