aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--markdown/extensions/toc.py10
-rw-r--r--tests/extensions-x-toc/nested.html16
-rw-r--r--tests/extensions-x-toc/nested.txt9
3 files changed, 28 insertions, 7 deletions
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py
index 1624ccf..1d9489c 100644
--- a/markdown/extensions/toc.py
+++ b/markdown/extensions/toc.py
@@ -60,13 +60,9 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
if header_rgx.match(c.tag):
tag_level = int(c.tag[-1])
- # Regardless of how many levels we jumped
- # only one list should be created, since
- # empty lists containing lists are illegal.
-
- if tag_level < level:
+ while tag_level < level:
list_stack.pop()
- level = tag_level
+ level -= 1
if tag_level > level:
newlist = etree.Element("ul")
@@ -75,7 +71,7 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
else:
list_stack[-1].append(newlist)
list_stack.append(newlist)
- level = tag_level
+ level += 1
# Do not override pre-existing ids
if not "id" in c.attrib:
diff --git a/tests/extensions-x-toc/nested.html b/tests/extensions-x-toc/nested.html
new file mode 100644
index 0000000..a8a1583
--- /dev/null
+++ b/tests/extensions-x-toc/nested.html
@@ -0,0 +1,16 @@
+<h1 id="header-a">Header A</h1>
+<h2 id="header-1">Header 1</h2>
+<h3 id="header-i">Header i</h3>
+<h1 id="header-b">Header B</h1>
+<div class="toc">
+<ul>
+<li><a href="#header-a">Header A</a><ul>
+<li><a href="#header-1">Header 1</a><ul>
+<li><a href="#header-i">Header i</a></li>
+</ul>
+</li>
+</ul>
+</li>
+<li><a href="#header-b">Header B</a></li>
+</ul>
+</div> \ No newline at end of file
diff --git a/tests/extensions-x-toc/nested.txt b/tests/extensions-x-toc/nested.txt
new file mode 100644
index 0000000..9b515f9
--- /dev/null
+++ b/tests/extensions-x-toc/nested.txt
@@ -0,0 +1,9 @@
+# Header A
+
+## Header 1
+
+### Header i
+
+# Header B
+
+[TOC]