diff options
author | Jannis Vajen <jvajen@gmail.com> | 2022-05-05 21:53:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-05 15:53:38 -0400 |
commit | 93d17b9cc4a3f53dc2059e44a691a28797422d21 (patch) | |
tree | 208bcc0ef29db5f4125bbaf2fa6ef7fd054c7ffe | |
parent | efec51ac92059fc5b72f08832b481919a3ded6f3 (diff) | |
download | python-markdown-93d17b9cc4a3f53dc2059e44a691a28797422d21.tar.gz |
Support custom CSS class on TOC element
Closes #1224
-rw-r--r-- | docs/change_log/release-3.4.md | 4 | ||||
-rw-r--r-- | docs/extensions/toc.md | 3 | ||||
-rw-r--r-- | markdown/extensions/toc.py | 6 | ||||
-rw-r--r-- | mkdocs.yml | 1 | ||||
-rw-r--r-- | tests/test_syntax/extensions/test_toc.py | 43 |
5 files changed, 56 insertions, 1 deletions
diff --git a/docs/change_log/release-3.4.md b/docs/change_log/release-3.4.md index 71f9a20..025d443 100644 --- a/docs/change_log/release-3.4.md +++ b/docs/change_log/release-3.4.md @@ -51,6 +51,10 @@ The following new features have been included in the 3.3 release: Ex: `[{}]` will give <sup>[1]</sup>, `({})` will give <sup>(1)</sup>, or just by default, the current behavior: <sup>1</sup>. +* The [Table of Contents](../extensions/toc.md) extension now accepts a `toc_class` + parameter which can be used to set the CSS class(es) on the `<div>` that contains the + Table of Contents (#1224). + ## Bug fixes The following bug fixes are included in the 3.4 release: diff --git a/docs/extensions/toc.md b/docs/extensions/toc.md index 8dce335..8d7bd35 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' `<div>`. Defaults to `None`. +* **`toc_class`**: + CSS class(es) used for the `<div>` containing the Table of Contents. Defaults to `toc`. + * **`anchorlink`**: Set to `True` to cause all headers to link to themselves. Default is `False`. diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 57d2e3b..80138b3 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -160,6 +160,7 @@ class TocTreeprocessor(Treeprocessor): self.base_level = int(config["baselevel"]) - 1 self.slugify = config["slugify"] self.sep = config["separator"] + self.toc_class = config["toc_class"] self.use_anchors = parseBoolValue(config["anchorlink"]) self.anchorlink_class = config["anchorlink_class"] self.use_permalinks = parseBoolValue(config["permalink"], False) @@ -239,7 +240,7 @@ class TocTreeprocessor(Treeprocessor): def build_toc_div(self, toc_list): """ Return a string div given a toc list. """ div = etree.Element("div") - div.attrib["class"] = "toc" + div.attrib["class"] = self.toc_class # Add title to the div if self.title: @@ -328,6 +329,9 @@ class TocExtension(Extension): "title": ["", "Title to insert into TOC <div> - " "Defaults to an empty string"], + "toc_class": ['toc', + 'CSS class(es) used for the link. ' + 'Defaults to "toclink"'], "anchorlink": [False, "True if header should be a self link - " "Defaults to False"], @@ -41,6 +41,7 @@ nav: - Test Tools: test_tools.md - Contributing to Python-Markdown: contributing.md - Change Log: change_log/index.md + - Release Notes for v.3.4: change_log/release-3.4.md - Release Notes for v.3.3: change_log/release-3.3.md - Release Notes for v.3.2: change_log/release-3.2.md - Release Notes for v.3.1: change_log/release-3.1.md diff --git a/tests/test_syntax/extensions/test_toc.py b/tests/test_syntax/extensions/test_toc.py index 6871340..d879f6e 100644 --- a/tests/test_syntax/extensions/test_toc.py +++ b/tests/test_syntax/extensions/test_toc.py @@ -569,3 +569,46 @@ class TestTOC(TestCase): '<p>[TOC]<br />\ntext</p>', extensions=[TocExtension(), Nl2BrExtension()] ) + + def testTOCWithCustomClass(self): + + self.assertMarkdownRenders( + self.dedent( + ''' + [TOC] + # Header + ''' + ), + self.dedent( + ''' + <div class="custom"> + <ul> + <li><a href="#header">Header</a></li> + </ul> + </div> + <h1 id="header">Header</h1> + ''' + ), + extensions=[TocExtension(toc_class="custom")] + ) + + def testTOCWithCustomClasses(self): + self.assertMarkdownRenders( + self.dedent( + ''' + [TOC] + # Header + ''' + ), + self.dedent( + ''' + <div class="custom1 custom2"> + <ul> + <li><a href="#header">Header</a></li> + </ul> + </div> + <h1 id="header">Header</h1> + ''' + ), + extensions=[TocExtension(toc_class="custom1 custom2")] + ) |