iPeng6 / docsify-sidebar-collapse

a docsify plugin, support sidebar catalog expand and collapse
MIT License
176 stars 37 forks source link

Parents in the hierarchy that aren't links don't work #3

Closed mrpotatoes closed 4 years ago

mrpotatoes commented 5 years ago

If the parent in the markdown doesn't have a link it doesn't work.

Given this sidebar.md code

* Start Here
  * [Cheatsheet](start-here/cheatsheet.md)
  * [Diagram](start-here/diagram.md)
  * [About Currying & Composition](start-here/currying-composition.md)
  * [Glossary](start-here/glossary.md)
  * [How to read](start-here/how-to-read.md)
  * [Todo](start-here/todo.md)

It will not be clickable to open. I must make the parent be a link ala:

* [Start Here](start-here/cheatsheet.md)
mrpotatoes commented 4 years ago

This is the cheapest way to do it (for modern browsers) and get non-link items to work.

diff --git a/src/index.js b/src/index.js
index ddeba40..56d3205 100644
--- a/src/index.js
+++ b/src/index.js
@@ -59,8 +59,13 @@ document.addEventListener(
 )

 document.addEventListener('DOMContentLoaded', () => {
-  document.querySelector('.sidebar-nav').addEventListener('click',
+  document.querySelector('.sidebar-nav').addEventListener(
+    'click',
     e => {
+      if (e.target.tagName === 'P') {
+        e.target.parentElement.classList.toggle('open')
+      }
+
       if (e.target.tagName === 'A') {
         const elp = e.target.parentElement
         if (elp.tagName === 'LI') {
@@ -84,4 +89,4 @@ document.addEventListener('DOMContentLoaded', () => {
     },
     true
   )
-})
+})
\ No newline at end of file