From f51125d01b88067d8523e9706cfa4558b3808222 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Wed, 5 Sep 2018 15:54:16 -0400 Subject: 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. --- markdown/extensions/toc.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'markdown') 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 -- cgit v1.2.3