Skip to content

Commit b434317

Browse files
committed
toc: add option to limit which heading levels get anchor links
1 parent 22e89c1 commit b434317

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

docs/extensions/toc.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,12 @@ The following options are provided to configure the output:
239239
Word separator. Character which replaces white space in id. Defaults to "`-`".
240240

241241
* **`toc_depth`**
242-
Define the range of section levels to include in the Table of Contents.
243-
A single integer (`b`) defines the bottom section level (`<h1>..<hb>`) only.
244-
A string consisting of two digits separated by a hyphen in between (`"2-5"`),
245-
define the top (`t`) and the bottom (`b`) (`<ht>..<hb>`). Defaults to `6` (bottom).
246-
247-
When used with conjunction with `baselevel`, this parameter will not
248-
take the fitted hierarchy from `baselevel` into account. That is, if
249-
both `toc_depth` and `baselevel` are `3`, then only the highest level
250-
will be present in the table. If you set `baselevel` to `3` and
251-
`toc_depth` to `"2-6"`, the *first* headline will be `<h3>` and so still
252-
included in the Table of Contents. To exclude this first level, you
253-
have to set `toc_depth` to `"4-6"`.
242+
Define the highest heading level (deepest `<hN>`) for which to add anchor links or permalinks.
243+
Defaults to `6`
244+
245+
For example, setting max_level to `2` adds anchors only to `<h1>` and `<h2>` elements.
246+
247+
* **`max_level`**
254248

255249
A trivial example:
256250

markdown/extensions/toc.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def __init__(self, md: Markdown, config: dict[str, Any]):
257257
self.permalink_class: str = config["permalink_class"]
258258
self.permalink_title: str = config["permalink_title"]
259259
self.permalink_leading: bool | None = parseBoolValue(config["permalink_leading"], False)
260-
self.header_rgx = re.compile("[Hh][123456]")
260+
self.header_rgx = re.compile(f'[Hh][1-{config["max_level"]}')
261261
if isinstance(config["toc_depth"], str) and '-' in config["toc_depth"]:
262262
self.toc_top, self.toc_bottom = [int(x) for x in config["toc_depth"].split('-')]
263263
else:
@@ -466,6 +466,11 @@ def __init__(self, **kwargs):
466466
'separated by a hyphen in between (`2-5`) defines the top (t) and the bottom (b) (<ht>..<hb>). '
467467
'Default: `6` (bottom).'
468468
],
469+
'max_level': [
470+
6,
471+
'Define the max heading level for which to add anchors/permalinks.'
472+
'Default: `6`'
473+
]
469474
}
470475
""" Default configuration options. """
471476

tests/test_syntax/extensions/test_toc.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,24 @@ def testAnchorLinkWithDoubleInlineCode(self):
555555
extensions=[TocExtension(anchorlink=True)]
556556
)
557557

558+
def testAnchorLinkWithMaxLevel(self):
559+
self.assertMarkdownRenders(
560+
self.dedent(
561+
'''
562+
# Header 1
563+
564+
## Header *2*
565+
'''
566+
),
567+
self.dedent(
568+
'''
569+
<h1 id="header-1"><a class="toclink" href="#header-1">Header 1</a></h1>
570+
<h2 id="header-2">Header <em>2</em></h2>
571+
'''
572+
),
573+
extensions=[TocExtension(anchorlink=True, max_level=1)]
574+
)
575+
558576
def testPermalink(self):
559577
self.assertMarkdownRenders(
560578
'# Header',

0 commit comments

Comments
 (0)