aboutsummaryrefslogtreecommitdiffstats
path: root/markdown
diff options
context:
space:
mode:
authorWaylan Limberg <waylan.limberg@icloud.com>2018-09-05 15:54:16 -0400
committerGitHub <noreply@github.com>2018-09-05 15:54:16 -0400
commitf51125d01b88067d8523e9706cfa4558b3808222 (patch)
treea3bfba21a78865d023129f20e8ca2395d583354f /markdown
parentda68eb57a1f88b02543950f623a64ec6186d2ab2 (diff)
downloadmarkdown-f51125d01b88067d8523e9706cfa4558b3808222.tar.gz
markdown-f51125d01b88067d8523e9706cfa4558b3808222.tar.bz2
markdown-f51125d01b88067d8523e9706cfa4558b3808222.zip
Support custom labels in TOC. (#700)
New `toc_tokens` attribute on Markdown class. Contains the raw tokens used to build the Table of Contents. Users can use this to build their own custom Table of Contents rather than needing to parse the HTML available on the `toc` attribute of the Markdown class.
Diffstat (limited to 'markdown')
-rw-r--r--markdown/extensions/toc.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py
index 2741037..f6121c2 100644
--- a/markdown/extensions/toc.py
+++ b/markdown/extensions/toc.py
@@ -247,15 +247,20 @@ class TocTreeprocessor(Treeprocessor):
toc_tokens.append({
'level': int(el.tag[-1]),
'id': el.attrib["id"],
- 'name': text
+ 'name': el.attrib.get('data-toc-label', text)
})
+ # Remove the data-toc-label attribute as it is no longer needed
+ if 'data-toc-label' in el.attrib:
+ del el.attrib['data-toc-label']
+
if self.use_anchors:
self.add_anchor(el, el.attrib["id"])
if self.use_permalinks:
self.add_permalink(el, el.attrib["id"])
- div = self.build_toc_div(nest_toc_tokens(toc_tokens))
+ toc_tokens = nest_toc_tokens(toc_tokens)
+ div = self.build_toc_div(toc_tokens)
if self.marker:
self.replace_marker(doc, div)
@@ -263,6 +268,7 @@ class TocTreeprocessor(Treeprocessor):
toc = self.md.serializer(div)
for pp in self.md.postprocessors:
toc = pp.run(toc)
+ self.md.toc_tokens = toc_tokens
self.md.toc = toc
@@ -310,6 +316,7 @@ class TocExtension(Extension):
def reset(self):
self.md.toc = ''
+ self.md.toc_tokens = []
def makeExtension(**kwargs): # pragma: no cover