Skip to content

Commit 6fbb968

Browse files
Update extract_titles_from_notebook_node to fix some parsing issues.
1 parent de2ff4e commit 6fbb968

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

nbconvert/filters/markdown_mistune.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,35 @@ def extract_titles_from_notebook_node(nb: NotebookNode):
514514
markdown_collection = ""
515515
for cell in nb.cells:
516516
if cell.cell_type == "markdown":
517-
markdown_collection = markdown_collection + cell.source + "\n"
517+
lines = cell.source.splitlines()
518+
for line in lines:
519+
if line.startswith('#') and line.count('#') != 1: # exclude the main title to build the table of content
520+
markdown_collection = markdown_collection + line.strip() + "\n"
521+
if line.startswith('<h2>'):
522+
line = line.replace("<h2>", "# ")
523+
if line.startswith('<h3>'):
524+
line = line.replace("<h3>", "# ")
525+
if line.startswith('<h4>'):
526+
line = line.replace("<h4>", "# ")
527+
if line.startswith('<h5>'):
528+
line = line.replace("<h5>", "# ")
529+
if line.startswith('<h6>'):
530+
line = line.replace("<h6>", "# ")
531+
if '</h2>' in line:
532+
line = line.replace("</h2>", "")
533+
markdown_collection = markdown_collection + line.strip() + "\n"
534+
if '</h3>' in line:
535+
line = line.replace("</h3>", "")
536+
markdown_collection = markdown_collection + line.strip() + "\n"
537+
if '</h4>' in line:
538+
line = line.replace("</h4>", "")
539+
markdown_collection = markdown_collection + line.strip() + "\n"
540+
if '</h5>' in line:
541+
line = line.replace("</h5>", "")
542+
markdown_collection = markdown_collection + line.strip() + "\n"
543+
if '</h6>' in line:
544+
line = line.replace("</h6>", "")
545+
markdown_collection = markdown_collection + line.strip() + "\n"
518546

519547
titles_array = []
520548
renderer = HeadingExtractor()
@@ -531,4 +559,6 @@ def extract_titles_from_notebook_node(nb: NotebookNode):
531559
id = raw_text.replace(" ", "-")
532560
href = "#" + id
533561
titles_array.append([header_level, raw_text, id, href])
562+
# print('header_level:', header_level)
563+
# print('raw_text:', raw_text)
534564
return titles_array

0 commit comments

Comments
 (0)