Skip to content

Commit

Permalink
Add sysmetically an id to the headers to be sure to have a defined href.
Browse files Browse the repository at this point in the history
  • Loading branch information
HaudinFlorence committed Nov 7, 2024
1 parent e5d2da6 commit 44080a1
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 44080a1

Please sign in to comment.