1
Fork 0

Extract TOC

This commit is contained in:
Jake Howard 2024-04-30 21:23:21 +01:00
parent 3a32e32fca
commit 22731310ba
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 10 additions and 1 deletions

View file

@ -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)

View file

@ -2,5 +2,6 @@
<title>{{ page.title }}</title>
</head>
<body>
{{ page.toc.html|safe }}
{{ content }}
</body>