Skip to content

Commit 15819a8

Browse files
authored
ENH: add show_toc_level config option to choose visible levels of toc (#256)
1 parent 7163801 commit 15819a8

File tree

11 files changed

+57
-6
lines changed

11 files changed

+57
-6
lines changed

docs/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"github_url": "https://github.com/pandas-dev/pydata-sphinx-theme",
6666
"twitter_url": "https://twitter.com/pandas_dev",
6767
"use_edit_page_button": True,
68+
"show_toc_level": 1,
6869
}
6970

7071
html_context = {

docs/user_guide/configuring.rst

+19
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,22 @@ use the following configuration:
150150
html_theme_options = {
151151
"navigation_with_keys": False
152152
}
153+
154+
155+
Show more levels of the in-page TOC by default
156+
==============================================
157+
158+
Normally only the 2nd-level headers of a page are show in the right
159+
table of contents, and deeper levels are only shown when they are part
160+
of an active section (when it is scrolled on screen).
161+
162+
You can show deeper levels by default by using the following configuration, indicating how many levels should be displayed:
163+
164+
.. code-block:: python
165+
166+
html_theme_options = {
167+
"show_toc_level": 2
168+
}
169+
170+
All headings up to and including the level specified will now be shown
171+
regardless of what is displayed on the page.

pydata_sphinx_theme/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ def get_edit_url():
173173

174174
context["get_edit_url"] = get_edit_url
175175

176+
# Ensure that the max TOC level is an integer
177+
context["theme_show_toc_level"] = int(context.get("theme_show_toc_level", 1))
178+
176179

177180
# -----------------------------------------------------------------------------
178181

pydata_sphinx_theme/docs-toc.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<li class="nav-item toc-entry toc-h{{ loop.depth + 1 }}">
1313
<a href="{{ item.url }}" class="nav-link">{{ item.title }}</a>
1414
{%- if item.children -%}
15-
<ul class="nav section-nav flex-column">
15+
<ul class="nav section-nav flex-column{% if (loop.depth + 1) <= theme_show_toc_level %} visible{% endif %}">
1616
{{ loop(item.children) }}
1717
</ul>
1818
{%- endif %}

pydata_sphinx_theme/static/css/index.689d8a1e63c34b0b2966dc009a5ecd08.css renamed to pydata_sphinx_theme/static/css/index.73d71520a4ca3b99cfee5594769eaaae.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pydata_sphinx_theme/static/webpack-macros.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
{% endmacro %}
1717

1818
{% macro head_pre_bootstrap() %}
19-
<link rel="stylesheet" href="{{ pathto('_static/css/index.689d8a1e63c34b0b2966dc009a5ecd08.css', 1) }}">
19+
<link rel="stylesheet" href="{{ pathto('_static/css/index.73d71520a4ca3b99cfee5594769eaaae.css', 1) }}">
2020
{% endmacro %}
2121

2222
{% macro head_js_preload() %}
23-
<link rel="preload" as="script" href="{{ pathto('_static/js/index.0c807b2b8646875702ce.js', 1) }}">
23+
<link rel="preload" as="script" href="{{ pathto('_static/js/index.3da636dd464baa7582d2.js', 1) }}">
2424
{% endmacro %}
2525

2626
{% macro body_post() %}
27-
<script src="{{ pathto('_static/js/index.0c807b2b8646875702ce.js', 1) }}"></script>
27+
<script src="{{ pathto('_static/js/index.3da636dd464baa7582d2.js', 1) }}"></script>
2828
{% endmacro %}

pydata_sphinx_theme/theme.conf

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ google_analytics_id =
1313
show_prev_next = True
1414
search_bar_text = Search the docs ...
1515
search_bar_position = sidebar
16-
navigation_with_keys = True
16+
navigation_with_keys = True
17+
show_toc_level = 1

src/scss/index.scss

+6
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,14 @@ table.field-list {
350350
.bd-toc .nav {
351351
.nav {
352352
display: none;
353+
354+
// So we can manually specify a level as visible in the config
355+
&.visible {
356+
display: block;
357+
}
353358
}
354359

360+
355361
> .active > ul {
356362
display: block;
357363
}

tests/sites/base/index.rst

+9
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ Index ``with code`` in title
88
page1
99
page2
1010
section1/index
11+
12+
A header
13+
--------
14+
15+
A sub-header
16+
~~~~~~~~~~~~
17+
18+
A sub-sub-header
19+
````````````````

tests/test_build.py

+12
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,15 @@ def test_build_book(file_regression, sphinx_build):
6868
)
6969

7070
sphinx_build.clean()
71+
72+
73+
def test_toc_visibility(file_regression, sphinx_build):
74+
sphinx_build.copy()
75+
76+
# Test that setting TOC level visibility works as expected
77+
sphinx_build.build(["-D", "html_theme_options.show_toc_level=2"])
78+
index_html = sphinx_build.get("index.html")
79+
80+
# The 3rd level headers should be visible, but not the fourth-level
81+
assert "visible" in index_html.select(".toc-h2 ul")[0].attrs["class"]
82+
assert "visible" not in index_html.select(".toc-h3 ul")[0].attrs["class"]

0 commit comments

Comments
 (0)