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 frontmatter title in place of level 1 heading for TOC and heading enumeration #94

Open
loneguardian opened this issue Dec 24, 2023 · 3 comments · May be fixed by #104
Open

Use frontmatter title in place of level 1 heading for TOC and heading enumeration #94

loneguardian opened this issue Dec 24, 2023 · 3 comments · May be fixed by #104

Comments

@loneguardian
Copy link

loneguardian commented Dec 24, 2023

This is more of a feature request than bug report.

I am trying to use this plugin but the majority of the docs do not have a level 1 heading, resulting in almost all of them triggering the following error:

The page [page name] ([page.location]) does not start with a level 1 heading.This is required for print page Table of Contents and/or enumeration of headings.

To enhance the compatibility of this plugin regarding page title, I would like to propose for the plugin to mimic the MkDocs behaviour of determining the page title:

MkDocs will attempt to determine the title of a document in the following ways, in order:

  1. A title defined in the nav configuration setting for a document.
  2. A title defined in the title meta-data key of a document.
  3. A level 1 Markdown header on the first line of the document body.
    (Setext-style headers are supported only since MkDocs 1.5.)
  4. The filename of a document.
@sblausten
Copy link

This would be great. It is very painful to have to go around adding h1s to docs to get them to work.

@dtuite
Copy link

dtuite commented May 23, 2024

Agreed. It's a pain to add these h1's after the fact.

timvink added a commit that referenced this issue May 23, 2024
@timvink timvink linked a pull request May 23, 2024 that will close this issue
@timvink
Copy link
Owner

timvink commented May 23, 2024

This one is tricky. The most common 'mistake' I've seen is that people use a secondary level heading as the first heading on a page, like so:

## title

content

Then other pages have normal titles again like so:

# title

content

When you combine these pages, the level of the title is not identical, which really messes up the layout and the TOC (the small one on the right):

image

Hence the logic to detect this and raise an error (also deals with the edge case that there are pages that have no title at all, because some users indeed it to be part of the previous page when the entire site is combined into a single page, or because they just didn't want a title).

# We need to validate that the first heading on each page is a h1
# This is required for the print page table of contents and enumeration logic
if self.config.get("add_table_of_contents") or self.config.get(
"enumerate_headings"
):
if page in self.all_pages_in_nav:
match = re.search(r"\<h[0-6]", html)
if match:
if not match.group() == "<h1":
msg = f"The page {page.title} ({page.file.src_path}) does not start with a level 1 heading."
msg += "This is required for print page Table of Contents and/or enumeration of headings."
raise AssertionError(msg)

You propose using the mkdocs behaviour of page titles, which is actually just using page.title. I went down the rabbit hole and I think I have a fix. The new behavior is to raise a warning instead of an error (which will become an error for users that use mkdocs build --strict)

image

The unit tests pass.. but before I release it I'd appreciate if someone could give it a spin for their use case. Maybe @sblausten or @dtuite ? You can install it form the pull request branch: #104

pip install git+https://github.com/timvink/mkdocs-print-site-plugin.git@support_missing_h1

If it works I'll merge and release on pypi.

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 a pull request may close this issue.

4 participants