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

Markdown template skips h2 level when no section exists #663

Open
flying-sheep opened this issue Aug 23, 2024 · 0 comments · May be fixed by #664
Open

Markdown template skips h2 level when no section exists #663

flying-sheep opened this issue Aug 23, 2024 · 0 comments · May be fixed by #664

Comments

@flying-sheep
Copy link

flying-sheep commented Aug 23, 2024

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'] }}
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.

1 participant