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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>
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
7 changes: 5 additions & 2 deletions src/pydata_sphinx_theme/toctree.py
Expand Up @@ -36,7 +36,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 +55,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 +152,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}
AlenkaF marked this conversation as resolved.
Show resolved Hide resolved
</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
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