timvink / mkdocs-print-site-plugin

MkDocs Plugin that adds an additional page that combines all pages, allowing easy exports to PDF and standalone HTML.
https://timvink.github.io/mkdocs-print-site-plugin/
MIT License
145 stars 24 forks source link

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

Closed loneguardian closed 4 months ago

loneguardian commented 9 months ago

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 commented 4 months ago

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

dtuite commented 4 months ago

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

timvink commented 4 months ago

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).

https://github.com/timvink/mkdocs-print-site-plugin/blob/0a7300b251929d0c06d5f719a741525cf0ec26f8/mkdocs_print_site_plugin/plugin.py#L219-L230

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: https://github.com/timvink/mkdocs-print-site-plugin/pull/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.