Skip to content

Commit 5201659

Browse files
committed
adding captions to left sidebar
1 parent 8a76bc8 commit 5201659

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

pydata_sphinx_theme/__init__.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def get_nav_object(maxdepth=None, collapse=True, **kwargs):
4141
toctree = TocTree(app.env).get_toctree_for(
4242
pagename, app.builder, collapse=collapse, maxdepth=maxdepth, **kwargs
4343
)
44+
4445
# If no toctree is defined (AKA a single-page site), skip this
4546
if toctree is None:
4647
return []
@@ -51,8 +52,14 @@ def get_nav_object(maxdepth=None, collapse=True, **kwargs):
5152
# <list_item classes="toctree-l1">
5253
# <list_item classes="toctree-l1">
5354
# `list_item`s are the actual TOC links and are the only thing we want
54-
toc_items = [item for child in toctree.children for item in child
55-
if isinstance(item, docutils.nodes.list_item)]
55+
toc_items = []
56+
for child in toctree.children:
57+
if isinstance(child, docutils.nodes.caption):
58+
toc_items.append(child)
59+
elif isinstance(child, docutils.nodes.bullet_list):
60+
for list_entries in child:
61+
if isinstance(list_entries, docutils.nodes.list_item):
62+
toc_items.append(list_entries)
5663

5764
# Now convert our docutils nodes into dicts that Jinja can use
5865
nav = [docutils_node_to_jinja(child, only_pages=True)
@@ -91,6 +98,12 @@ def docutils_node_to_jinja(list_item, only_pages=False):
9198
The TocTree, converted into a dictionary with key/values that work
9299
within Jinja.
93100
"""
101+
# If a caption, pass it through
102+
if isinstance(list_item, docutils.nodes.caption):
103+
nav = {"text": list_item.astext(), "type": "caption"}
104+
return nav
105+
106+
# Else, we assume it's a list item and need to parse the item content
94107
if not list_item.children:
95108
return None
96109

@@ -112,6 +125,7 @@ def docutils_node_to_jinja(list_item, only_pages=False):
112125
nav["title"] = title
113126
nav["url"] = url
114127
nav["active"] = active
128+
nav["type"] = "ref"
115129

116130
# Recursively convert children as well
117131
# If there are sub-pages for this list_item, there should be two children:

pydata_sphinx_theme/docs-sidebar.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
{% set nav = get_nav_object(maxdepth=3, collapse=True) %}
1111

1212
<ul class="nav bd-sidenav">
13+
<!-- top-level pages -->
1314
{% for main_nav_item in nav %}
1415
{% if main_nav_item.active %}
16+
<!-- sub-pages within each top level page -->
1517
{% for nav_item in main_nav_item.children %}
16-
{% if nav_item.children %}
18+
{% if nav_item["type"] == "caption" %}
19+
<li class="nav-caption">{{ nav_item.text }}</li>
20+
{% elif nav_item.children %}
1721

1822
<li class="{% if nav_item.active%}active{% endif %}">
1923
<a href="{{ nav_item.url }}">{{ nav_item.title }}</a>

0 commit comments

Comments
 (0)