Skip to content

Commit

Permalink
feat: hide toc button if toc empty
Browse files Browse the repository at this point in the history
  • Loading branch information
SilvanVerhoeven committed Jul 29, 2024
1 parent adfe1b4 commit b058128
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 32 deletions.
1 change: 1 addition & 0 deletions myhpi/core/templates/core/information_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

{% block content %}
{% with page.body|markdown as parsed_md %}
{% include "core/toc_button.html" with toc=parsed_md.1 %}
<div class="row flex-column flex-lg-row">
<div class="col-lg-9">
<h1 class="page-title">
Expand Down
5 changes: 3 additions & 2 deletions myhpi/core/templates/core/minutes.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
{% load bootstrap_icons %}

{% block content %}
<div class="row flex-column flex-lg-row">
{% with page.body|markdown as parsed_md %}
{% with page.body|markdown as parsed_md %}
{% include "core/toc_button.html" with toc=parsed_md.1 %}
<div class="row flex-column flex-lg-row">
<h1 class="page-title">
<span class="underline">
{{ page.title }}
Expand Down
48 changes: 26 additions & 22 deletions myhpi/core/templates/core/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ <h1 class="side-panel-title">{% translate "Visibility" %}</h1>
<!-- Bootstrap 5.2 supports responsive offcanvas.
This duplication is a workaround and should be removed after the upgrade -->
<div class="d-none d-lg-block side-panel-container">
{%if parsed_md.1|hasTocContent %}
<aside class="side-panel border-accent sidebar-container d-print-none">
<h1 class="side-panel-title">{% translate "Table of contents" %}</h1>
{{ parsed_md.1 }}
</aside>
{% endif %}
{% if page.attachments.all %}
<aside class="side-panel border-accent sidebar-container d-print-none">
<h1 class="side-panel-title">{% translate "Attachments" %}</h1>
Expand All @@ -38,28 +40,30 @@ <h1 class="side-panel-title">{% translate "Attachments" %}</h1>
{% endif %}
</div>

<div class="offcanvas offcanvas-bottom" tabindex="-1" id="sidebar-offcanvas" aria-labelledby="sidebar-offcanvas-label">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="sidebar-offcanvas-label">{% translate "Table of contents" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas"
aria-label="{% translate 'Close' %}"></button>
</div>
<div class="offcanvas-body">
<aside class="side-panel border-accent sidebar-container d-print-none">
<h1 class="side-panel-title">{% translate "Table of contents" %}</h1>
{{ parsed_md.1 }}
</aside>
{% if page.attachments.all %}
{%if parsed_md.1|hasTocContent %}
<div class="offcanvas offcanvas-bottom" tabindex="-1" id="sidebar-offcanvas" aria-labelledby="sidebar-offcanvas-label">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="sidebar-offcanvas-label">{% translate "Table of contents" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas"
aria-label="{% translate 'Close' %}"></button>
</div>
<div class="offcanvas-body">
<aside class="side-panel border-accent sidebar-container d-print-none">
<h1 class="side-panel-title">{% translate "Attachments" %}</h1>
<ul>
{% for attachment in page.attachments.all %}
<li>
<a href="{{ attachment.url }}">{{ attachment.title }}</a>
</li>
{% endfor %}
</ul>
<h1 class="side-panel-title">{% translate "Table of contents" %}</h1>
{{ parsed_md.1 }}
</aside>
{% endif %}
{% if page.attachments.all %}
<aside class="side-panel border-accent sidebar-container d-print-none">
<h1 class="side-panel-title">{% translate "Attachments" %}</h1>
<ul>
{% for attachment in page.attachments.all %}
<li>
<a href="{{ attachment.url }}">{{ attachment.title }}</a>
</li>
{% endfor %}
</ul>
</aside>
{% endif %}
</div>
</div>
</div>
{% endif %}
15 changes: 15 additions & 0 deletions myhpi/core/templates/core/toc_button.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- usage requires Table of Contents HTML in include context: https://docs.djangoproject.com/en/5.0/ref/templates/builtins/#include -->

{% load core_extras %}
{% load i18n %}
{% load bootstrap_icons %}

{%if toc|hasTocContent %}
<div id="sidebar-toggle" class="d-grid d-lg-none d-print-none">
<button type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebar-offcanvas"
aria-controls="sidebar-offcanvas" class="btn btn-light mb-3" aria-label="{% translate 'Table of contents' %}">
{% bs_icon 'list-ol' size='1.3rem' %}
{% translate "Table of contents" %}
</button>
</div>
{% endif %}
5 changes: 5 additions & 0 deletions myhpi/core/templatetags/core_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def insert_footer(page):
return {"footer_columns": [footer.column_1, footer.column_2, footer.column_3], "page": page}


@register.filter()
def hasTocContent(toc):
return "li" in toc


@register.filter(name="tag_external_links")
def tag_external_links(content):
"""Takes the content of a website and inserts external link icons after every external link."""
Expand Down
8 changes: 0 additions & 8 deletions myhpi/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,6 @@
</nav>
{% endif %}

<div id="sidebar-toggle" class="d-grid d-lg-none d-print-none">
<button type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebar-offcanvas"
aria-controls="sidebar-offcanvas" class="btn btn-light mb-3" aria-label="{% translate 'Table of contents' %}">
{% bs_icon 'list-ol' size='1.3rem' %}
{% translate "Table of contents" %}
</button>
</div>

{% block content %}{% endblock %}
</div>
</div>
Expand Down

0 comments on commit b058128

Please sign in to comment.