Fix link activation

This commit is contained in:
Tobias Kunze 2025-04-07 22:47:00 +02:00
parent 845c50f891
commit 3ecf57b884

View file

@ -1,13 +1,12 @@
/** /**
* This script marks the current path as active in the sidebar. * This script marks the current path as active in the menu.
*/ */
const markActive = (link) => { const markActive = (link) => {
const parentItem = link.closest('.sidebar-item'); const parentItem = link.closest('.menu-item');
link.classList.add('active');
if (parentItem) { if (parentItem) {
parentItem.classList.add('active'); parentItem.classList.add('active');
} else {
link.classList.add('active');
} }
} }
@ -16,13 +15,13 @@ const checkLink = (fuzzy) => {
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const currentPath = window.location.pathname; const currentPath = window.location.pathname;
const sidebarLinks = [...document.querySelectorAll('a.sidebar-link')] const menuLinks = [...document.querySelectorAll('a.menu-link')]
const exactMatches = sidebarLinks.filter(link => link.getAttribute('href') === currentPath) const exactMatches = menuLinks.filter(link => link.getAttribute('href') === currentPath)
if (exactMatches.length > 0) { if (exactMatches.length > 0) {
markActive(exactMatches[0]) markActive(exactMatches[0])
} else { } else {
fuzzyMatches = sidebarLinks.filter(link => currentPath.startsWith(link.getAttribute('href'))) fuzzyMatches = menuLinks.filter(link => currentPath.startsWith(link.getAttribute('href')))
if (fuzzyMatches.length > 0) { if (fuzzyMatches.length > 0) {
const longestMatch = fuzzyMatches.sort((a, b) => b.href.length - a.href.length)[0] const longestMatch = fuzzyMatches.sort((a, b) => b.href.length - a.href.length)[0]
markActive(longestMatch) markActive(longestMatch)