From 22731310ba232cd4a92dab3ab760d714c88f555a Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 30 Apr 2024 21:23:21 +0100 Subject: [PATCH] Extract TOC --- yamdl_playground/core/models.py | 10 +++++++++- yamdl_playground/core/templates/content.html | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/yamdl_playground/core/models.py b/yamdl_playground/core/models.py index c16c2e5..7efa8bd 100644 --- a/yamdl_playground/core/models.py +++ b/yamdl_playground/core/models.py @@ -20,6 +20,7 @@ class Page(models.Model): raw_content = models.TextField() content = models.TextField() + toc = models.JSONField() slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True) tags = models.ManyToManyField(Tag) @@ -28,9 +29,16 @@ class Page(models.Model): def from_yaml(cls, **data): tags = data.pop("tags", None) + md = markdown.Markdown(extensions=["toc"]) + content = data.pop("content") data["raw_content"] = content - data["content"] = markdown.markdown(content) + + data["content"] = md.convert(content) + data["toc"] = { + "html": md.toc, + "tokens": md.toc_tokens + } instance = cls.objects.create(**data) diff --git a/yamdl_playground/core/templates/content.html b/yamdl_playground/core/templates/content.html index 3bf664b..dea49fe 100644 --- a/yamdl_playground/core/templates/content.html +++ b/yamdl_playground/core/templates/content.html @@ -2,5 +2,6 @@ {{ page.title }} + {{ page.toc.html|safe }} {{ content }}