Skip to content

Commit 27b8ca2

Browse files
authored
FIX secondary sidebar persists even if it is empty (#1688)
* FIX secondary sidebar removal via configuration * test when all templates are empty
1 parent 7de2c04 commit 27b8ca2

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
{%- extends "basic/layout.html" %}
88
{%- import "static/webpack-macros.html" as _webpack with context %}
99
{# A flag for whether we include a secondary sidebar based on the page metadata #}
10-
{% set remove_sidebar_secondary = (meta is defined and meta is not none and 'html_theme.sidebar_secondary.remove' in meta) or not theme_secondary_sidebar_items %}
10+
{# Note: secondary_sidebar_items is an array set by set_secondary_sidebar_items() in utils.py #}
11+
{% set remove_sidebar_secondary = (meta is defined and meta is not none and 'html_theme.sidebar_secondary.remove' in meta) or secondary_sidebar_items|length == 0 %}
1112
{%- block css %}
1213
{# The data-cfasync attribute disables CloudFlare's Rocket loader so that #}
1314
{# mode/theme are correctly set before the browser renders the page. #}

tests/test_build.py

+22
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,11 @@ def test_render_secondary_sidebar_dict(sphinx_build_factory) -> None:
10351035
assert not sphinx_build.html_tree("section1/index.html").select("div.sourcelink")
10361036
assert sphinx_build.html_tree("section2/index.html").select("div.sourcelink")
10371037

1038+
# Check that the secondary sidebar is completely removed for section1/index
1039+
assert not sphinx_build.html_tree("section1/index.html").select(
1040+
"div.bd-sidebar-secondary"
1041+
)
1042+
10381043

10391044
def test_render_secondary_sidebar_dict_glob_subdir(sphinx_build_factory) -> None:
10401045
"""Test that the secondary sidebar can be built with a dict of templates that globs a subdir."""
@@ -1142,3 +1147,20 @@ def test_role_main_for_search_highlights(sphinx_build_factory):
11421147
"""
11431148
sphinx_build = sphinx_build_factory("base").build()
11441149
assert sphinx_build.html_tree("index.html").select_one('[role="main"]')
1150+
1151+
1152+
def test_sidebar_secondary_templates_all_empty(sphinx_build_factory) -> None:
1153+
"""Test that the secondary sidebar is removed if all templates are empty."""
1154+
confoverrides = {
1155+
"html_theme_options": {
1156+
**COMMON_CONF_OVERRIDES,
1157+
"secondary_sidebar_items": ["page-toc", "sourcelink"],
1158+
},
1159+
"html_show_sourcelink": False,
1160+
}
1161+
1162+
# The page-toc component is empty because there is no in-page toc for page1
1163+
# The sourcelink component is empty because we disabled it in configuration
1164+
# Hence the secondary sidebar has all its templates empty and should be removed
1165+
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
1166+
assert not sphinx_build.html_tree("page1.html").select("div.bd-sidebar-secondary")

0 commit comments

Comments
 (0)