Skip to content

Commit be211c1

Browse files
feat: hide toc button if toc empty
1 parent cb4450b commit be211c1

File tree

6 files changed

+50
-32
lines changed

6 files changed

+50
-32
lines changed

myhpi/core/templates/core/information_page.html

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
{% block content %}
77
{% with page.body|markdown as parsed_md %}
8+
{% include "core/toc_button.html" with toc=parsed_md.1 %}
89
<div class="row flex-column flex-lg-row">
910
<div class="col-lg-9">
1011
<h1 class="page-title">

myhpi/core/templates/core/minutes.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
{% load bootstrap_icons %}
77

88
{% block content %}
9-
<div class="row flex-column flex-lg-row">
10-
{% with page.body|markdown as parsed_md %}
9+
{% with page.body|markdown as parsed_md %}
10+
{% include "core/toc_button.html" with toc=parsed_md.1 %}
11+
<div class="row flex-column flex-lg-row">
1112
<h1 class="page-title">
1213
<span class="underline">
1314
{{ page.title }}

myhpi/core/templates/core/sidebar.html

+26-22
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ <h1 class="side-panel-title">{% translate "Visibility" %}</h1>
2020
<!-- Bootstrap 5.2 supports responsive offcanvas.
2121
This duplication is a workaround and should be removed after the upgrade -->
2222
<div class="d-none d-lg-block side-panel-container">
23+
{%if parsed_md.1|hasTocContent %}
2324
<aside class="side-panel border-accent sidebar-container d-print-none">
2425
<h1 class="side-panel-title">{% translate "Table of contents" %}</h1>
2526
{{ parsed_md.1 }}
2627
</aside>
28+
{% endif %}
2729
{% if page.attachments.all %}
2830
<aside class="side-panel border-accent sidebar-container d-print-none">
2931
<h1 class="side-panel-title">{% translate "Attachments" %}</h1>
@@ -38,28 +40,30 @@ <h1 class="side-panel-title">{% translate "Attachments" %}</h1>
3840
{% endif %}
3941
</div>
4042

41-
<div class="offcanvas offcanvas-bottom" tabindex="-1" id="sidebar-offcanvas" aria-labelledby="sidebar-offcanvas-label">
42-
<div class="offcanvas-header">
43-
<h5 class="offcanvas-title" id="sidebar-offcanvas-label">{% translate "Table of contents" %}</h5>
44-
<button type="button" class="btn-close" data-bs-dismiss="offcanvas"
45-
aria-label="{% translate 'Close' %}"></button>
46-
</div>
47-
<div class="offcanvas-body">
48-
<aside class="side-panel border-accent sidebar-container d-print-none">
49-
<h1 class="side-panel-title">{% translate "Table of contents" %}</h1>
50-
{{ parsed_md.1 }}
51-
</aside>
52-
{% if page.attachments.all %}
43+
{%if parsed_md.1|hasTocContent %}
44+
<div class="offcanvas offcanvas-bottom" tabindex="-1" id="sidebar-offcanvas" aria-labelledby="sidebar-offcanvas-label">
45+
<div class="offcanvas-header">
46+
<h5 class="offcanvas-title" id="sidebar-offcanvas-label">{% translate "Table of contents" %}</h5>
47+
<button type="button" class="btn-close" data-bs-dismiss="offcanvas"
48+
aria-label="{% translate 'Close' %}"></button>
49+
</div>
50+
<div class="offcanvas-body">
5351
<aside class="side-panel border-accent sidebar-container d-print-none">
54-
<h1 class="side-panel-title">{% translate "Attachments" %}</h1>
55-
<ul>
56-
{% for attachment in page.attachments.all %}
57-
<li>
58-
<a href="{{ attachment.url }}">{{ attachment.title }}</a>
59-
</li>
60-
{% endfor %}
61-
</ul>
52+
<h1 class="side-panel-title">{% translate "Table of contents" %}</h1>
53+
{{ parsed_md.1 }}
6254
</aside>
63-
{% endif %}
55+
{% if page.attachments.all %}
56+
<aside class="side-panel border-accent sidebar-container d-print-none">
57+
<h1 class="side-panel-title">{% translate "Attachments" %}</h1>
58+
<ul>
59+
{% for attachment in page.attachments.all %}
60+
<li>
61+
<a href="{{ attachment.url }}">{{ attachment.title }}</a>
62+
</li>
63+
{% endfor %}
64+
</ul>
65+
</aside>
66+
{% endif %}
67+
</div>
6468
</div>
65-
</div>
69+
{% endif %}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- usage requires Table of Contents HTML in include context: https://docs.djangoproject.com/en/5.0/ref/templates/builtins/#include -->
2+
3+
{% load core_extras %}
4+
{% load i18n %}
5+
{% load bootstrap_icons %}
6+
7+
{%if toc|hasTocContent %}
8+
<div id="sidebar-toggle" class="d-grid d-lg-none d-print-none">
9+
<button type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebar-offcanvas"
10+
aria-controls="sidebar-offcanvas" class="btn btn-light mb-3" aria-label="{% translate 'Table of contents' %}">
11+
{% bs_icon 'list-ol' size='1.3rem' %}
12+
{% translate "Table of contents" %}
13+
</button>
14+
</div>
15+
{% endif %}

myhpi/core/templatetags/core_extras.py

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def insert_footer(page):
3535
return {"footer_columns": [footer.column_1, footer.column_2, footer.column_3], "page": page}
3636

3737

38+
@register.filter()
39+
def hasTocContent(toc):
40+
return "li" in toc
41+
42+
3843
@register.filter(name="tag_external_links")
3944
def tag_external_links(content):
4045
"""Takes the content of a website and inserts external link icons after every external link."""

myhpi/templates/base.html

-8
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,6 @@
150150
</nav>
151151
{% endif %}
152152

153-
<div id="sidebar-toggle" class="d-grid d-lg-none d-print-none">
154-
<button type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebar-offcanvas"
155-
aria-controls="sidebar-offcanvas" class="btn btn-light mb-3" aria-label="{% translate 'Table of contents' %}">
156-
{% bs_icon 'list-ol' size='1.3rem' %}
157-
{% translate "Table of contents" %}
158-
</button>
159-
</div>
160-
161153
{% block content %}{% endblock %}
162154
</div>
163155
</div>

0 commit comments

Comments
 (0)