Skip to content

Conditionally check for TOC #34

Answered by allejo
jeremyzilar asked this question in Q&A
Discussion options

You must be logged in to vote

Untested code but you'd want to use the following logic:

  1. Capture the rendered TOC
  2. Strip any whitespace around the TOC
  3. Check to see if a TOC was rendered. If there was, render the surrounding <div> and use the TOC that was captured
{% capture page_toc %}
  {% include toc.html html=content %}
{% endcapture %}
{% assign page_toc = page_toc | strip %}

{% if page_toc != "" %}
  <div class="toc">
    <h2>On this page</h2>
    {{ page_toc }}
  </div>
{% endif %}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by allejo
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #34 on December 14, 2020 02:45.