Skip to content

Commit 0443d82

Browse files
authored
Merge pull request #2 from justcoded/feature/add-open-close-first-level-nav
Feature/add open close first level nav
2 parents e093e16 + 96fd4bc commit 0443d82

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
---
1010
<!--- END HEADER -->
1111

12+
## [1.5.0]() _(2024-05-03)_
13+
### Added
14+
15+
* Open-close for first level of sidebar menu
16+
17+
---
18+
1219
## [1.4.0]() _(2024-02-22)_
1320
### Added
1421

html/assets/css/themeable-customization.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@
1717
.mermaid {
1818
background-color: #fff;
1919
}
20+
21+
[data-submenu-state="collapsed"] > ul {
22+
display: none;
23+
}
24+
.sidebar-nav [data-submenu-state] > a {
25+
padding: var(--sidebar-nav-pagelink-padding);
26+
}
27+
.sidebar-nav [data-submenu-state="collapsed"] > a {
28+
background: var(--sidebar-nav-pagelink-background);
29+
}
30+
.sidebar-nav [data-submenu-state="opened"] > a {
31+
background: var(--sidebar-nav-pagelink-background--active);
32+
color: var(--sidebar-nav-link-color--active);
33+
}

html/assets/js/plugins/folder-related-links.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
const folder = vm.route.file.match(folderNameRegex)[1];
1515
const matches = markdown.matchAll(markdownLinkRegex);
1616
let newMarkdown = markdown;
17-
console.log([folder, matches]);
17+
1818

1919
for (const [match, firstSymbol, urlInBrackets] of matches) {
20-
console.log([match, firstSymbol, urlInBrackets])
20+
2121
const url = urlInBrackets.slice(1, -1);
2222

2323
if (firstSymbol === '!' || httpsRegex.test(url)) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(function () {
2+
const menuCollapse = function (hook, vm) {
3+
// Hook to execute after the page is fully loaded https://github.com/docsifyjs/docsify/blob/develop/docs/write-a-plugin.md
4+
hook.doneEach(() => {
5+
// Select all parent items
6+
const parentItems = document.querySelectorAll('.sidebar-nav > ul > li');
7+
8+
parentItems.forEach(item => {
9+
/**
10+
* Skip items that already have link or not rendered
11+
* Check on empty string required because an original docsify menu does not provide hooks after load
12+
*/
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);
21+
22+
// Check if the parent item has a sublist
23+
const sublist = item.querySelector('ul');
24+
if (!sublist) return;
25+
26+
// Check if any nested items in the sublist have the 'active' class
27+
const hasActiveItems = sublist.querySelector('.active');
28+
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');
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+
});
45+
});
46+
});
47+
};
48+
49+
// Add the plugin to the Docsify plugin array
50+
$docsify = $docsify || {};
51+
$docsify.plugins = [$docsify.plugins || [], menuCollapse];
52+
})();

html/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<!--# if expr="${SSI_PLUGIN_FOLDER_RELATED_LINKS} = /(true|yes|1)/" -->
3636
<script src="/assets/js/plugins/folder-related-links.js"></script>
3737
<!--# endif -->
38+
<script src="/assets/js/plugins/menu-collapse.js"></script>
3839
<script src="/assets/js/prism/prism-bash.min.js"></script>
3940
<script src="/assets/js/prism/prism-php.min.js"></script>
4041
<script src="/assets/js/prism/prism-diff.min.js"></script>

0 commit comments

Comments
 (0)