Fixed navigation/toc issues when h1 is present

This commit is contained in:
squidfunk 2016-02-05 15:34:19 +01:00
parent 7d1a71e9a5
commit e03c58f487
4 changed files with 75 additions and 44 deletions

View File

@ -53,7 +53,7 @@
<!-- Webfonts -->
<link rel="stylesheet" type="text/css"
href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,700" />
href="https://fonts.googleapis.com/css?family=Ubuntu:400,700" />
<link rel="stylesheet" type="text/css"
href="https://fonts.googleapis.com/css?family=Ubuntu+Mono" />
@ -95,6 +95,14 @@
<!-- Main content -->
<main class="main">
<!--
This is a nasty hack that checks whether the content contains a
h1 headline. If it does, the variable h1 is set to true. This is
necessary for correctly rendering the table of contents which is
embedded into the navigation and the actual headline.
-->
{% set h1 = "\x3c/h1\x3e" in content %}
<!-- Drawer with navigation -->
<div class="drawer">
{% include "drawer.html" %}
@ -105,12 +113,13 @@
<div class="wrapper">
<!-- Headline -->
{% if page_title %}
<h1>{{ page_title }}</h1>
{% else %}
<h1>{{ site_name }}</h1>
{% if not h1 %}
{% if page_title %}
<h1>{{ page_title }}</h1>
{% else %}
<h1>{{ site_name }}</h1>
{% endif %}
{% endif %}
<hr />
<!-- Article content -->
{{ content }}

View File

@ -58,7 +58,7 @@
<!-- Table of contents -->
<ul class="toc">
{% for nav_item in nav %}
{% include "toc.html" %}
{% include "nav.html" %}
{% endfor %}
<!-- Author-related links -->

59
src/nav.html Normal file
View File

@ -0,0 +1,59 @@
<!-- Render sections -->
{% if nav_item.children %}
<li>
<hr />
<span class="section">{{ nav_item.title }}</span>
<ul>
<!-- Render pages of section -->
{% for nav_item in nav_item.children %}
{% include 'nav.html' %}
{% endfor %}
</ul>
</li>
<!-- Render page link -->
{% else %}
<li>
<a class="{% if nav_item.active %}current{% endif %}"
title="{{ nav_item.title }}" href="{{ nav_item.url }}" >
{{ nav_item.title }}
</a>
<!-- Expand active pages -->
{% if nav_item == current_page %}
<ul>
<!-- Render anchors of active page -->
{% for toc_item in toc %}
<!--
The top-level anchor must be skipped if the article contains a h1
headline, since it would be redundant to the link to the current
page that is located just above the anchor. Therefore we directly
continue with the children of the anchor.
-->
{% if h1 %}
{% for toc_item in toc_item.children %}
<!-- Render anchor -->
<li class="anchor">
<a title="{{ toc_item.title }}" href="{{ toc_item.url }}">
{{ toc_item.title }}
</a>
</li>
{% endfor %}
{% else %}
<!-- Render anchor -->
<li class="anchor">
<a title="{{ toc_item.title }}" href="{{ toc_item.url }}">
{{ toc_item.title }}
</a>
</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
</li>
{% endif %}

View File

@ -1,37 +0,0 @@
<!-- Render sections -->
{% if nav_item.children %}
<li>
<hr />
<span class="section">{{ nav_item.title }}</span>
<ul>
<!-- Render links of section -->
{% for nav_item in nav_item.children %}
{% include 'toc.html' %}
{% endfor %}
</ul>
</li>
<!-- Render link -->
{% else %}
<li>
<a class="{% if nav_item.active %}current{% endif %}"
title="{{ nav_item.title }}" href="{{ nav_item.url }}" >
{{ nav_item.title }}
</a>
<!-- Expand active pages -->
{% if nav_item == current_page %}
<ul>
{% for toc_item in toc %}
<li class="anchor">
<a class="{% if loop.first %}current{% endif %}"
title="{{ toc_item.title }}" href="{{ toc_item.url }}">
{{ toc_item.title }}
</a>
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endif %}