Skip to content

Commit

Permalink
Merge pull request #282 from MattX/toc-depth
Browse files Browse the repository at this point in the history
Add TOC depth option
  • Loading branch information
nicholasserra committed Nov 16, 2017
2 parents 8b7409d + eba1e53 commit a28426a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/markdown2.py
Expand Up @@ -243,6 +243,10 @@ def __init__(self, html4tags=False, tab_width=4, safe_mode=None,
assert isinstance(self.extras, dict)
if "toc" in self.extras and "header-ids" not in self.extras:
self.extras["header-ids"] = None # "toc" implies "header-ids"
if self.extras["toc"] is None:
self._toc_depth = 6
else:
self._toc_depth = self.extras["toc"].get("depth", 6)
self._instance_extras = self.extras.copy()

self.link_patterns = link_patterns
Expand Down Expand Up @@ -1500,6 +1504,8 @@ def header_id_from_text(self, text, prefix, n):

_toc = None
def _toc_add_entry(self, level, id, name):
if level > self._toc_depth:
return
if self._toc is None:
self._toc = []
self._toc.append((level, id, self._unescape_special_chars(name)))
Expand Down
19 changes: 19 additions & 0 deletions test/tm-cases/toc_depth.html
@@ -0,0 +1,19 @@
<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>
5 changes: 5 additions & 0 deletions test/tm-cases/toc_depth.opts
@@ -0,0 +1,5 @@
{
"extras": {
"toc": {"depth": 3}
}
}
1 change: 1 addition & 0 deletions test/tm-cases/toc_depth.tags
@@ -0,0 +1 @@
toc extra
20 changes: 20 additions & 0 deletions test/tm-cases/toc_depth.text
@@ -0,0 +1,20 @@
# README for Blah

## Introduction

## The Meat

### Beef

##### Steak

##### Burgers

### Chicken

### Pork

#### Mmmmmmmm, bacon

# At the *top* level again!?

13 changes: 13 additions & 0 deletions test/tm-cases/toc_depth.toc_html
@@ -0,0 +1,13 @@
<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></li>
<li><a href="#chicken">Chicken</a></li>
<li><a href="#pork">Pork</a></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 a28426a

Please sign in to comment.