Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [pull #737] Fix XSS from code spans that the span gamut doesn't treat as code spans in safe mode (#726)
- [pull #743] Add a `strip-syntax` option to the `header-ids` extra that leaves link URLs, image paths, inline HTML tags and emphasis markers out of header ids (#192)
- [pull #747] Fix `link-patterns` extra linking text inside Markdown links and images whose label wraps onto another line
- [pull #754] Fix `toc` extra generating invalid HTML when a header is higher than the first one, and nesting headers under the wrong parent after a skipped level


## python-markdown2 2.5.5
Expand Down
9 changes: 8 additions & 1 deletion lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4639,11 +4639,18 @@ def indent():
elif level == h_stack[-1]:
lines[-1] += "</li>"
else:
while level < h_stack[-1]:
# Close nested lists until we're back in the one this header
# belongs in. The outermost list is never closed, so a header
# above the first one's level (eg: an h1 after an opening h2)
# stays at the top level.
while len(h_stack) > 2 and level <= h_stack[-2]:
h_stack.pop()
if not lines[-1].endswith("</li>"):
lines[-1] += "</li>"
lines.append("%s</ul></li>" % indent())
if not lines[-1].endswith("</li>"):
lines[-1] += "</li>"
h_stack[-1] = level
lines.append('{}<li><a href="#{}">{}</a>'.format(
indent(), id, name))
while len(h_stack) > 1:
Expand Down
15 changes: 15 additions & 0 deletions test/tm-cases/toc_shallower_headers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h2 id="overview">Overview</h2>

<h1 id="getting-started">Getting Started</h1>

<h2 id="install">Install</h2>

<h3 id="notes">Notes</h3>

<h1 id="api">API</h1>

<h3 id="functions">Functions</h3>

<h2 id="classes">Classes</h2>

<h2 id="errors">Errors</h2>
1 change: 1 addition & 0 deletions test/tm-cases/toc_shallower_headers.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["toc"]}
1 change: 1 addition & 0 deletions test/tm-cases/toc_shallower_headers.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
toc extra
15 changes: 15 additions & 0 deletions test/tm-cases/toc_shallower_headers.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Overview

# Getting Started

## Install

### Notes

# API

### Functions

## Classes

## Errors
16 changes: 16 additions & 0 deletions test/tm-cases/toc_shallower_headers.toc_html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#getting-started">Getting Started</a>
<ul>
<li><a href="#install">Install</a>
<ul>
<li><a href="#notes">Notes</a></li>
</ul></li>
</ul></li>
<li><a href="#api">API</a>
<ul>
<li><a href="#functions">Functions</a></li>
<li><a href="#classes">Classes</a></li>
<li><a href="#errors">Errors</a></li>
</ul></li>
</ul>