[gi-docgen/nielsdg/toc-sidebar-for-content-files] content files: generate a navbar with the headers




commit 626a01a6f90919ae0b02361b3b5dbaa48c49fc57
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri Jan 21 20:17:55 2022 +0100

    content files: generate a navbar with the headers
    
    We're already using the ["toc"] extension of the Markdown module, which
    allows us to get the header hierarchy after parsing as a list headers,
    each represented as a recursive `dict`.
    
    ["toc"]: https://python-markdown.github.io/extensions/toc/

 gidocgen/gdgenerate.py                |  1 +
 gidocgen/templates/basic/content.html | 15 +++++++++++++++
 2 files changed, 16 insertions(+)
---
diff --git a/gidocgen/gdgenerate.py b/gidocgen/gdgenerate.py
index fa286b0..25b1abc 100644
--- a/gidocgen/gdgenerate.py
+++ b/gidocgen/gdgenerate.py
@@ -2584,6 +2584,7 @@ def gen_content_files(config, theme_config, content_dirs, output_dir, jinja_env,
             "output_file": content_file,
             "meta": md.Meta,
             "title": title,
+            "toc": md.toc_tokens,
             "data": dst_data,
         }
 
diff --git a/gidocgen/templates/basic/content.html b/gidocgen/templates/basic/content.html
index c6d51aa..a8268ef 100644
--- a/gidocgen/templates/basic/content.html
+++ b/gidocgen/templates/basic/content.html
@@ -24,3 +24,18 @@ SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later
   </section>
 </section>
 {% endblock %}
+
+{% if content.toc|length > 0 %}
+{% block navbar %}
+<div id="toc" class="toc">
+  <nav aria-labelledby="toc-title">
+    <p id="toc-title">Content</p>
+    <ul class="toc-list">
+      {% for header in content.toc %}
+      <li class="toc-list-item"><a href="#{{ header.id }}"><span class="link-text">{{ header.name 
}}</span></a></li>
+      {% endfor %}
+    </ul>
+  </nav>
+</div>
+{% endblock %}
+{% endif %}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]