Skip to content
Merged
Prev Previous commit
Next Next commit
change toc_depth to an optional range, remove toc_height again
  • Loading branch information
klml committed Feb 14, 2019
commit cc8de79c43e0c95ff48e1f4605746ce6545ee2cc
27 changes: 11 additions & 16 deletions docs/extensions/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,16 @@ The following options are provided to configure the output:
* **`separator`**:
Word separator. Character which replaces white space in id. Defaults to "`-`".

* **`toc_height`**
Define the highest section level "n" (`<hn>` to `<h6>`, where `1 <= n <= 6`)
to include in the Table of Contents. Section levels above are omitted.
Defaults to `1`.

When used with conjunction with `baselevel`, this parameter will not
take the fitted hierarchy from `baselevel` into account.
If you set `baselevel` to 3 and `toc_height` to 2, the *first* headline
will be `<h3>` and so still included in the Table of Contents. To exclude
this first level, you have to set `toc_height` to 4.

* **`toc_depth`**
Define up to which section level "n" (`<h1>` to `<hn>`, where `1 <= n <= 6`)
to include in the Table of Contents. Defaults to `6`.
Define the range of section levels to include in the Table of Contents.
A single integer (b) defines the bottom section level (<h1>..<hb>) only.
Two digits seperated by a hyphen in between (```2-5```), define the
top (t) and the bottom (b) (<ht>..<hb>). Defaults to `6` (bottom).

When used with conjunction with `baselevel` this parameter will limit the
resulting (adjusted) heading. That is, if both `toc_depth` and `baselevel`
are 3, then only the highest level will be present in the table.
When used with conjunction with `baselevel`, this parameter will not
take the fitted hierarchy from `baselevel` into account. That is, if
both `toc_depth` and `baselevel` are 3, then only the highest level
will be present in the table. If you set `baselevel` to 3 and
`toc_depth` to '2-6', the *first* headline will be `<h3>` and so still
included in the Table of Contents. To exclude this first level, you
have to set `toc_depth` to '4-6'.
20 changes: 12 additions & 8 deletions markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def __init__(self, md, config):
if self.use_permalinks is None:
self.use_permalinks = config["permalink"]
self.header_rgx = re.compile("[Hh][123456]")
self.toc_height = config["toc_height"]
self.toc_depth = config["toc_depth"]

def iterparent(self, node):
Expand Down Expand Up @@ -236,8 +235,12 @@ def run(self, doc):
for el in doc.iter():
if isinstance(el.tag, string_type) and self.header_rgx.match(el.tag):
self.set_level(el)
if int(el.tag[-1]) < int(self.toc_height) or int(el.tag[-1]) > int(self.toc_depth):

if type( self.toc_depth ) != int:
toc_top, toc_bottom = self.toc_depth.split('-')
else:
toc_top = 1
toc_bottom = self.toc_depth
if int(el.tag[-1]) < int(toc_top) or int(el.tag[-1]) > int(toc_bottom):
continue
text = ''.join(el.itertext()).strip()

Expand Down Expand Up @@ -297,12 +300,13 @@ def __init__(self, **kwargs):
"Function to generate anchors based on header text - "
"Defaults to the headerid ext's slugify function."],
'separator': ['-', 'Word separator. Defaults to "-".'],
"toc_height": [1,
"Define the highest section level n (<hn>..<h6>) to"
"include in the TOC. Section levels above are omitted."],
"toc_depth": [6,
"Define up to which section level n (<h1>..<hn>) to "
"include in the TOC"]
"Define the range of section levels to include in"
"the Table of Contents. A single integer (b) defines"
"the bottom section level (<h1>..<hb>) only."
"Two digits seperated by a hyphen in between"
" (```2-5```), define the top (t) and the bottom (b)"
" (<ht>..<hb>). Defaults to `6` (bottom)."],
}

super(TocExtension, self).__init__(**kwargs)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,10 +1021,10 @@ def testTocInHeaders(self):
'<h1 id="toc"><em>[TOC]</em></h1>' # noqa
)

def testMinLevel(self):
def testMinMaxLevel(self):
""" Test toc_height setting """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(toc_height=3)]
extensions=[markdown.extensions.toc.TocExtension(toc_depth='3-4')]
)
text = '# Header 1 not in TOC\n\n## Header 2 not in TOC\n\n### Header 3\n\n####Header 4'
self.assertEqual(
Expand Down Expand Up @@ -1076,10 +1076,10 @@ def testMaxLevel(self):

self.assertNotIn("Header 3", md.toc)

def testMinLevelwithBaseLevel(self):
def testMinMaxLevelwithBaseLevel(self):
""" Test toc_height setting together with baselevel """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(toc_height=4,
extensions=[markdown.extensions.toc.TocExtension(toc_depth='4-6',
baselevel=3)]
)
text = '# First Header\n\n## Second Level\n\n### Third Level'
Expand Down