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

feat: add NumberedHeadingsPreprocessor #2187

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

sapristi
Copy link

@sapristi sapristi commented Oct 19, 2024

Fixes #1586

Add pre-processor that adds numbering to markdown headers:

transforms title in markdown cells, such that e.g.

# title 
## subtitle
content 
# other title

turns into

# 1 title 
## 1.1 subtitle
content 
# 2 other title

@sapristi sapristi force-pushed the main branch 3 times, most recently from 4e870ae to df53b2b Compare October 19, 2024 18:16
@krassowski
Copy link
Member

Thank you @sapristi for sending the PR. Was this requested in an issue before? Just trying to understand if there is sufficient community need for this to live in nbconvert given the maintenance costs for adding a new pre-processor.

@sapristi
Copy link
Author

Hello, I just checked, it seems that some people were interested in this as well, e.g. #1586

IMO this is a very essential feature for working with a document - it's also a feature provided in the Jupyter web UI, so I think I'm not the only one.


def _transform_markdown_line(self, line, resources):
"""Rewrites one markdown line, if needed"""
if m := re.match(r"^(?P<level>#+) (?P<heading>.*)", line):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I we have a code in markdown, e.g.:

This is how you create a heading in Markdown
```markdown
# heading
```

It feels like a more robust solution would be to use mistune API for parsing, see https://mistune.lepture.com/en/latest/api.html (markdown_mistune is already in use here, but it should fail nicely).

It is also possible to instruct pandoc to run with --number-sections (see https://www.youtube.com/watch?v=gDV63UMJR_U and https://pandoc.org/chunkedhtml-demo/8.3-headings.html and https://pandoc.org/MANUAL.html) but pandoc is neither the only nor even default markdown parser.

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

Successfully merging this pull request may close these issues.

nbconvert -Number Headings when exporting from jupyter ipython to html
2 participants