mkdocs-enumerate-headings-plugin icon indicating copy to clipboard operation
mkdocs-enumerate-headings-plugin copied to clipboard

enumerate nav links

Open fmatter opened this issue 2 years ago • 8 comments

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)

fmatter avatar Sep 26 '23 21:09 fmatter

@timvink

fmatter avatar Oct 19 '23 02:10 fmatter

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') or page.title is not None before overriding it.
  • I'd like to add it behind an option, enumerate_navigation which can have a default of True. Then, if users don't like the new behaviour, they can disable it

timvink avatar Oct 25 '23 11:10 timvink

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?

fmatter avatar Oct 25 '23 17:10 fmatter

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 ?

timvink avatar Oct 25 '23 18:10 timvink

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? ;)

fmatter avatar Oct 25 '23 21:10 fmatter

Nice progress!

timvink avatar Oct 27 '23 10:10 timvink

OK, I've fixed the conditional and what was causing linting to fail.

fmatter avatar Oct 27 '23 14:10 fmatter

@timvink I think this is ready

fmatter avatar Nov 04 '23 16:11 fmatter