|
| 1 | +document.addEventListener('DOMContentLoaded', function () { |
| 2 | + // Only run on the tutorials page |
| 3 | + const isTutorialsPage = window.location.pathname.includes('/tutorials'); |
| 4 | + |
| 5 | + if (isTutorialsPage) { |
| 6 | + collapseTutorialNav(); |
| 7 | + } |
| 8 | + |
| 9 | + function collapseTutorialNav() { |
| 10 | + // Find the navigation sidebar |
| 11 | + const navSidebar = document.querySelector('.md-sidebar--primary'); |
| 12 | + if (!navSidebar) return; |
| 13 | + |
| 14 | + // Find the 'Tutorials' section in the navigation |
| 15 | + const tutorialsSection = Array.from( |
| 16 | + navSidebar.querySelectorAll('.md-nav__item') |
| 17 | + ).find((item) => { |
| 18 | + const linkSpan = item.querySelector('.md-nav__link .md-ellipsis'); |
| 19 | + return linkSpan && linkSpan.textContent.trim() === 'Tutorials'; |
| 20 | + }); |
| 21 | + |
| 22 | + if (!tutorialsSection) return; |
| 23 | + |
| 24 | + // Find all nested subsections under Tutorials (level 2) |
| 25 | + const tutorialsNav = tutorialsSection.querySelector( |
| 26 | + '.md-nav[data-md-level="1"]' |
| 27 | + ); |
| 28 | + if (!tutorialsNav) return; |
| 29 | + |
| 30 | + const subsections = tutorialsNav.querySelectorAll( |
| 31 | + ':scope > .md-nav__list > .md-nav__item.md-nav__item--nested' |
| 32 | + ); |
| 33 | + |
| 34 | + subsections.forEach((subsection) => { |
| 35 | + // Find the nested navigation (level 2) |
| 36 | + const nestedNav = subsection.querySelector('.md-nav[data-md-level="2"]'); |
| 37 | + if (!nestedNav) return; |
| 38 | + |
| 39 | + const nestedList = nestedNav.querySelector('.md-nav__list'); |
| 40 | + if (!nestedList) return; |
| 41 | + |
| 42 | + const items = Array.from( |
| 43 | + nestedList.querySelectorAll(':scope > .md-nav__item') |
| 44 | + ); |
| 45 | + |
| 46 | + // Limit to 3 items visible |
| 47 | + const maxVisibleItems = 3; |
| 48 | + |
| 49 | + if (items.length > maxVisibleItems) { |
| 50 | + // Hide items beyond the limit |
| 51 | + items.slice(maxVisibleItems).forEach((item) => { |
| 52 | + item.style.display = 'none'; |
| 53 | + }); |
| 54 | + |
| 55 | + // Get the category link to determine the 'More tutorials' URL |
| 56 | + const categoryLink = subsection.querySelector( |
| 57 | + ':scope > .md-nav__container > a.md-nav__link, :scope > a.md-nav__link' |
| 58 | + ); |
| 59 | + const categoryUrl = categoryLink |
| 60 | + ? categoryLink.getAttribute('href') |
| 61 | + : '#'; |
| 62 | + |
| 63 | + // Create and add 'More tutorials' link |
| 64 | + const moreTutorialsItem = document.createElement('li'); |
| 65 | + moreTutorialsItem.className = 'md-nav__item learn-more-item'; |
| 66 | + |
| 67 | + const moreTutorialsLink = document.createElement('a'); |
| 68 | + moreTutorialsLink.className = 'md-nav__link'; |
| 69 | + moreTutorialsLink.href = categoryUrl; |
| 70 | + moreTutorialsLink.textContent = 'More tutorials →'; |
| 71 | + |
| 72 | + moreTutorialsItem.appendChild(moreTutorialsLink); |
| 73 | + nestedList.appendChild(moreTutorialsItem); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | +}); |
0 commit comments