Skip to content

Commit 25b64df

Browse files
authored
Add customizable TOC title class
Fixes #1293.
1 parent acdbdc1 commit 25b64df

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

.spell-dict

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Cogumbreiro
2828
CommonMark
2929
convertFile
3030
CSS
31+
customizable
3132
dedent
3233
deliminators
3334
deregister

docs/changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ links to the header, placing them after all other header content.
2525
* Add support for cPython version 3.12 (and PyPy 3.10) and drop support for
2626
Python version 3.7 (#1357).
2727
* Refactor changelog to use the format defined at <https://keepachangelog.com/>.
28-
* Updated the list of empty HTML tags (#1353).
28+
* Update the list of empty HTML tags (#1353).
29+
* Add customizable TOC title class to TOC extension (#1293).
2930

3031
## [3.4.4] -- 2023-07-25
3132

docs/extensions/toc.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ The following options are provided to configure the output:
151151
* **`title`**:
152152
Title to insert in the Table of Contents' `<div>`. Defaults to `None`.
153153

154+
* **`title_class`**:
155+
CSS class used for the title contained in the Table of Contents. Defaults to `toctitle`.
156+
154157
* **`toc_class`**:
155158
CSS class(es) used for the `<div>` containing the Table of Contents. Defaults to `toc`.
156159

markdown/extensions/toc.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def __init__(self, md, config):
161161
self.slugify = config["slugify"]
162162
self.sep = config["separator"]
163163
self.toc_class = config["toc_class"]
164+
self.title_class = config["title_class"]
164165
self.use_anchors = parseBoolValue(config["anchorlink"])
165166
self.anchorlink_class = config["anchorlink_class"]
166167
self.use_permalinks = parseBoolValue(config["permalink"], False)
@@ -251,7 +252,8 @@ def build_toc_div(self, toc_list):
251252
# Add title to the div
252253
if self.title:
253254
header = etree.SubElement(div, "span")
254-
header.attrib["class"] = "toctitle"
255+
if self.title_class:
256+
header.attrib["class"] = self.title_class
255257
header.text = self.title
256258

257259
def build_etree_ul(toc_list, parent):
@@ -335,6 +337,9 @@ def __init__(self, **kwargs):
335337
"title": ["",
336338
"Title to insert into TOC <div> - "
337339
"Defaults to an empty string"],
340+
"title_class": ['toctitle',
341+
'CSS class used for the title. '
342+
'Defaults to "toctitle"'],
338343
"toc_class": ['toc',
339344
'CSS class(es) used for the link. '
340345
'Defaults to "toclink"'],

tests/test_syntax/extensions/test_toc.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,45 @@ def testTOCWithCustomClasses(self):
629629
),
630630
extensions=[TocExtension(toc_class="custom1 custom2")]
631631
)
632+
633+
def testTOCWithEmptyTitleClass(self):
634+
635+
self.assertMarkdownRenders(
636+
self.dedent(
637+
'''
638+
[TOC]
639+
# Header
640+
'''
641+
),
642+
self.dedent(
643+
'''
644+
<div class="toc"><span>ToC</span><ul>
645+
<li><a href="#header">Header</a></li>
646+
</ul>
647+
</div>
648+
<h1 id="header">Header</h1>
649+
'''
650+
),
651+
extensions=[TocExtension(title_class="", title='ToC')]
652+
)
653+
654+
def testTOCWithCustomTitleClass(self):
655+
656+
self.assertMarkdownRenders(
657+
self.dedent(
658+
'''
659+
[TOC]
660+
# Header
661+
'''
662+
),
663+
self.dedent(
664+
'''
665+
<div class="toc"><span class="tocname">ToC</span><ul>
666+
<li><a href="#header">Header</a></li>
667+
</ul>
668+
</div>
669+
<h1 id="header">Header</h1>
670+
'''
671+
),
672+
extensions=[TocExtension(title_class="tocname", title='ToC')]
673+
)

0 commit comments

Comments
 (0)