Skip to content

Commit

Permalink
Add an inline table of contents for small screens
Browse files Browse the repository at this point in the history
This ToC is only shown when the screen is too small for the sticky sidebar ToC.

We use a `<!-- toc -->` comment and the `replace` function to allow inserting the ToC at well defined points of the page. We don't use the shortcode feature of Zola because of getzola/zola#584.
  • Loading branch information
phil-opp committed Mar 19, 2019
1 parent d0c0cd8 commit 6055a2e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions blog/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ aside#all-posts-link {
}

@media (min-width: 80rem) {
#toc-inline {
display: none;
}

#toc-aside {
display: block;
width: 12rem;
Expand Down
16 changes: 16 additions & 0 deletions blog/templates/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ <h2 class="post-title"><a href="/{{ page.path | safe }}">{{ page.title }}</a></h
async>
</script>
{% endmacro utterances %}

{% macro toc(page) %}
<details id = "toc-inline">
<summary><b>Table of Contents</b></summary>
<ul>
{% for h2 in page.toc %}<li>
<a href="#{{h2.id | safe}}">{{ h2.title | safe }}</a>
{% if h2.children %}<ul>
{% for h3 in h2.children %}<li>
<a href="#{{h3.id | safe}}">{{ h3.title | safe }}</a>
</li>{% endfor %}
</ul>{% endif %}
</li>{% endfor %}
</ul>
</details>
{% endmacro toc %}
2 changes: 1 addition & 1 deletion blog/templates/second-edition/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h2>Table of Contents</h2>
</li>{% endfor %}
</ol>
</aside>
{{ page.content | safe }}
{{ page.content | safe | replace(from="<!-- toc -->", to=macros::toc(page=page))}}

<div class="post-footer-support">
<h2>Support Me</h2>
Expand Down

0 comments on commit 6055a2e

Please sign in to comment.