From a31db4c7df89548dbc16a39011d45303a20778b8 Mon Sep 17 00:00:00 2001 From: M Bussonnier Date: Wed, 8 May 2024 06:41:33 -0700 Subject: [PATCH] Do not generate dropdown in sidebar. (#1771) * Do not generate dropdown in sidebar. This should fix #1735, it is complementary to #1564 but with a different approach. Instead of generating the same navbar as the fullpage width one, it generate one with no dropdowns, with this, any css that tries to make downtown looks like normal links in html becomes unnecessary. Co-authored-by: gabalafou * apply review * Add failing test for dropdown sidebar --------- Co-authored-by: gabalafou --- .../sections/sidebar-primary.html | 7 +++++- src/pydata_sphinx_theme/toctree.py | 22 +++++++++++++++---- tests/test_build.py | 9 ++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/sections/sidebar-primary.html b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/sections/sidebar-primary.html index af21e502d..378643bc4 100644 --- a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/sections/sidebar-primary.html +++ b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/sections/sidebar-primary.html @@ -6,7 +6,12 @@ {% if theme_navbar_center %} {% endif %} diff --git a/src/pydata_sphinx_theme/toctree.py b/src/pydata_sphinx_theme/toctree.py index c023d6cd7..a566f2e6b 100644 --- a/src/pydata_sphinx_theme/toctree.py +++ b/src/pydata_sphinx_theme/toctree.py @@ -2,7 +2,7 @@ from functools import cache from itertools import count -from typing import Iterator, List, Union +from typing import Iterator, List, Tuple, Union from urllib.parse import urlparse import sphinx @@ -101,8 +101,22 @@ def unique_html_id(base_id: str): return next(get_or_create_id_generator(base_id)) @cache - def generate_header_nav_before_dropdown(n_links_before_dropdown): - """The cacheable part.""" + def _generate_header_nav_before_dropdown( + n_links_before_dropdown, + ) -> Tuple[str, List[str]]: + """Return html for navbar and dropdown. + + .. warning:: + + Private helper function, do not call this directly. + + Given the number of links before the dropdown, return the html for the navbar, + as well as the list of links to put in a dropdown. + + Returns: + - HTML str for the navbar + - list of HTML str for the dropdown + """ try: n_links_before_dropdown = int(n_links_before_dropdown) except Exception: @@ -226,7 +240,7 @@ def generate_header_nav_html( n_links_before_dropdown:The number of links to show before nesting the remaining links in a Dropdown element. dropdown_text:Text of the dropdown element button. """ - out, links_dropdown = generate_header_nav_before_dropdown( + out, links_dropdown = _generate_header_nav_before_dropdown( n_links_before_dropdown ) diff --git a/tests/test_build.py b/tests/test_build.py index b71736037..3971531ea 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -349,6 +349,8 @@ def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None: } sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() index_html = sphinx_build.html_tree("index.html") + + # classic navbar: navbar = index_html.select("ul.bd-navbar-elements")[0] dropdowns = navbar.select("li.dropdown") standalone_links = navbar.select(".navbar-nav > li.nav-item:not(.dropdown)") @@ -362,6 +364,13 @@ def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None: # There should be no dropdown and only standalone links assert standalone_links and not dropdowns + # sidebar nav should never have dropdown + navbar = index_html.select("ul.bd-navbar-elements")[1] + dropdowns = navbar.select("li.dropdown") + standalone_links = navbar.select(".navbar-nav > li.nav-item:not(.dropdown)") + assert len(standalone_links) == 7 + assert len(dropdowns) == 0 + @pytest.mark.parametrize("dropdown_text", (None, "Other")) # None -> default "More" def test_navbar_header_dropdown_button(sphinx_build_factory, dropdown_text) -> None: