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

👌 IMPROVE: Making in-page TOC title configurable #299

Merged
merged 9 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ html_theme_options = {
}
```

## Control the right sidebar items

You can rename the title of the in-page table of contents, in the right sidebar:

```python
html_theme_options = {
"toc_title": "{your-title}"
}
```

The default value of the title is `Contents`.

## Control the left sidebar items

Expand Down
8 changes: 7 additions & 1 deletion sphinx_book_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,15 @@ def add_header_level_recursive(ul, level):
else:
toc_out = soup.prettify()

if not context["theme_toc_title"]:
raise ValueError(
"'toc_title' key cannot be empty. Please set a non-empty value."
)

out = f"""
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> { context["translate"]('Contents') }
<i class="fas fa-list"></i>
{ context["translate"](context["theme_toc_title"]) }
</div>
<nav id="bd-toc-nav">
{toc_out}
Expand Down
1 change: 1 addition & 0 deletions sphinx_book_theme/theme.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ use_download_button = True
use_repository_button = False
theme_dev_mode = False
show_navbar_depth = 1
toc_title = Contents
1 change: 1 addition & 0 deletions src/jinja/theme.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ use_issues_button = False
use_repository_button = False
theme_dev_mode = False
show_navbar_depth = 1
toc_title = Contents
1 change: 1 addition & 0 deletions src/jinja/theme.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ use_download_button = True
use_repository_button = False
theme_dev_mode = False
show_navbar_depth = 1
toc_title = Contents
8 changes: 8 additions & 0 deletions tests/sites/base/page1.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Page 1

Test content with <a href="https://google.com">Some raw HTML</a> to test.

## Section 1

First section

## Section 2

Second section
25 changes: 24 additions & 1 deletion tests/test_build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from pathlib import Path
from shutil import copytree
from shutil import copytree, rmtree
from subprocess import check_call

from bs4 import BeautifulSoup
Expand Down Expand Up @@ -300,3 +300,26 @@ def test_topbar_download_button_off(sphinx_build_factory, file_regression):
"div", attrs={"class": "topbar-main"}
)[0]
file_regression.check(source_btns.prettify(), extension=".html", encoding="utf8")


def test_right_sidebar_title(sphinx_build_factory, file_regression):
confoverrides = {"html_theme_options.toc_title": "My Contents"}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build(
assert_pass=True
)

sidebar_title = sphinx_build.html_tree("page1.html").find_all(
"div", attrs={"class": "tocsection"}
)[0]

file_regression.check(sidebar_title.prettify(), extension=".html", encoding="utf8")

# Testing the exception for empty title
rmtree(str(sphinx_build.src))

confoverrides = {"html_theme_options.toc_title": ""}

with pytest.raises(
ThemeError, match="key cannot be empty. Please set a non-empty value."
):
sphinx_build_factory("base", confoverrides=confoverrides).build()
2 changes: 1 addition & 1 deletion tests/test_build/escaped_description.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<meta content='Page 1 Test content with &lt;a href="https://google.com"&gt;Some raw HTML&lt;/a&gt; to test.' property="og:description"/>
<meta content='Page 1 Test content with &lt;a href="https://google.com"&gt;Some raw HTML&lt;/a&gt; to test. Section 1 First section Section 2 Second sectionSection 1 First sectionSe' property="og:description"/>
5 changes: 5 additions & 0 deletions tests/test_build/test_right_sidebar_title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list">
</i>
My Contents
</div>