toshimaru / jekyll-toc

Jekyll plugin which generates a table of contents.
MIT License
344 stars 52 forks source link

Customize automatically generated TOC on jekyll #120

Open meera1002 opened 4 years ago

meera1002 commented 4 years ago

In _config.yml I have the following setting that ensures that only h2 elements show up in the automatically generated table of contents. _toc: min_level: 2 # default: 1 maxlevel: 2 # default: 6 This works fine, but on some pages, I would like to include h3 elements in the toc as well. So I want to change the max_level to 3 in a particular case. Is this possible? I want to change max_level value dynamically from the template code. Please help

daipom commented 2 months ago

I want to specify min_level and max_level per post.

For example, it would be nice to be able to specify them as follows.

---
layout: post
title: "Welcome to Jekyll!"
toc:
  min_level: 2
---
schmittnieto commented 2 months ago

Hi @daipom

currently in the main repository of Minimal Mistakes there is this file https://github.com/mmistakes/minimal-mistakes/blob/master/_includes/toc.html

Which allows using the following command {% include toc.html=content html=content sanitize=true class=“inline_toc” id=“my_toc” h_min=2 h_max=3 %} to solve what we are asking for.

This should be added after the front matter on the first line before the article text. For this the toc: true should be removed from the front matter as well.

This afternoon if I have some time I'll take a look at it and let you know if it works as I told you.

Best regards, Cristian

daipom commented 2 months ago

@schmittnieto Thanks! https://github.com/allejo/jekyll-toc works!

{% if page.toc %}
  {% include toc.html html=content h_min=page.toc.h_min h_max=page.toc.h_max %}
{% endif %}
---
toc:
  h_min: 3
---

My problem is solved. Thanks!