Skip to content

Commit d70d894

Browse files
🐛 Fix in-page headers incorrectly appearing in navbar (#307)
1 parent a19d244 commit d70d894

File tree

7 files changed

+71
-7
lines changed

7 files changed

+71
-7
lines changed

pydata_sphinx_theme/__init__.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ def generate_nav_html(kind, **kwargs):
4141
for li in soup("li", {"class": "current"}):
4242
li["class"].append("active")
4343

44+
# Remove navbar/sidebar links to sub-headers on the page
45+
for li in soup.select("li"):
46+
# Remove
47+
if li.find("a"):
48+
href = li.find("a")["href"]
49+
if "#" in href and href != "#":
50+
li.decompose()
51+
4452
if kind == "navbar":
4553
# Add CSS for bootstrap
4654
for li in soup("li"):
@@ -49,13 +57,6 @@ def generate_nav_html(kind, **kwargs):
4957
out = "\n".join([ii.prettify() for ii in soup.find_all("li")])
5058

5159
elif kind == "sidebar":
52-
# Remove sidebar links to sub-headers on the page
53-
for li in soup.select("li.current ul li"):
54-
# Remove
55-
if li.find("a"):
56-
href = li.find("a")["href"]
57-
if "#" in href and href != "#":
58-
li.decompose()
5960

6061
# Join all the top-level `li`s together for display
6162
current_lis = soup.select("li.current.toctree-l1 li.toctree-l2")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -- Project information -----------------------------------------------------
2+
3+
project = "PyData Tests"
4+
copyright = "2020, Pydata community"
5+
author = "Pydata community"
6+
7+
master_doc = "index"
8+
9+
# -- General configuration ---------------------------------------------------
10+
11+
html_theme = "pydata_sphinx_theme"
12+
13+
html_copy_source = True
14+
html_sourcelink_suffix = ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test-navbar-in-page-headers
2+
===========================
3+
4+
.. toctree::
5+
6+
page1
7+
page2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Page 1
2+
======
3+
4+
Some text
5+
6+
Another header
7+
==============
8+
9+
Some text
10+
11+
Sub-header
12+
----------
13+
14+
Some text
15+
16+
And another one
17+
===============
18+
19+
Some text
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Page 2
2+
======

tests/test_build.py

+9
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,12 @@ def test_navbar_align_right(sphinx_build_factory):
145145
index_html = sphinx_build.html_tree("index.html")
146146
assert "col-lg-9" not in index_html.select("div#navbar-menu")[0].attrs["class"]
147147
assert "ml-auto" in index_html.select("ul#navbar-main-elements")[0].attrs["class"]
148+
149+
150+
def test_navbar_no_in_page_headers(sphinx_build_factory, file_regression):
151+
# https://github.com/pandas-dev/pydata-sphinx-theme/issues/302
152+
sphinx_build = sphinx_build_factory("test_navbar_no_in_page_headers").build()
153+
154+
index_html = sphinx_build.html_tree("index.html")
155+
navbar = index_html.select("ul#navbar-main-elements")[0]
156+
file_regression.check(navbar.prettify(), extension=".html")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<ul class="navbar-nav mr-auto" id="navbar-main-elements">
2+
<li class="toctree-l1 nav-item">
3+
<a class="reference internal nav-link" href="page1.html">
4+
Page 1
5+
</a>
6+
</li>
7+
<li class="toctree-l1 nav-item">
8+
<a class="reference internal nav-link" href="page2.html">
9+
Page 2
10+
</a>
11+
</li>
12+
</ul>

0 commit comments

Comments
 (0)