mkdocs-enumerate-headings-plugin
mkdocs-enumerate-headings-plugin copied to clipboard
enumerate nav links
Since chapter numbers are added to h1 titles it seems logical to me that you would want enumeration in the site navigation, too (which is distinct from the TOC). See the plugin demo gif, where chapter titles occur unnumbered in the navigation, and numbered in the body.
(exclude works as expected)
@timvink
Hi @fmatter.
Thanks for the PR. Makes sense to add that. Two comments:
- Unit tests are failing, can you have a look? I think the fix is to check if
hasattr(page, 'title')orpage.title is not Nonebefore overriding it. - I'd like to add it behind an option,
enumerate_navigationwhich can have a default ofTrue. Then, if users don't like the new behaviour, they can disable it
Unit tests are failing, can you have a look? I think the fix is to check if hasattr(page, 'title') or page.title is not None before overriding it.
Unfortunately, it's slightly more complicated: I think pages without headings will receive a <h1> from mkdocs, sourced from page.title. If that title already has a hard-coded enumeration, we end up with double enumeration, which causes the test to fail.
I tried checking if there were no <h1> in the content, then -- if necessary -- adding f"# {page.title}" to page.markdown and rerendering. The rerendered page.content does now have an <h1>, but the final result stays the same. I gave up at that point; do you maybe have an idea why mkdocs still creates an <h1> from the title, even though there should be one in the page content?
Ah I vaguely remember now having trouble with the navigation enumeration when I wrote this plugin.
It's complex, plugins can mess with the title and navigation, and the navigation title can be set from mkdocs or can be inferred from the markdown.
I'm sure it's possible, but I don't use this plugin anymore and already maintain too many mkdocs plugins, so I won't pursue this one. Shall we close the PR ?
There we go! I think using on_post_page to remove the chapter number, followed by a period and a space, inside an <h1> is OK behavior? ;)
Nice progress!
OK, I've fixed the conditional and what was causing linting to fail.
@timvink I think this is ready