diff --git a/pydata_sphinx_theme/__init__.py b/pydata_sphinx_theme/__init__.py index 19a5a3aaa..3cd067c29 100644 --- a/pydata_sphinx_theme/__init__.py +++ b/pydata_sphinx_theme/__init__.py @@ -99,13 +99,13 @@ def generate_toc_html(kind="html"): # Add toc-hN + visible classes def add_header_level_recursive(ul, level): + if ul is None: + return if level <= (context["theme_show_toc_level"] + 1): ul["class"] = ul.get("class", []) + ["visible"] for li in ul("li", recursive=False): li["class"] = li.get("class", []) + [f"toc-h{level}"] - ul = li.find("ul", recursive=False) - if ul: - add_header_level_recursive(ul, level + 1) + add_header_level_recursive(li.find("ul", recursive=False), level + 1) add_header_level_recursive(soup.find("ul"), 1) diff --git a/tests/sites/test_included_toc/conf.py b/tests/sites/test_included_toc/conf.py new file mode 100644 index 000000000..a788bae46 --- /dev/null +++ b/tests/sites/test_included_toc/conf.py @@ -0,0 +1,13 @@ +# -- Project information ----------------------------------------------------- + +project = "Test" +copyright = "Test" +author = "Test" + +# -- General configuration --------------------------------------------------- + +master_doc = "index" + +# -- Options for HTML output ------------------------------------------------- + +html_theme = "pydata_sphinx_theme" diff --git a/tests/sites/test_included_toc/included-page.rst b/tests/sites/test_included_toc/included-page.rst new file mode 100644 index 000000000..f8059d601 --- /dev/null +++ b/tests/sites/test_included_toc/included-page.rst @@ -0,0 +1,3 @@ +########## +Test page +########## diff --git a/tests/sites/test_included_toc/index.rst b/tests/sites/test_included_toc/index.rst new file mode 100644 index 000000000..835e503a2 --- /dev/null +++ b/tests/sites/test_included_toc/index.rst @@ -0,0 +1,8 @@ +######################## +ABC +######################## + +Test Content + +.. include:: toc-extension.include.rst + diff --git a/tests/sites/test_included_toc/toc-extension.include.rst b/tests/sites/test_included_toc/toc-extension.include.rst new file mode 100644 index 000000000..60ed2bd3e --- /dev/null +++ b/tests/sites/test_included_toc/toc-extension.include.rst @@ -0,0 +1,5 @@ +.. toctree:: + :hidden: + :maxdepth: 2 + + included-page diff --git a/tests/test_build.py b/tests/test_build.py index be461fa81..4157e030b 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -191,3 +191,16 @@ def test_sidebars_level2(sphinx_build_factory, file_regression): # Sidebar structure sidebar = subindex_html.select("nav#bd-docs-nav")[0] file_regression.check(sidebar.prettify(), extension=".html") + + +def test_included_toc(sphinx_build_factory): + """Test that Sphinx project containing TOC (.. toctree::) included + via .. include:: can be successfully built. + """ + # Regression test for bug resolved in #347. + # Tests mainly makes sure that the sphinx_build.build() does not raise exception. + # https://github.com/pydata/pydata-sphinx-theme/pull/347 + + sphinx_build = sphinx_build_factory("test_included_toc").build() + included_page_html = sphinx_build.html_tree("included-page.html") + assert included_page_html is not None