Skip to content

🐛 Fix in-page headers incorrectly appearing in navbar #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ def generate_nav_html(kind, **kwargs):
for li in soup("li", {"class": "current"}):
li["class"].append("active")

# Remove navbar/sidebar links to sub-headers on the page
for li in soup.select("li"):
# Remove
if li.find("a"):
href = li.find("a")["href"]
if "#" in href and href != "#":
li.decompose()

if kind == "navbar":
# Add CSS for bootstrap
for li in soup("li"):
Expand All @@ -49,13 +57,6 @@ def generate_nav_html(kind, **kwargs):
out = "\n".join([ii.prettify() for ii in soup.find_all("li")])

elif kind == "sidebar":
# Remove sidebar links to sub-headers on the page
for li in soup.select("li.current ul li"):
# Remove
if li.find("a"):
href = li.find("a")["href"]
if "#" in href and href != "#":
li.decompose()

# Join all the top-level `li`s together for display
current_lis = soup.select("li.current.toctree-l1 li.toctree-l2")
Expand Down
14 changes: 14 additions & 0 deletions tests/sites/test_navbar_no_in_page_headers/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -- Project information -----------------------------------------------------

project = "PyData Tests"
copyright = "2020, Pydata community"
author = "Pydata community"

master_doc = "index"

# -- General configuration ---------------------------------------------------

html_theme = "pydata_sphinx_theme"

html_copy_source = True
html_sourcelink_suffix = ""
7 changes: 7 additions & 0 deletions tests/sites/test_navbar_no_in_page_headers/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test-navbar-in-page-headers
===========================

.. toctree::

page1
page2
19 changes: 19 additions & 0 deletions tests/sites/test_navbar_no_in_page_headers/page1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Page 1
======

Some text

Another header
==============

Some text

Sub-header
----------

Some text

And another one
===============

Some text
2 changes: 2 additions & 0 deletions tests/sites/test_navbar_no_in_page_headers/page2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Page 2
======
9 changes: 9 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,12 @@ def test_navbar_align_right(sphinx_build_factory):
index_html = sphinx_build.html_tree("index.html")
assert "col-lg-9" not in index_html.select("div#navbar-menu")[0].attrs["class"]
assert "ml-auto" in index_html.select("ul#navbar-main-elements")[0].attrs["class"]


def test_navbar_no_in_page_headers(sphinx_build_factory, file_regression):
# https://github.com/pandas-dev/pydata-sphinx-theme/issues/302
sphinx_build = sphinx_build_factory("test_navbar_no_in_page_headers").build()

index_html = sphinx_build.html_tree("index.html")
navbar = index_html.select("ul#navbar-main-elements")[0]
file_regression.check(navbar.prettify(), extension=".html")
12 changes: 12 additions & 0 deletions tests/test_build/test_navbar_no_in_page_headers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ul class="navbar-nav mr-auto" id="navbar-main-elements">
<li class="toctree-l1 nav-item">
<a class="reference internal nav-link" href="page1.html">
Page 1
</a>
</li>
<li class="toctree-l1 nav-item">
<a class="reference internal nav-link" href="page2.html">
Page 2
</a>
</li>
</ul>