Skip to content

Commit 84f87b3

Browse files
committed
Auto-detect TOC heading selectors in tocbot
1 parent 6d11014 commit 84f87b3

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/main/resources/docinfo/noorm-documentation/index-docinfo-footer.html

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,54 @@
2020
<script src="script/tocbot-3.0.2/tocbot.min.js"></script>
2121
<script>
2222
/* Tocbot dynamic TOC, works with tocbot 3.0.2 */
23-
/* Source: https://github.com/asciidoctor/asciidoctor/issues/699#issuecomment-321066006 */
23+
/* Source: https://github.com/asciidoctor/asciidoctor/issues/699#issuecomment-321066006
24+
* Adapted to auto-detect the heading selector.
25+
*/
2426
var oldtoc = document.getElementById('toctitle').nextElementSibling;
27+
28+
var headingSelectorVal;
29+
if (oldtoc.querySelector('.sectlevel5')) {
30+
headingSelectorVal = 'h1, h2, h3, h4, h5, h6'
31+
}
32+
else if (oldtoc.querySelector('.sectlevel4')) {
33+
headingSelectorVal = 'h1, h2, h3, h4, h5'
34+
}
35+
else if (oldtoc.querySelector('.sectlevel3')) {
36+
headingSelectorVal = 'h1, h2, h3, h4'
37+
}
38+
else if (oldtoc.querySelector('.sectlevel2')) {
39+
headingSelectorVal = 'h1, h2, h3'
40+
}
41+
else if (oldtoc.querySelector('.sectlevel1')) {
42+
headingSelectorVal = 'h1, h2'
43+
}
44+
else {
45+
// In case something went wrong
46+
headingSelectorVal = 'h1, h2, h3, h4'
47+
}
48+
2549
var newtoc = document.createElement('div');
2650
newtoc.setAttribute('id', 'tocbot');
2751
newtoc.setAttribute('class', 'js-toc');
2852
oldtoc.parentNode.replaceChild(newtoc, oldtoc);
2953
tocbot.init({ contentSelector: '#content',
30-
headingSelector: 'h1, h2, h3, h4',
54+
headingSelector: headingSelectorVal,
3155
smoothScroll: false });
3256
var handleTocOnResize = function() {
3357
var width = window.innerWidth
3458
|| document.documentElement.clientWidth
3559
|| document.body.clientWidth;
3660
if (width < 768) {
3761
tocbot.refresh({ contentSelector: '#content',
38-
headingSelector: 'h1, h2, h3, h4',
62+
headingSelector: headingSelectorVal,
3963
collapseDepth: 6,
4064
activeLinkClass: 'ignoreactive',
4165
throttleTimeout: 1000,
4266
smoothScroll: false });
4367
}
4468
else {
4569
tocbot.refresh({ contentSelector: '#content',
46-
headingSelector: 'h1, h2, h3, h4',
70+
headingSelector: headingSelectorVal,
4771
smoothScroll: false });
4872
}
4973
};

src/main/resources/docinfo/noorm-documentation/index-docinfo.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
}
2323
@media print{
2424
#tocbot a.toc-link.node-name--H4{ display:none }
25+
#tocbot a.toc-link.node-name--H5{ display:none }
26+
#tocbot a.toc-link.node-name--H6{ display:none }
2527
}
2628
</style>
2729
<!-- /Dynamic TOC -->

0 commit comments

Comments
 (0)