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 5 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.

Copy link
Author

Choose a reason for hiding this comment

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

Hello,
sorry for the late reply, I'm a bit busy at the moment.

Indeed, I'll make that more robust with mistune parsing / treatment / dumping back to markdown.

About your pandoc remark, I'm not sure to follow you.

Copy link
Author

Choose a reason for hiding this comment

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

Implemented changes to use mistune for parsing

  • only compatible with mistune v3, do you think it's worth it keeping compatibility with v2 as well ?
  • I'm using a logger, not sure if that's the preferred way to report warning / errors to users ?

Copy link
Member

Choose a reason for hiding this comment

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

only compatible with mistune v3, do you think it's worth it keeping compatibility with v2 as well ?

I think that's fine

I'm using a logger, not sure if that's the preferred way to report warning / errors to users ?

I left a comment on the code

@sapristi sapristi force-pushed the main branch 3 times, most recently from fbcdf15 to 45553a1 Compare February 6, 2025 22:47
@sapristi sapristi force-pushed the main branch 2 times, most recently from 9f8f427 to 6ff5dbb Compare February 9, 2025 13:46
@@ -97,6 +97,7 @@ class Exporter(LoggingConfigurable):
"nbconvert.preprocessors.ExtractOutputPreprocessor",
"nbconvert.preprocessors.ExtractAttachmentsPreprocessor",
"nbconvert.preprocessors.ClearMetadataPreprocessor",
"nbconvert.preprocessors.NumberedHeadingsPreprocessor",
Copy link
Author

Choose a reason for hiding this comment

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

Just added the processor here, otherwise it was not running python -m nbconvert --to=html --NumberedHeadingsPreprocessor.enabled=true notebook.ipynb (even though the option to enable it appeared in python -m nbconvert --help-all

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