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 }}