4

I want to change the depth of the number of headers the table of contents present on the TOC.

I can see toc_depth option exists as a configuration option but this is global. Is there a way to specify a single document?

Cheers

Ariel
  • 529
  • 2
  • 13

1 Answers1

2

You can update your templates using custom templates. Override the toc.html template file item macro https://github.com/mkdocs/mkdocs/blob/234fb10e543af4c5dcf0ee54c1d831846303f4cb/mkdocs/themes/mkdocs/toc.html#L9 and replace:

{%- if item.level <= config.theme.navigation_depth %}

by something like:

{% if not page.meta.toc_depth or item.level <= page.meta.toc_depth or item.level <= config.theme.navigation_depth %}

and add toc_depth in meta header of your .md page.

---
description: this is the page I want TOC to only display level 2 max.
toc_depth: 2
---

If you use Material for MkDocs theme you can override the partials/toc-item.html file with the following:

{% if not page.meta.toc_depth or toc_item.level <= page.meta.toc_depth %}
Nicolas Massart
  • 530
  • 9
  • 21
  • 1
    Worked for me. Thanks. Used `pip show mkdocs` to find package. In my case toc.html is in ~/.local/lib/python3.10/site-packages/mkdocs/themes/mkdocs. Another thing is that part '{%-' looks like part of syntax. In the answer's second line of code dash is missing after %, I kept mine. As I said: Worked for me. – dobhareach Sep 22 '22 at 20:06
  • 1
    The dash `-` (minus sign) is just a whitespace strip insruction. It doesn't impact the condition, only the rendering on the final html. So you can put it or not, it doesn't matter. See https://jinja.palletsprojects.com/en/3.1.x/templates/#whitespace-control – Nicolas Massart Sep 23 '22 at 09:51