Skip to content

Commit 4cabf3b

Browse files
author
Waylan Limberg
committed
TOC extension now attaches toc to Markdown instance (Markdown.toc), but only if a marker was not found in the document.
1 parent 596492b commit 4cabf3b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

markdown/extensions/toc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def iterparent(self, root):
2323
yield parent, child
2424

2525
def run(self, doc):
26+
marker_found = False
27+
2628
div = etree.Element("div")
2729
div.attrib["class"] = "toc"
2830
last_li = None
@@ -61,6 +63,7 @@ def run(self, doc):
6163
if p[i] == c:
6264
p[i] = div
6365
break
66+
marker_found = True
6467

6568
if header_rgx.match(c.tag):
6669
tag_level = int(c.tag[-1])
@@ -106,6 +109,14 @@ def run(self, doc):
106109
c.append(anchor)
107110

108111
list_stack[-1].append(last_li)
112+
if not marker_found:
113+
# searialize and attach to markdown instance.
114+
prettify = self.markdown.treeprocessors.get('prettify')
115+
if prettify: prettify.run(div)
116+
toc = self.markdown.serializer(div)
117+
for pp in self.markdown.postprocessors.values():
118+
toc = pp.run(toc)
119+
self.markdown.toc = toc
109120

110121
class TocExtension(markdown.Extension):
111122
def __init__(self, configs):

0 commit comments

Comments
 (0)