Use the closest section when clicking on a headerlink that isn't in the sidebar

This commit is contained in:
Mathieu Agopian 2016-09-30 16:25:31 +02:00 committed by Anthony Johnson
parent 92ac72952f
commit afaf0dc9bd
4 changed files with 28 additions and 2 deletions

View File

@ -32,6 +32,7 @@ extensions = [
'sphinx.ext.autodoc', 'sphinx.ext.autodoc',
'sphinx.ext.mathjax', 'sphinx.ext.mathjax',
'sphinx.ext.viewcode', 'sphinx.ext.viewcode',
'sphinxcontrib.httpdomain',
] ]
# Math # Math

View File

@ -20,7 +20,7 @@ Contents:
list list
.. toctree:: .. toctree::
:titlesonly: :maxdepth: 2
toc toc

View File

@ -9,4 +9,16 @@ Two
--- ---
Three Three
````` .....
Four //five
```````````
.. http:post:: /foobar
:synopsis: Some synopsis
Some content

View File

@ -97,6 +97,19 @@ function ThemeNav () {
try { try {
var link = $('.wy-menu-vertical') var link = $('.wy-menu-vertical')
.find('[href="' + anchor + '"]'); .find('[href="' + anchor + '"]');
// If we didn't find a link, it may be because we clicked on
// something that is not in the sidebar (eg: when using
// sphinxcontrib.httpdomain it generates headerlinks but those
// aren't picked up and placed in the toctree). So let's find
// the closest header in the document and try with that one.
if (link.length === 0) {
var doc_link = $('.document a[href="' + anchor + '"]');
var closest_section = doc_link.closest('div.section');
// Try again with the closest section entry.
link = $('.wy-menu-vertical')
.find('[href="#' + closest_section.attr("id") + '"]');
}
$('.wy-menu-vertical li.toctree-l1 li.current') $('.wy-menu-vertical li.toctree-l1 li.current')
.removeClass('current'); .removeClass('current');
link.closest('li.toctree-l2').addClass('current'); link.closest('li.toctree-l2').addClass('current');