Skip to content

Commit

Permalink
Change method extract_titles_from_markdown_input to extract_titles_fr…
Browse files Browse the repository at this point in the history
…om_notebook_node, include it in the resources dictionary and use it directly in the index.html.j2 template.
  • Loading branch information
HaudinFlorence committed Oct 14, 2024
1 parent cdc2743 commit de2ff4e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 2 additions & 8 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
13 changes: 10 additions & 3 deletions nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion share/templates/lab/index.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ a.anchor-link {
<main>
{%- block tableofcontents -%}
{%- if resources.include_tableofcontents -%}
{%- set tableofcontents= resources.extract_titles_from_nodebook_node(nb) -%}
<div class="jp-RenderedHTMLTOC-Title">Table of contents</div>
{%- for item in resources.tableofcontents -%}
{%- for item in tableofcontents -%}
{%- set (level, text, id, href) = item -%}
<div class="
{%- if level==1 -%}
Expand Down

0 comments on commit de2ff4e

Please sign in to comment.