diff --git a/nbconvert/exporters/html.py b/nbconvert/exporters/html.py
index c6f10418e..c953e7223 100644
--- a/nbconvert/exporters/html.py
+++ b/nbconvert/exporters/html.py
@@ -29,7 +29,7 @@
from nbconvert.filters.markdown_mistune import (
IPythonRenderer,
MarkdownWithMath,
- extract_titles_from_markdown_input,
+ extract_titles_from_notebook_node,
)
from nbconvert.filters.widgetsdatatypefilter import WidgetsDataTypeFilter
from nbconvert.utils.iso639_1 import iso639_1
@@ -260,15 +260,8 @@ def from_notebook_node( # type:ignore[explicit-override, override]
highlight_code = self.filters.get(
"highlight_code", Highlight2HTML(pygments_lexer=lexer, parent=self)
)
- markdown_collection = ""
- for cell in nb.cells:
- if cell.cell_type == "markdown":
- markdown_collection = markdown_collection + cell.source + "\n"
resources = self._init_resources(resources)
- if resources is None:
- resources = {}
- resources["tableofcontents"] = extract_titles_from_markdown_input(markdown_collection)
filter_data_type = WidgetsDataTypeFilter(
notebook_metadata=self._nb_metadata, parent=self, resources=resources
@@ -372,4 +365,5 @@ def resources_include_url(name):
resources["should_sanitize_html"] = self.sanitize_html
resources["language_code"] = self.language_code
resources["should_not_encode_svg"] = self.skip_svg_encoding
+ resources["extract_titles_from_nodebook_node"] = extract_titles_from_notebook_node
return resources
diff --git a/nbconvert/filters/markdown_mistune.py b/nbconvert/filters/markdown_mistune.py
index 0c698fc27..44287203e 100644
--- a/nbconvert/filters/markdown_mistune.py
+++ b/nbconvert/filters/markdown_mistune.py
@@ -21,6 +21,7 @@
from pygments.util import ClassNotFound
from nbconvert.filters.strings import add_anchor
+from nbformat import NotebookNode
try: # for Mistune >= 3.0
from mistune import ( # type:ignore[attr-defined]
@@ -505,14 +506,20 @@ def heading(self, text, level):
return ""
-def extract_titles_from_markdown_input(markdown_input):
+def extract_titles_from_notebook_node(nb: NotebookNode):
"""Create a Markdown parser with the HeadingExtractor renderer to collect all the headings of a notebook
- The input argument is markdown_input that is a single string with all the markdown content concatenated
+ The input argument is the notebooknode from which a single string with all the markdown content concatenated
The output is an array containing information about the headings such as their level, their text content, an identifier and a href that can be used in case of html converter.s"""
+
+ markdown_collection = ""
+ for cell in nb.cells:
+ if cell.cell_type == "markdown":
+ markdown_collection = markdown_collection + cell.source + "\n"
+
titles_array = []
renderer = HeadingExtractor()
extract_titles = mistune.create_markdown(renderer=renderer)
- extract_titles(markdown_input)
+ extract_titles(markdown_collection)
headings = renderer.headings
# Iterate on all headings to get the necessary information on the various titles
diff --git a/share/templates/lab/index.html.j2 b/share/templates/lab/index.html.j2
index 20b2eaae1..7b756fb50 100644
--- a/share/templates/lab/index.html.j2
+++ b/share/templates/lab/index.html.j2
@@ -178,8 +178,9 @@ a.anchor-link {
{%- block tableofcontents -%}
{%- if resources.include_tableofcontents -%}
+{%- set tableofcontents= resources.extract_titles_from_nodebook_node(nb) -%}
Table of contents
-{%- for item in resources.tableofcontents -%}
+{%- for item in tableofcontents -%}
{%- set (level, text, id, href) = item -%}