Skip to content

Commit

Permalink
Merge pull request #568 from Crozzers/prepend-toc
Browse files Browse the repository at this point in the history
Add `prepend` arg to toc extra (#397)
  • Loading branch information
nicholasserra committed Feb 28, 2024
2 parents ca4ba2f + f90c2e1 commit 646e6d2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,7 @@

- [pull #519] Add support for custom extras
- [pull #519] Drop Python 3.5 support
- [pull #568] Add `prepend` arg to toc extra (#397)


## python-markdown2 2.4.13
Expand Down
2 changes: 1 addition & 1 deletion lib/markdown2.py
Expand Up @@ -513,7 +513,7 @@ def toc_sort(entry):
self._toc_html = calculate_toc_html(self._toc)

# Prepend toc html to output
if self.cli:
if self.cli or (self.extras['toc'] is not None and self.extras['toc'].get('prepend', False)):
text = '{}\n{}'.format(self._toc_html, text)

text += "\n"
Expand Down
40 changes: 40 additions & 0 deletions test/tm-cases/toc_prepend.html
@@ -0,0 +1,40 @@
<ul>
<li><a href="#readme-for-blah">README for Blah</a>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#the-meat">The Meat</a>
<ul>
<li><a href="#beef">Beef</a>
<ul>
<li><a href="#steak">Steak</a></li>
<li><a href="#burgers">Burgers</a></li>
</ul></li>
<li><a href="#chicken">Chicken</a></li>
<li><a href="#pork">Pork</a>
<ul>
<li><a href="#mmmmmmmm-bacon">Mmmmmmmm, bacon</a></li>
</ul></li>
</ul></li>
</ul></li>
<li><a href="#at-the-top-level-again">At the <em>top</em> level again!?</a></li>
</ul>

<h1 id="readme-for-blah">README for Blah</h1>

<h2 id="introduction">Introduction</h2>

<h2 id="the-meat">The Meat</h2>

<h3 id="beef">Beef</h3>

<h5 id="steak">Steak</h5>

<h5 id="burgers">Burgers</h5>

<h3 id="chicken">Chicken</h3>

<h3 id="pork">Pork</h3>

<h4 id="mmmmmmmm-bacon">Mmmmmmmm, bacon</h4>

<h1 id="at-the-top-level-again">At the <em>top</em> level again!?</h1>
1 change: 1 addition & 0 deletions test/tm-cases/toc_prepend.opts
@@ -0,0 +1 @@
{"extras": {"toc": {"prepend": True}}}
1 change: 1 addition & 0 deletions test/tm-cases/toc_prepend.tags
@@ -0,0 +1 @@
toc extra
20 changes: 20 additions & 0 deletions test/tm-cases/toc_prepend.text
@@ -0,0 +1,20 @@
# README for Blah

## Introduction

## The Meat

### Beef

##### Steak

##### Burgers

### Chicken

### Pork

#### Mmmmmmmm, bacon

# At the *top* level again!?

20 changes: 20 additions & 0 deletions test/tm-cases/toc_prepend.toc_html
@@ -0,0 +1,20 @@
<ul>
<li><a href="#readme-for-blah">README for Blah</a>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#the-meat">The Meat</a>
<ul>
<li><a href="#beef">Beef</a>
<ul>
<li><a href="#steak">Steak</a></li>
<li><a href="#burgers">Burgers</a></li>
</ul></li>
<li><a href="#chicken">Chicken</a></li>
<li><a href="#pork">Pork</a>
<ul>
<li><a href="#mmmmmmmm-bacon">Mmmmmmmm, bacon</a></li>
</ul></li>
</ul></li>
</ul></li>
<li><a href="#at-the-top-level-again">At the <em>top</em> level again!?</a></li>
</ul>

0 comments on commit 646e6d2

Please sign in to comment.