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

include directive: start from alternative heading level #262

Closed
comabrewer opened this issue Oct 6, 2020 · 8 comments
Closed

include directive: start from alternative heading level #262

comabrewer opened this issue Oct 6, 2020 · 8 comments
Labels
enhancement New feature or request

Comments

@comabrewer
Copy link

Thanks for the great project. I perfectly fits the use case I have, with one minor issue.

TL;DR: Is it possible to fix or ignore non-consecutive header levels?

For a group of related projects, I want the contributors to write all the documentation in a single README.md.
From the README and the docstrings, I generate the online docs with Sphinx and MyST.
I want the sections of the README to end up in separate documents of my generated docs.
The docs/ directory is populated with generic stub files that are generated by a cookiecutter.

The README always has the same sections (second level):

# project-name
## Usage
## Installation
## Changelog
### 0.1.0 (2020-10-01)
## Contributing
...

From this, I generate separate pages (e.g. changelog.md):

# Changelog

```{include} ..\README.md
:start-after: "## Changelog"
:end-before: "## Contributing"

My index.md:

# project-name

```{toctree}
installation.md
usage.md
api.md
changelog.md
contribution.md
```

What was a level 2 heading now should be a level 1 heading in the split documents.
When building the docs, MyST emits the warning Non-consecutive header level increase because of this. The warning is correct and intended, see #71, but is there any way around this? I want to treat Sphinx warnings as errors, because it has proven very helpful in the past.

Possible solutions:

  • Is it possible to disable the specific warning? Sphinx offers suppress_warnings, but I couldn't figure out the type for this warning.
  • Is there a possibility to modify header levels of the included documents, or to set the initial level from 0 to 1?
@comabrewer comabrewer added the enhancement New feature or request label Oct 6, 2020
@welcome
Copy link

welcome bot commented Oct 6, 2020

Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.

If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).

Welcome to the EBP community! 🎉

@chrisjsewell
Copy link
Member

thanks, I'll have to think about this one, perhaps as part of the include directive there could be an option to set the initial header level

@choldgraf
Copy link
Member

Maybe as a first step we can figure out what warning class is raised here so that you could use the suppress warnings config that you mentioned. Surely somebody else in the Sphinx world has had the same problem before? Maybe there’s a stackoverflow question about this or something?

@chrisjsewell
Copy link
Member

chrisjsewell commented Jan 20, 2021

No this warning arises directly from MyST-Parser:

if (level > parent_level) and (parent_level + 1 != level):
self.current_node.append(
self.reporter.warning(
"Non-consecutive header level increase; {} to {}".format(
parent_level, level
),
line=section.line,
)
)

correctly, because for example

# title
## sub-title
# title
### sub-title
## title
### sub-title

all are parsed to sphinx AST:

<section>
  <section>

which will then be always rendered as

# title
## sub-title

so it is not possible to conserve the original levels, if they are not consecutive and starting from 1

include is a special case though, since you are inserting into an existing document, it does make sense to potentially start at a level greater than 1

@chrisjsewell
Copy link
Member

With #313, this warning can now be suppressed.

I will leave this issue open though, as for include it would probably be better to have an extra option to "add/minus x" to the initial heading level

@chrisjsewell chrisjsewell changed the title Allow for missing header levels without emitting a warning. include directive: start from alternative heading level Feb 15, 2021
@comabrewer
Copy link
Author

Thanks a lot for picking this up. Being able to suppress warnings does the job for the moment. Your suggestion sounds even nicer.

@webknjaz
Copy link
Contributor

webknjaz commented Dec 5, 2021

Sounds like a great idea but I'm a bit concerned about the inconsistency with the Sphinx-native directive settings. I think if this is to be implemented, maybe there should be a PR submitted to the Sphinx upstream simultaneously so that it's consistent between RST and MD.

@chrisjsewell
Copy link
Member

Myst's include now supports an offset-heading option https://myst-parser.readthedocs.io/en/latest/syntax/organising_content.html#inserting-other-documents-directly-into-the-current-document

maybe there should be a PR submitted to the Sphinx upstream simultaneously so that it's consistent between RST and MD.

Others are welcome to try, but I have rarely found much join in trying to upstream changes like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants