<nav>
  <h2>On this page</h2>

  {% if page %}
  {% set toc = page.toc %}
  {% elif section %}
  {% set toc= section.toc %}
  {% endif %}
  <ul>
    <li><a href="#main">Top</a></li>
    {% for h1 in toc %}
    <li><a href="{{h1.permalink | safe }}">{{ h1.title }}</a></li>
    {% endfor %}
    <li><a href="#footer">Footer</a></li>
  </ul>
</nav>