From 46dcd3dbdd94fb3b6334c46e409c6bdea8485fac Mon Sep 17 00:00:00 2001 From: rashidakanchwala <37628668+rashidakanchwala@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:29:52 +0100 Subject: [PATCH] Fix active navigation (#17) --- .../theme/kedro-sphinx-theme/layout.html | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/kedro_sphinx_theme/theme/kedro-sphinx-theme/layout.html b/src/kedro_sphinx_theme/theme/kedro-sphinx-theme/layout.html index bf80edf..1bf69bf 100644 --- a/src/kedro_sphinx_theme/theme/kedro-sphinx-theme/layout.html +++ b/src/kedro_sphinx_theme/theme/kedro-sphinx-theme/layout.html @@ -13,7 +13,7 @@
- Kedro + Kedro Kedro-Viz Kedro-Datasets
@@ -32,6 +32,20 @@ //inline styling for read-the-docs flyout document.documentElement.style.setProperty('--readthedocs-flyout-font-size', '0.9em'); + function highlightActiveLink() { + var currentUrl = window.location.href; + const links = document.querySelectorAll(".wy-main-nav .wy-main-nav-link"); + + // Iterate over each link and check if its href is part of the current URL + links.forEach(link => { + if (currentUrl.includes(link.href)) { + link.classList.add("active"); + } else { + link.classList.remove("active"); // Remove the active class if it's not active + } + }); + } + document.addEventListener("DOMContentLoaded", function() { function showReadTheDocsSearch() { @@ -66,19 +80,12 @@ openBtn.classList.add('wy-nav-top-open'); document.querySelector('.wy-nav-top').prepend(openBtn); - // Select all navigation links within the .wy-main-nav container - var navLinks = document.querySelectorAll('.wy-main-nav .wy-main-nav-link'); - var currentPath = window.location.pathname; + highlightActiveLink(); - // Iterate over each link to check if the current path includes the link's unique ID - navLinks.forEach(function(link) { - var kedroSpecificPath = 'https://docs.kedro.org/en'; - var isKedroLink = link.id === 'kedro'; - - if ((isKedroLink && currentPath.includes(kedroSpecificPath)) || (!isKedroLink && currentPath.includes(link.id))) { - link.classList.add('active'); - } + // Optionally, listen for URL changes (e.g., when using history.pushState) + window.addEventListener('popstate', function(event) { + highlightActiveLink(); }); -}); + }); {% endblock %}