Commits

Waylan Limberg  committed 40ff4fa

Fixed TOC extension to properly nest multiple header levels in the table of contents. Specificly, when stepping back multiple levels, the nestsed listed now follow suite. Test included. Thanks for the patch Jack Miller.

  • Participants
  • Parent commits 6f462c3

Comments (0)

Files changed (3)

File markdown/extensions/toc.py

             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")
                     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:

File tests/extensions-x-toc/nested.html

+<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>

File tests/extensions-x-toc/nested.txt

+# Header A
+
+## Header 1
+
+### Header i
+
+# Header B
+
+[TOC]