Skip to content
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

Add dropdown_text argument to generate_header_nav_html #1423

Merged
merged 3 commits into from
Sep 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
{{ _("Site Navigation") }}
</p>
<ul class="bd-navbar-elements navbar-nav">
{{ generate_header_nav_html(n_links_before_dropdown=theme_header_links_before_dropdown) }}
{{ generate_header_nav_html(n_links_before_dropdown=theme_header_links_before_dropdown, dropdown_text=theme_header_dropdown_text) }}
</ul>
</nav>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ show_nav_level = 1
show_toc_level = 1
navbar_align = content
header_links_before_dropdown = 5
header_dropdown_text = More
switcher =
check_switcher = True
pygment_light_style = a11y-high-contrast-light
Expand Down
8 changes: 6 additions & 2 deletions src/pydata_sphinx_theme/toctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sphinx.addnodes import toctree as toctree_node
from sphinx.application import Sphinx
from sphinx.environment.adapters.toctree import TocTree
from sphinx.locale import _

from .utils import traverse_or_findall

Expand All @@ -36,7 +37,9 @@ def add_toctree_functions(
"""Add functions so Jinja templates can add toctree objects."""

@lru_cache(maxsize=None)
def generate_header_nav_html(n_links_before_dropdown: int = 5) -> str:
def generate_header_nav_html(
n_links_before_dropdown: int = 5, dropdown_text: str = "More"
) -> str:
"""Generate top-level links that are meant for the header navigation.

We use this function instead of the TocTree-based one used for the
Expand All @@ -53,6 +56,7 @@ def generate_header_nav_html(n_links_before_dropdown: int = 5) -> str:

Parameters:
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.
"""
try:
n_links_before_dropdown = int(n_links_before_dropdown)
Expand Down Expand Up @@ -149,7 +153,7 @@ def generate_header_nav_html(n_links_before_dropdown: int = 5) -> str:
out += f"""
<li class="nav-item dropdown">
<button class="btn dropdown-toggle nav-item" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-controls="pst-header-nav-more-links">
More
{_(dropdown_text)}
</button>
<ul id="pst-header-nav-more-links" class="dropdown-menu">
{links_dropdown_html}
Expand Down
28 changes: 28 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,34 @@ def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None:
assert standalone_links and not dropdowns


@pytest.mark.parametrize("dropdown_text", (None, "Other")) # None -> default "More"
def test_navbar_header_dropdown_button(sphinx_build_factory, dropdown_text) -> None:
"""Test whether dropdown button text is configurable."""
if dropdown_text:
confoverrides = {
"html_theme_options": {
"header_links_before_dropdown": 2,
"header_dropdown_text": dropdown_text,
}
}
else:
confoverrides = {
"html_theme_options": {
"header_links_before_dropdown": 2,
}
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
navbar = index_html.select("ul.bd-navbar-elements")[0]
button_text = str(navbar.select("button")[0])
if dropdown_text:
# The dropdown text should be "Other"
assert dropdown_text in button_text
else:
# The dropdown text should be "More"
assert "More" in button_text


def test_sidebars_captions(sphinx_build_factory, file_regression) -> None:
"""Test that the captions are rendered."""
sphinx_build = sphinx_build_factory("sidebars").build()
Expand Down
Loading