tscanlin / tocbot

Build a table of contents from headings in an HTML document.
MIT License
1.39k stars 115 forks source link

How can I let the Headings of tocbot indent to its corresponding position? #289

Closed Norlandz closed 1 year ago

Norlandz commented 1 year ago

Question

How can I let the Headings of tocbot indent to its corresponding position?

Problem eg

normal situation:

(There is nothing wrong with this example, tocbot is behaving normally)

heading1
  heading2
    heading3 
  heading2 
    heading3 // <- this will be removed
      heading4
    heading3

problematic situation

Say, now the heading4 no longer follows a heading3 in the html file. Then here is the problem, the heading4 here is indented to same alignment as the below heading3. (Though this is common in some of the Toc design. ) How can I make the heading4 indent back to 1 more indentation righter than heading3?

current behavior:

heading1
  heading2
    heading3 
  heading2 
    heading4 // <- problem 
    heading3

expecting behavior:

heading1
  heading2
    heading3 
  heading2 
      heading4 // <- expect such indentation
    heading3
tscanlin commented 1 year ago

Hey, thanks for bringing this up. Accomplishing what you are describing is not currently possible as tocbot doesn't handle heading margins like that right now. However, using some custom custom CSS you may be able to accomplish this. something like: .node-name--H3 ~ .toc-list .node-name--H5 { margin-left: 10px } Hopefully that is helpful. I'd also be open to a contribution to add this feature if you are able to implement it.

Norlandz commented 1 year ago

Spent couple days to implement it. Not the perfect. You may (or may not) need to look through to fix some of it.

Added additional features (categorized into 3, see comments inside), (so it turns out become much larger than I thought...)


To use the script. Paste below js code into tocbot.js at proper location, this should replaces some of the old codes. (The old feature will lost after replacing the old code.)

Read the comments inside for much clearer explanation.

tscanlin commented 1 year ago

Thanks for working on this!

The code looks like a lot and a bit hard for me to follow so I don't plan to include it but feel free to make another library or a blog article to discuss it and share it with the world. I took a try at expanding on the CSS way I mentioned earlier and came up with this:

.toc-link.node-name--H1 ~ .toc-list .toc-list-item .toc-link.node-name--H3,
.toc-link.node-name--H2 ~ .toc-list .toc-list-item .toc-link.node-name--H4,
.toc-link.node-name--H3 ~ .toc-list .toc-list-item .toc-link.node-name--H5 {
  margin-left: 10px;
}
Screenshot 2023-01-19 at 4 30 43 PM

It should handle the display the way you described without adjusting the nesting logic or re-writing so much code. This could easily be expanded upon to support deeper nesting levels, but I'm not sure how common those cases are or if it makes sense to add code for them. Open to it though! Let me know what you think.

tscanlin commented 1 year ago

Closing this but feel free to comment if you are still having issues.