Do not show empty ToC boxes

This commit is contained in:
akp 2025-01-19 00:17:58 +00:00
parent 866660b81a
commit d3ce9e072b
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
4 changed files with 7 additions and 3 deletions

View file

@ -7,6 +7,7 @@ import mistune.toc
import pygments import pygments
import pygments.lexers import pygments.lexers
import pygments.formatters import pygments.formatters
import html
class LevelAdjustingHTMLRenderer(mistune.HTMLRenderer): class LevelAdjustingHTMLRenderer(mistune.HTMLRenderer):
@ -89,4 +90,5 @@ def create(
def render_toc_from_state(render_state: dict[str, Any], min_level: int = 1) -> str: def render_toc_from_state(render_state: dict[str, Any], min_level: int = 1) -> str:
return mistune.toc.render_toc_ul(filter(lambda x: x[0] >= min_level, render_state.env["toc_items"])) # hilarious note: if you supply a raw filter object, this dies. hence the cast to list. see https://github.com/lepture/mistune/pull/407
return mistune.toc.render_toc_ul(list(filter(lambda x: x[0] >= min_level, render_state.env["toc_items"])))

2
poetry.lock generated
View file

@ -282,4 +282,4 @@ files = [
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "e9b4ebba9c7c7c01dc374f54ee71e90cc08f549ae8fa175cf7c651407fd744d0" content-hash = "d7f23cd48511949feb0baad457ed037727428a0cb01b734c4437d5831254a7eb"

View file

@ -11,7 +11,7 @@ Jinja2 = "^3.1.2"
PyYAML = "^6.0.1" PyYAML = "^6.0.1"
fire = "^0.7.0" fire = "^0.7.0"
rich = "^13.7.0" rich = "^13.7.0"
mistune = "^3.0.2" mistune = "^3.1.0"
pygments = "^2.17.2" pygments = "^2.17.2"

View file

@ -1,7 +1,9 @@
{# :t content = str #} {# :t content = str #}
{% macro render(content) -%} {% macro render(content) -%}
{% if content != "" %}
<div class="toc-container"> <div class="toc-container">
<div class="header"><span class="font--monospace-bold h4">On this page</span></div> <div class="header"><span class="font--monospace-bold h4">On this page</span></div>
<div class="content">{{ content | safe }}</div> <div class="content">{{ content | safe }}</div>
</div> </div>
{% endif %}
{%- endmacro %} {%- endmacro %}