Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] sidebar item add collapse and expand #728

Closed
Wsine opened this issue Dec 28, 2018 · 10 comments
Closed

[feature request] sidebar item add collapse and expand #728

Wsine opened this issue Dec 28, 2018 · 10 comments

Comments

@Wsine
Copy link

Wsine commented Dec 28, 2018

Appreciate for the work of this project.

I know it seems rule to ask for such feature and maybe it is "impossible" to implement. But, how if user provides directories structure in _sidebar.md file.

for example _sidebar.md

- FolderName
  - [Article1](/FolderName/Article1.md)
  - [Article2](/FolderName/Article2.md)
- FolderName2
  - [Article3](/FolderName2/Article3.md)
  - [Article4](/FolderName2/Article4.md)

The original demand comes from that i want to construct my personal blog with doscify. However, the sidebar is as long as my article amount.

I found that i can write a bash script to help me scan my folders and then generate a sidebar file. such as
find . -mindepth 2 -name "*.md" | awk -F'/' 'BEGIN {RS=".md"} {arr[$2]=arr[$2]"\n - ["$3"](/"$2"/"$3")"} END {for (key in arr) print "- "key, arr[key]}' > _sidebar.md
what's more, travis ci can help us do such a job.

so, can docsify add a toggle feature for no hyperlink item in the sidebar? it is ok for adding it into configurable options.

@Wsine
Copy link
Author

Wsine commented Jul 11, 2019

I wrote a small solution this afternoon, although it may waste more calculation resource.

window.$docsify = {
  plugins: [
    function(hook, vm) {
      hook.doneEach(function() {
        document.querySelectorAll(".sidebar-nav > ul > li").forEach(
          function(node, index, nodelist) {
            var span = document.createElement("span");
            span.innerHTML = node.firstChild.data;
            span.style.cursor = "pointer";
            span.onclick = function(event) {
              var ul = event.target.nextElementSibling;
              if (ul.style.display === "none") {
                ul.style.display = "block";
              } else {
                ul.style.display = "none";
              }
            };
            node.firstChild.replaceWith(span);
            node.lastChild.style.display = "none";
        });
        var active = document.querySelector(".sidebar-nav li.active");
        if (active) {
          active.parentElement.style.display = "block";
        }
      });
    }
  ]
}

there is also a live demo. => https://wsine.github.io/blog

@w1024k
Copy link

w1024k commented Oct 29, 2019

当一章只有一级的时候,这章就不显示了

@kinm
Copy link

kinm commented Nov 26, 2019

当一章只有一级的时候,这章就不显示了

+1

@anikethsaha
Copy link
Member

I wrote a small solution this afternoon, although it may waste more calculation resource.

window.$docsify = {
  plugins: [
    function(hook, vm) {
      hook.doneEach(function() {
        document.querySelectorAll(".sidebar-nav > ul > li").forEach(
          function(node, index, nodelist) {
            var span = document.createElement("span");
            span.innerHTML = node.firstChild.data;
            span.style.cursor = "pointer";
            span.onclick = function(event) {
              var ul = event.target.nextElementSibling;
              if (ul.style.display === "none") {
                ul.style.display = "block";
              } else {
                ul.style.display = "none";
              }
            };
            node.firstChild.replaceWith(span);
            node.lastChild.style.display = "none";
        });
        var active = document.querySelector(".sidebar-nav li.active");
        if (active) {
          active.parentElement.style.display = "block";
        }
      });
    }
  ]
}

there is also a live demo. => https://wsine.github.io/blog

@Wsine Can you refactor this a bit as per your choice and submit a PR to add this plugin in the docs.
Publish this as a plugin and submit it in the docs

@Wsine
Copy link
Author

Wsine commented Dec 5, 2019

@anikethsaha Well, it seems that current version did not satisfy all the demands on collapsable sidebar. I will try my best when I have time.

@anikethsaha
Copy link
Member

Sure👍

@stale
Copy link

stale bot commented Feb 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Feb 4, 2020
@vagra
Copy link
Contributor

vagra commented Apr 19, 2020

very good!

view my site: https://kepan.org/docsify

i have many heading levels (up to 108), i want the sidebar items can be expand & collapse.

like Typora's side bar, that's very good! it can expand, collapse, and sync scroll.

@ediphy-dcottrell
Copy link

Is this still the best way to achieve this? I see various things kicking around when i search for it.

@paulhibbitts
Copy link
Collaborator

This looks to be a duplicate of issue #900, which also includes a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants