|
2 | 2 | const menuCollapse = function (hook, vm) { |
3 | 3 | // Hook to execute after the page is fully loaded https://github.com/docsifyjs/docsify/blob/develop/docs/write-a-plugin.md |
4 | 4 | hook.doneEach(() => { |
| 5 | + // Select all parent items |
5 | 6 | const parentItems = document.querySelectorAll('.sidebar-nav > ul > li'); |
6 | 7 |
|
7 | 8 | parentItems.forEach(item => { |
8 | 9 | /** |
9 | 10 | * Skip items that already have link or not rendered |
10 | 11 | * Check on empty string required because an original docsify menu does not provide hooks after load |
11 | 12 | */ |
12 | | - if (item.firstChild.nodeType !== Node.TEXT_NODE || item.firstChild.textContent.trim() === "") return; |
| 13 | + const textNode = item.firstChild; |
| 14 | + if (textNode.nodeType !== Node.TEXT_NODE || textNode.textContent.trim() === "") return; |
| 15 | + |
| 16 | + // Create link for text item |
| 17 | + const link = document.createElement('a'); |
| 18 | + link.href = '#'; |
| 19 | + link.textContent = textNode.textContent; |
| 20 | + textNode.parentNode.replaceChild(link, textNode); |
13 | 21 |
|
14 | 22 | // Check if the parent item has a sublist |
15 | 23 | const sublist = item.querySelector('ul'); |
| 24 | + if (!sublist) return; |
16 | 25 |
|
17 | | - if (sublist) { |
18 | | - // Check if any nested items in the sublist have the 'active' class |
19 | | - const nestedItems = sublist.querySelectorAll('li'); |
20 | | - const hasActiveItems = Array.from(nestedItems).some(nestedItem => nestedItem.classList.contains('active')); |
21 | | - |
22 | | - /** |
23 | | - * Add the 'collapse' class to items without active nested items, otherwise add 'active' |
24 | | - * That is need to highlight menu item on a page load when subitem is active |
25 | | - */ |
26 | | - if (!hasActiveItems) { |
27 | | - item.classList.add('collapse'); |
28 | | - } else { |
29 | | - item.classList.add('active'); |
30 | | - item.classList.remove('collapse'); |
31 | | - } |
32 | | - |
33 | | - // Add class to sublist |
34 | | - sublist.classList.add('app-sub-sidebar'); |
35 | | - |
36 | | - // Wrap text item in a link to follow default menu behavior and classes |
37 | | - const textNode = item.firstChild; |
38 | | - const link = document.createElement('a'); |
39 | | - link.href = '#/'; |
40 | | - link.textContent = textNode.textContent; |
41 | | - textNode.parentNode.replaceChild(link, textNode); |
| 26 | + // Check if any nested items in the sublist have the 'active' class |
| 27 | + const hasActiveItems = sublist.querySelector('.active'); |
42 | 28 |
|
43 | | - // Add click event listener to link to prevent default behavior |
44 | | - link.addEventListener('click', (e) => { |
45 | | - e.preventDefault(); |
46 | | - // Close all other collapsed items when a link is clicked |
47 | | - parentItems.forEach(otherItem => { |
48 | | - if (otherItem !== item) { |
49 | | - otherItem.classList.remove('collapse'); |
50 | | - } |
51 | | - }); |
52 | | - }); |
| 29 | + /** |
| 30 | + * Set data-submenu-state attribute to items without active nested items, otherwise set it to 'active' |
| 31 | + * That is need to highlight menu item on a page load when subitem is active |
| 32 | + */ |
| 33 | + if (hasActiveItems) { |
| 34 | + item.setAttribute('data-submenu-state', 'opened'); |
| 35 | + } else { |
| 36 | + item.setAttribute('data-submenu-state', 'collapsed'); |
53 | 37 | } |
| 38 | + |
| 39 | + // Add click event listener to link to toggle submenu state |
| 40 | + link.addEventListener('click', (e) => { |
| 41 | + e.preventDefault(); |
| 42 | + const submenuState = item.getAttribute('data-submenu-state'); |
| 43 | + item.setAttribute('data-submenu-state', submenuState === 'collapsed' ? 'opened' : 'collapsed'); |
| 44 | + }); |
54 | 45 | }); |
55 | 46 | }); |
56 | 47 | }; |
57 | 48 |
|
58 | 49 | // Add the plugin to the Docsify plugin array |
59 | 50 | $docsify = $docsify || {}; |
60 | | - $docsify.plugins = [].concat(menuCollapse, $docsify.plugins || []); |
| 51 | + $docsify.plugins = [$docsify.plugins || [], menuCollapse]; |
61 | 52 | })(); |
0 commit comments