Skip to content

Commit

Permalink
Add customizable TOC title class
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloxnet authored Aug 11, 2023
1 parent acdbdc1 commit 25b64df
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions .spell-dict
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Cogumbreiro
CommonMark
convertFile
CSS
customizable
dedent
deliminators
deregister
Expand Down
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://keepachangelog.com/>.
* 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

Expand Down
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`.

* **`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 `<div>` containing the Table of Contents. Defaults to `toc`.

Expand Down
7 changes: 6 additions & 1 deletion markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -335,6 +337,9 @@ def __init__(self, **kwargs):
"title": ["",
"Title to insert into TOC <div> - "
"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"'],
Expand Down
42 changes: 42 additions & 0 deletions tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,45 @@ def testTOCWithCustomClasses(self):
),
extensions=[TocExtension(toc_class="custom1 custom2")]
)

def testTOCWithEmptyTitleClass(self):

self.assertMarkdownRenders(
self.dedent(
'''
[TOC]
# Header
'''
),
self.dedent(
'''
<div class="toc"><span>ToC</span><ul>
<li><a href="#header">Header</a></li>
</ul>
</div>
<h1 id="header">Header</h1>
'''
),
extensions=[TocExtension(title_class="", title='ToC')]
)

def testTOCWithCustomTitleClass(self):

self.assertMarkdownRenders(
self.dedent(
'''
[TOC]
# Header
'''
),
self.dedent(
'''
<div class="toc"><span class="tocname">ToC</span><ul>
<li><a href="#header">Header</a></li>
</ul>
</div>
<h1 id="header">Header</h1>
'''
),
extensions=[TocExtension(title_class="tocname", title='ToC')]
)

0 comments on commit 25b64df

Please sign in to comment.