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

FIX empty primary sidebar showing up #1682

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 0 additions & 4 deletions src/pydata_sphinx_theme/toctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

ancestors = [*_get_toctree_ancestors(app.env.toctree_includes, pagename)]
else:
ancestors = toctree.get_toctree_ancestors(pagename)

Check warning on line 42 in src/pydata_sphinx_theme/toctree.py

View check run for this annotation

Codecov / codecov/patch

src/pydata_sphinx_theme/toctree.py#L42

Added line #L42 was not covered by tests
try:
out = ancestors[-startdepth]
except IndexError:
Expand Down Expand Up @@ -71,10 +71,6 @@
)
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
Charlie-XIAO marked this conversation as resolved.
Show resolved Hide resolved
# there's a TocTree fragment that should be shown on this page; unfortunately we
Expand Down Expand Up @@ -273,14 +269,14 @@
HTML string (if kind == "sidebar") OR BeautifulSoup object (if kind == "raw")
"""
if startdepth == 0:
html_toctree = context["toctree"](**kwargs)

Check warning on line 272 in src/pydata_sphinx_theme/toctree.py

View check run for this annotation

Codecov / codecov/patch

src/pydata_sphinx_theme/toctree.py#L272

Added line #L272 was not covered by tests
else:
# find relevant ancestor page; some pages (search, genindex) won't have one
ancestorname, toctree_obj = _get_ancestor_pagename(
app=app, pagename=pagename, startdepth=startdepth
)
if ancestorname is None:
raise RuntimeError(

Check warning on line 279 in src/pydata_sphinx_theme/toctree.py

View check run for this annotation

Codecov / codecov/patch

src/pydata_sphinx_theme/toctree.py#L279

Added line #L279 was not covered by tests
"Template requested to generate a TocTree fragment but no suitable "
"ancestor found to act as root node. Please report this to theme "
"developers."
Expand Down Expand Up @@ -522,5 +518,5 @@
# ... and merge them into a single entity
result = toctrees[0]
for resolved_toctree in toctrees[1:]:
result.extend(resolved_toctree.children)

Check warning on line 521 in src/pydata_sphinx_theme/toctree.py

View check run for this annotation

Codecov / codecov/patch

src/pydata_sphinx_theme/toctree.py#L521

Added line #L521 was not covered by tests
return result
18 changes: 18 additions & 0 deletions tests/sites/test_primary_sidebar_toc/conf.py
Original file line number Diff line number Diff line change
@@ -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}
7 changes: 7 additions & 0 deletions tests/sites/test_primary_sidebar_toc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test-primary-sidebar-toc
========================

.. toctree::

page
sec/index
5 changes: 5 additions & 0 deletions tests/sites/test_primary_sidebar_toc/page.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
============
Page at Root
============

Some text.
10 changes: 10 additions & 0 deletions tests/sites/test_primary_sidebar_toc/sec/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
===============
Section at Root
===============

Some text.

.. toctree::
:hidden:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test case is at all dependent on the change made in this PR, but it seems like a nice test case to have in the test suite. 😄


subpage
15 changes: 15 additions & 0 deletions tests/sites/test_primary_sidebar_toc/sec/subpage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
===============
Page in Section
===============

Some text.

Subsection
==========

Some text.

Subsubsection
-------------

Some text.
20 changes: 20 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,3 +1173,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)
Loading