From 44080a1a181eeecb4ae7d64aa59e30d0b1a650ce Mon Sep 17 00:00:00 2001 From: HaudinFlorence Date: Thu, 7 Nov 2024 09:58:21 +0100 Subject: [PATCH] Add sysmetically an id to the headers to be sure to have a defined href. --- nbconvert/filters/markdown_mistune.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nbconvert/filters/markdown_mistune.py b/nbconvert/filters/markdown_mistune.py index d1d3226c9..59a104649 100644 --- a/nbconvert/filters/markdown_mistune.py +++ b/nbconvert/filters/markdown_mistune.py @@ -505,13 +505,15 @@ def extract_titles_from_notebook_node(nb: NotebookNode): titles_array = [] html_collection = bs4.BeautifulSoup(cells_html_collection, "html.parser") - headings = html_collection.select("h1, h2, h3, h4, h5, h6") + headings = html_collection.select("h1, h2, h3, h4, h5, h6") + # Iterate on all headings to get the necessary information on the various titles for heading in headings: text = heading.get_text().lstrip().rstrip() level = int(heading.name[1]) - id = text.replace(" ", "-") - href = "#" + id + header_id = text.replace(" ", "-") + heading['id'] = header_id + href = "#" + header_id titles_array.append([str(heading), level, href]) return titles_array