Skip to content

Commit 4e0ad3c

Browse files
authored
only show TOC if there are headers for it (jupyter-book#441)
only show TOC if there are headers for it
2 parents 6e1d8a1 + 7c8550f commit 4e0ad3c

File tree

1 file changed

+19
-11
lines changed
  • jupyter_book/book_template/assets/js/page

1 file changed

+19
-11
lines changed

jupyter_book/book_template/assets/js/page/tocbot.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@ const initToc = () => {
1111
document.querySelector('nav.onthispage').classList.add('no_sidebar_content')
1212
}
1313

14-
// Initialize the TOC bot
15-
tocbot.init({
16-
tocSelector: 'nav.onthispage',
17-
contentSelector: '.c-textbook__content',
18-
headingSelector: 'h1, h2, h3',
19-
orderedList: false,
20-
collapseDepth: 6,
21-
listClass: 'toc__menu',
22-
activeListItemClass: " ", // Not using, can't be empty
23-
activeLinkClass: " ", // Not using, can't be empty
24-
});
14+
// Initialize the TOC bot if we have TOC headers
15+
const tocContent = '.c-textbook__content';
16+
const tocHeaders = 'h1, h2, h3';
17+
var headers = document.querySelector(tocContent).querySelectorAll(tocHeaders);
18+
if (headers.length > 0) {
19+
document.querySelector('aside.sidebar__right').classList.remove('hidden');
20+
tocbot.init({
21+
tocSelector: 'nav.onthispage',
22+
contentSelector: tocContent,
23+
headingSelector: tocHeaders,
24+
orderedList: false,
25+
collapseDepth: 6,
26+
listClass: 'toc__menu',
27+
activeListItemClass: " ", // Not using, can't be empty
28+
activeLinkClass: " ", // Not using, can't be empty
29+
});
30+
} else {
31+
document.querySelector('aside.sidebar__right').classList.add('hidden');
32+
}
2533

2634
// Disable Turbolinks for TOC links
2735
document.querySelectorAll('.toc-list-item a')

0 commit comments

Comments
 (0)