diff --git a/src/pydata_sphinx_theme/toctree.py b/src/pydata_sphinx_theme/toctree.py index cc0ef0c1c..586cacea5 100644 --- a/src/pydata_sphinx_theme/toctree.py +++ b/src/pydata_sphinx_theme/toctree.py @@ -71,14 +71,7 @@ def suppress_sidebar_toctree(startdepth: int = 1, **kwargs): ) if ancestorname is None: return True # suppress - if kwargs.get("includehidden", False): - # if ancestor is found and `includehidden=True` we're guaranteed there's a - # TocTree to be shown, so don't suppress - return False - - # we've found an ancestor page, but `includehidden=False` so we can't be sure if - # there's a TocTree fragment that should be shown on this page; unfortunately we - # must resolve the whole TOC subtree to find out + toctree = get_nonroot_toctree( app, pagename, ancestorname, toctree_obj, **kwargs ) diff --git a/tests/sites/test_primary_sidebar_toc/conf.py b/tests/sites/test_primary_sidebar_toc/conf.py new file mode 100644 index 000000000..713af624f --- /dev/null +++ b/tests/sites/test_primary_sidebar_toc/conf.py @@ -0,0 +1,18 @@ +"""Test conf file.""" + +# -- Project information ----------------------------------------------------- + +project = "PyData Tests" +copyright = "2020, Pydata community" +author = "Pydata community" + +root_doc = "index" + +# -- General configuration --------------------------------------------------- + +html_theme = "pydata_sphinx_theme" + +html_copy_source = True +html_sourcelink_suffix = "" + +html_theme_options = {"navigation_with_keys": False} diff --git a/tests/sites/test_primary_sidebar_toc/index.rst b/tests/sites/test_primary_sidebar_toc/index.rst new file mode 100644 index 000000000..3aaa44df8 --- /dev/null +++ b/tests/sites/test_primary_sidebar_toc/index.rst @@ -0,0 +1,7 @@ +test-primary-sidebar-toc +======================== + +.. toctree:: + + page + sec/index diff --git a/tests/sites/test_primary_sidebar_toc/page.rst b/tests/sites/test_primary_sidebar_toc/page.rst new file mode 100644 index 000000000..87ad822e9 --- /dev/null +++ b/tests/sites/test_primary_sidebar_toc/page.rst @@ -0,0 +1,5 @@ +============ +Page at Root +============ + +Some text. diff --git a/tests/sites/test_primary_sidebar_toc/sec/index.rst b/tests/sites/test_primary_sidebar_toc/sec/index.rst new file mode 100644 index 000000000..88f6f90c0 --- /dev/null +++ b/tests/sites/test_primary_sidebar_toc/sec/index.rst @@ -0,0 +1,10 @@ +=============== +Section at Root +=============== + +Some text. + +.. toctree:: + :hidden: + + subpage diff --git a/tests/sites/test_primary_sidebar_toc/sec/subpage.rst b/tests/sites/test_primary_sidebar_toc/sec/subpage.rst new file mode 100644 index 000000000..159000e17 --- /dev/null +++ b/tests/sites/test_primary_sidebar_toc/sec/subpage.rst @@ -0,0 +1,15 @@ +=============== +Page in Section +=============== + +Some text. + +Subsection +========== + +Some text. + +Subsubsection +------------- + +Some text. diff --git a/tests/test_build.py b/tests/test_build.py index d97316ddc..32388bcff 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -1142,3 +1142,23 @@ def test_role_main_for_search_highlights(sphinx_build_factory): """ sphinx_build = sphinx_build_factory("base").build() assert sphinx_build.html_tree("index.html").select_one('[role="main"]') + + +def test_primary_sidebar_hidden_when_empty(sphinx_build_factory) -> None: + """Test that the primary sidebar does not show up if it is empty.""" + sphinx_build = sphinx_build_factory("test_primary_sidebar_toc").build() + sidebar_primary = sphinx_build.html_tree("page.html").select( + "div.bd-sidebar-primary" + )[0] + assert "hide-on-wide" in sidebar_primary.attrs["class"] + + +def test_primary_sidebar_exist_when_hidden_toctree(sphinx_build_factory) -> None: + """Test that the primary sidebar shows up if there are hidden toctrees.""" + sphinx_build = sphinx_build_factory("test_primary_sidebar_toc").build() + for page in ("sec/index.html", "sec/subpage.html"): + sidebar_primary = sphinx_build.html_tree(page).select("div.bd-sidebar-primary")[ + 0 + ] + assert "hide-on-wide" not in sidebar_primary.attrs["class"] + assert "Page in Section" in str(sidebar_primary)