Skip to content

Extract first level heading as nav bar entry doesn't work if SPDX meta data as HTML comments is present #3908

Open

Description

@buhtz

Hello,
this is a follow-up from squidfunk/mkdocs-material#7919 .

To my research MkDocs itself (not a plugin or theme) is responsible to generate the entries in the navigation bar. It will use the first level heading from the md file if present.

In my case this does not work because there is a multiline HTML comment containing SPDX/REUSE meta data.

File: additional.md

<!--
SPDX-FileCopyrightText: © 2016 Germar Reitze

SPDX-License-Identifier: GPL-2.0-or-later

This file is part of the program "Back In Time" which is released under GNU
General Public License v2 (GPLv2).
See file/folder LICENSE or
go to <https://spdx.org/licenses/GPL-2.0-or-later.html>
-->
# Additional sources
lore ipsum

The navbar contains additional not Additional sources.

Without the meta data it works as expected. So what can I do?

I am aware that REUSE do offer other workarounds (REUSE.toml and <myfile>.license). But these are workarounds not solutions.

Can we do something about it? Is there a good reason for mkdocs to behave different in the case of having a html comment in the beginning?

Activity

  1. kamilkrzyskow commented on Jan 19, 2025

    @kamilkrzyskow

    I'll copy the relevant part from the comment for a possible fix here

    The Treeprocessor goes over the tags in the file but breaks after the first tag is checked, I guess this is a good place to skip over comment tags:

    for el in root:
    if el.tag == 'h1':
    self.title = get_heading_text(el, self.md)
    break

    It could be worth it to also investigate how would using both HTML comments and the front-matter meta header, affect the render.
    Given that REUSE and perhaps other tools expect .md files to use HTML comments as meta headers, then MkDocs should support all variants with both HTML comments above and below the front-matter meta header.

  2. buhtz commented on Jan 19, 2025

    @buhtz
    Author

    I can confirm that the front matter heading thing does work with mkdocs and also reuse lint is satisfied.

    ---
    SPDX-FileCopyrightText: 2019 Jane Doe <[email protected]>
    SPDX-License-Identifier: MIT
    Note:
        Additional license info bla bla
    ---
    # Introduction
    
  3. pawamoy commented on Jan 19, 2025

    @pawamoy
    Contributor

    Maybe dumb question, but have you tried adding newlines?

    <!--
    SPDX-FileCopyrightText: © 2016 Germar Reitze
    
    SPDX-License-Identifier: GPL-2.0-or-later
    
    This file is part of the program "Back In Time" which is released under GNU
    General Public License v2 (GPLv2).
    See file/folder LICENSE or
    go to <https://spdx.org/licenses/GPL-2.0-or-later.html>
    -->
    
    # Additional sources
    
    lorem ipsum

    The parser can be sensitive to that.

  4. pawamoy commented on Jan 19, 2025

    @pawamoy
    Contributor

    @kamilkrzyskow thanks, looks like the break was always at this indentation level, which might have been an oversight? I suppose it could be indented once more, to make sure we actually break at the first H1 tag, without expecting it to be the absolute first thing in the file. I'll try that locally and see if that breaks any test.

  5. pawamoy commented on Jan 19, 2025

    @pawamoy
    Contributor

    Tests pass with the suggested change.

  6. kamilkrzyskow commented on Jan 20, 2025

    @kamilkrzyskow

    @pawamoy

    looks like the break was always at this indentation level, which might have been an oversight?

    This is not an oversight, but intentional given the previous code, where it read the first non-empty line and checked for #

    if not line.strip():
    continue
    if not line.startswith('# '):
    return None
    return line.lstrip('# ')

    I understand this approach as a way to optimize the performance and prevent scanning the whole file.

    I suppose it could be indented once more, to make sure we actually break at the first H1 tag

    Some people only use H2 tags, as Material for MkDocs does a hack to add missing H1 based on page.tile.
    So there has to be some constraint to prevent the script from scanning the whole file and not finding anything.

    Either skip the comments and stick to checking the first tag being H1, or break on any H1-H6, but the latter also opens up a performance issue for docs without any headings 🤔

    Still, like mentioned above, HTML comments need to be properly handled above and below the front matter meta header to support all cases.

  7. pawamoy commented on Jan 20, 2025

    @pawamoy
    Contributor

    Thanks @kamilkrzyskow, that makes sense if the intention was to stay as close as the previous behavior.

    I don't think the perf hit is important here if we indent break. It wouldn't scan the whole file, just the top-level HTML elements, and only their tag name. It's pretty fast, even for huge documents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions