Skip to content

Commit 6b3946a

Browse files
Add links to be able to go to the relevant sections when clicking on the items of the table of contents.
1 parent fab3c55 commit 6b3946a

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ docs/source/config_options.rst
4141
.vscode
4242

4343
# Downloaded theme files
44-
share/templates/lab/static/index.css
44+
#share/templates/lab/static/index.css
4545
share/templates/lab/static/theme-dark.css
4646
share/templates/lab/static/theme-light.css
4747
share/templates/classic/static/style.css

nbconvert/filters/markdown_mistune.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@ def heading(self, text, level):
501501
self.headings.append((level, text))
502502
return "" # We return an empty string to avoid outputting the headings
503503

504-
505504
def extract_titles_from_markdown_input(markdown_input):
506505
# Markdown_input is a single string with all the markdown content concatenated
507506
# Initiate list of titles
@@ -517,15 +516,14 @@ def extract_titles_from_markdown_input(markdown_input):
517516
extract_titles(markdown_input)
518517

519518
# renderer.headings is an array for each markdown element
520-
# print(renderer.headings)
521519

522520
# Extracted headings
523521
for level, title in renderer.headings:
524522
children = title["children"]
525523
attrs = title["attrs"]
526524
raw_text = children[0]["raw"]
527525
level = attrs["level"]
528-
titles_array.append([level, raw_text])
529-
530-
print(titles_array)
526+
id = raw_text.replace(' ', '-')
527+
href= "#" + id
528+
titles_array.append([level, raw_text, id, href])
531529
return titles_array

share/templates/lab/index.html.j2

+34-7
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,54 @@ a.anchor-link {
126126
<body class="jp-Notebook" data-jp-theme-light="true" data-jp-theme-name="JupyterLab Light">
127127
{% endif %}
128128
<main>
129+
{%- block tableofcontents -%}
130+
<div class="jp-RenderedHTMLTOC-Title">Table of contents</div>
129131
{%- for item in resources.tableofcontents -%}
130-
{%- set (level, text) = item -%}
132+
{%- set (level, text, id, href) = item -%}
131133
{%- if level==1 -%}
132-
<h1>{{text}}</h1>
134+
<div class="jp-RenderedHTMLCommon jp-RenderedHTMLTOC-Item-h1">
135+
<a href={{href}}>
136+
{{text}}
137+
</a>
138+
</div>
133139
{%- endif -%}
134140
{%- if level==2 -%}
135-
<h2>{{text}}</h2>
141+
<div class="jp-RenderedHTMLCommon jp-RenderedHTMLTOC-Item-h2">
142+
<a href={{href}}>
143+
{{text}}
144+
</a>
145+
</div>
136146
{%- endif -%}
137147
{%- if level==3 -%}
138-
<h3>{{text}}</h3>
148+
<div class="jp-RenderedHTMLCommon jp-RenderedHTMLTOC-Item-h3">
149+
<a href={{href}}>
150+
{{text}}
151+
</a>
152+
</div>
139153
{%- endif -%}
140154
{%- if level==4 -%}
141-
<h4>{{text}}</h4>
155+
<div class="jp-RenderedHTMLCommon jp-RenderedHTMLTOC-Item-h4">
156+
<a href={{href}}>
157+
{{text}}
158+
</a>
159+
</div>
142160
{%- endif -%}
143161
{%- if level==5 -%}
144-
<h5>{{text}}</h5>
162+
<div class="jp-RenderedHTMLCommon jp-RenderedHTMLTOC-Item-h5">
163+
<a href={{href}}>
164+
{{text}}
165+
</a>
166+
</div>
145167
{%- endif -%}
146168
{%- if level==6 -%}
147-
<h6>{{text}}</h6>
169+
<div class="jp-RenderedHTMLCommon jp-RenderedHTMLTOC-Item-h6">
170+
<a href={{href}}>
171+
{{text}}
172+
</a>
173+
</div>
148174
{%- endif -%}
149175
{%- endfor -%}
176+
{% endblock tableofcontents %}
150177

151178
{%- endblock body_header -%}
152179

share/templates/lab/static/index.css

-1
Original file line numberDiff line numberDiff line change
@@ -4043,7 +4043,6 @@ button.jp-Dialog-close-button {
40434043
font-weight: bold;
40444044
}
40454045

4046-
40474046
.jp-RenderedHTMLTOC-Item-h1 {
40484047
font-family: var(--jp-content-font-family);
40494048
font-size: 20px;

0 commit comments

Comments
 (0)