@@ -514,7 +514,35 @@ def extract_titles_from_notebook_node(nb: NotebookNode):
514
514
markdown_collection = ""
515
515
for cell in nb .cells :
516
516
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 "
518
546
519
547
titles_array = []
520
548
renderer = HeadingExtractor ()
@@ -531,4 +559,6 @@ def extract_titles_from_notebook_node(nb: NotebookNode):
531
559
id = raw_text .replace (" " , "-" )
532
560
href = "#" + id
533
561
titles_array .append ([header_level , raw_text , id , href ])
562
+ # print('header_level:', header_level)
563
+ # print('raw_text:', raw_text)
534
564
return titles_array
0 commit comments