Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TOC Extension's new toc_tokens to build TOC #1910

Closed
rockandska opened this issue Nov 25, 2019 · 2 comments
Closed

Use TOC Extension's new toc_tokens to build TOC #1910

rockandska opened this issue Nov 25, 2019 · 2 comments

Comments

@rockandska
Copy link

rockandska commented Nov 25, 2019

Hi Mkdocs,

It seems that the attribute "level" for page.toc items is not the level of the "hx" in the markdown documents but the "global" level

With a title :

# title 1    ------> level 0
## title 2  -------> level 1
### titile 3 ------> level 2

If there is not title :

## title 2  -------> level 0
### titile 3 ------> level 1

Is there anyway to get the real level of headings ?

# title 1    ------> level 1
## title 2  -------> level 2
### titile 3 ------> level 3
## title 2  -------> level 2
### titile 3 ------> level 3

Regards,

@waylan
Copy link
Member

waylan commented Nov 26, 2019

At this time, no, there is no way to get the header level (hn) from the TOC. MkDocs uses Python-Markdown's toc extension to generate the TOC and that extension does not preserve the header level in the HTML it generates. Therefore, that information is not available for MkDocs to use (MkDocs parses the HTML TOC and builds a data object which is passed on to the template).

That said, Python-Markdown recently updated the toc extension to also provide toc_tokens (a list if dicts) and that does preserve the header level. We could adapt MkDocs to make use of that, which would probably be better than parsing the HTML TOC anyway. This is something I've been meaning to explore, but just haven't had the time to get to. Of course, a PR is always welcome.

@rockandska
Copy link
Author

Thanks for your feedback.
I see that starting with python-markdown 3.0, it also includes a parameter 'toc_depth' who would allow a better control than 'navigation_depth' (since it allow restricting by range) and could be nice to benefit from it too.

Regards

@waylan waylan changed the title [TOC] Anyway to have the real "level" of a toc_item ? Use TOC Extension's new toc_tokens to build TOC Nov 26, 2019
@waylan waylan closed this as completed in 37e645d Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants