Skip to content

Markdown template skips h2 level when no section exists #663

@flying-sheep

Description

@flying-sheep

MyST doesn’t like that and throws a warning. I’d like to stay warning-free without spuriously ignoring warnings.

I filed #664, but a better solution for this is (and would have been for #648) to add a “initial section depth” config option for markdown instead of allowing users to customize the title section depth while not adapting the other sections.

We can kinda hack this in with the current solution as well:

if m := re.fullmatch(r"([^\n]*\n)*(?P<hashes>#+)\s.*", title_format, re.MULTILINE):
    initial_depth = len(m["hashes"])
else:
    initial_depth = 1
sec_depth = initial_depth + 1
cat_depths = defaultdict(lambda: sec_depth + 1)
cat_depths[""] = sec_depth
depth = dict(title=initial_depth, section=sec_depth, category=cat_depths)

and the template could read for e.g. the section header:

{{ "#" * depth["section"] }} {{section}}

and for the category header:

{{ "#" * depth["category"][section] }} {{ definitions[category]['name'] }}
alternative implementation

or less complex code

[…]
cat_depth = sec_depth + 1
depth = dict(title=initial_depth, section=sec_depth, category=cat_depth)

and more complex template

{{ "#" * depth["category" if section else "section"] }} {{ definitions[category]['name'] }}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions