diff --git a/.spell-dict b/.spell-dict index 87198833b..91cd7bd10 100644 --- a/.spell-dict +++ b/.spell-dict @@ -28,6 +28,7 @@ Cogumbreiro CommonMark convertFile CSS +customizable dedent deliminators deregister diff --git a/docs/changelog.md b/docs/changelog.md index 5d4e800f3..d233abfda 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -25,7 +25,8 @@ links to the header, placing them after all other header content. * Add support for cPython version 3.12 (and PyPy 3.10) and drop support for Python version 3.7 (#1357). * Refactor changelog to use the format defined at . -* Updated the list of empty HTML tags (#1353). +* Update the list of empty HTML tags (#1353). +* Add customizable TOC title class to TOC extension (#1293). ## [3.4.4] -- 2023-07-25 diff --git a/docs/extensions/toc.md b/docs/extensions/toc.md index f8d747902..1f80c7ea6 100644 --- a/docs/extensions/toc.md +++ b/docs/extensions/toc.md @@ -151,6 +151,9 @@ The following options are provided to configure the output: * **`title`**: Title to insert in the Table of Contents' `
`. Defaults to `None`. +* **`title_class`**: + CSS class used for the title contained in the Table of Contents. Defaults to `toctitle`. + * **`toc_class`**: CSS class(es) used for the `
` containing the Table of Contents. Defaults to `toc`. diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 33746d5e6..6d080deb7 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -161,6 +161,7 @@ def __init__(self, md, config): self.slugify = config["slugify"] self.sep = config["separator"] self.toc_class = config["toc_class"] + self.title_class = config["title_class"] self.use_anchors = parseBoolValue(config["anchorlink"]) self.anchorlink_class = config["anchorlink_class"] self.use_permalinks = parseBoolValue(config["permalink"], False) @@ -251,7 +252,8 @@ def build_toc_div(self, toc_list): # Add title to the div if self.title: header = etree.SubElement(div, "span") - header.attrib["class"] = "toctitle" + if self.title_class: + header.attrib["class"] = self.title_class header.text = self.title def build_etree_ul(toc_list, parent): @@ -335,6 +337,9 @@ def __init__(self, **kwargs): "title": ["", "Title to insert into TOC
- " "Defaults to an empty string"], + "title_class": ['toctitle', + 'CSS class used for the title. ' + 'Defaults to "toctitle"'], "toc_class": ['toc', 'CSS class(es) used for the link. ' 'Defaults to "toclink"'], diff --git a/tests/test_syntax/extensions/test_toc.py b/tests/test_syntax/extensions/test_toc.py index d64c6d391..797643643 100644 --- a/tests/test_syntax/extensions/test_toc.py +++ b/tests/test_syntax/extensions/test_toc.py @@ -629,3 +629,45 @@ def testTOCWithCustomClasses(self): ), extensions=[TocExtension(toc_class="custom1 custom2")] ) + + def testTOCWithEmptyTitleClass(self): + + self.assertMarkdownRenders( + self.dedent( + ''' + [TOC] + # Header + ''' + ), + self.dedent( + ''' +
ToC +
+

Header

+ ''' + ), + extensions=[TocExtension(title_class="", title='ToC')] + ) + + def testTOCWithCustomTitleClass(self): + + self.assertMarkdownRenders( + self.dedent( + ''' + [TOC] + # Header + ''' + ), + self.dedent( + ''' +
ToC +
+

Header

+ ''' + ), + extensions=[TocExtension(title_class="tocname", title='ToC')] + )