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

enumerate nav links #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

enumerate nav links #36

wants to merge 4 commits into from

Conversation

fmatter
Copy link

@fmatter fmatter commented Sep 26, 2023

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
Copy link
Author

fmatter commented Oct 19, 2023

@timvink

@timvink
Copy link
Owner

timvink commented Oct 25, 2023

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

@fmatter
Copy link
Author

fmatter commented Oct 25, 2023

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?

@timvink
Copy link
Owner

timvink commented Oct 25, 2023

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 ?

@fmatter
Copy link
Author

fmatter commented Oct 25, 2023

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

@timvink
Copy link
Owner

timvink commented Oct 27, 2023

Nice progress!

@fmatter
Copy link
Author

fmatter commented Oct 27, 2023

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

@fmatter
Copy link
Author

fmatter commented Nov 4, 2023

@timvink I think this is ready

Comment on lines +181 to +183
if self.config.get("enumerate_nav", True):
output = output.replace(f"<h1>{page.chapter}. ", "<h1>")

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this remove all enumeration from titles?

So the example gif from the README wouldn't have enumeration anymore?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants