close
Skip to content
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
4 changes: 4 additions & 0 deletions docs/change_log/release-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
3 changes: 3 additions & 0 deletions docs/extensions/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
6 changes: 5 additions & 1 deletion markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def __init__(self, md, config):
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)
Expand Down Expand Up @@ -239,7 +240,7 @@ def add_permalink(self, c, elem_id):
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:
Expand Down Expand Up @@ -328,6 +329,9 @@ def __init__(self, **kwargs):
"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"],
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,46 @@ def testNl2brCompatibility(self):
'<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")]
)