1
Fork 0

Render markdown during page import

This commit is contained in:
Jake Howard 2024-03-24 21:59:25 +00:00
parent aaf0bb4571
commit ff90e8422b
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 8 additions and 12 deletions

View file

@ -1,4 +1,5 @@
from django.db import models
import markdown
class Tag(models.Model):
__yamdl__ = True
@ -13,6 +14,7 @@ class Page(models.Model):
title = models.CharField(max_length=255)
raw_content = models.TextField()
content = models.TextField()
slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True)
@ -21,6 +23,11 @@ class Page(models.Model):
@classmethod
def from_yaml(cls, **data):
tags = data.pop("tags", None)
content = data.pop("content")
data["raw_content"] = content
data["content"] = markdown.markdown(content)
instance = cls.objects.create(**data)
if tags:

View file

@ -2,12 +2,7 @@ from .models import Page
from django.http import HttpResponse
from django.db import connections
from django.shortcuts import get_object_or_404
import markdown
from django.core.cache import caches
from threading import Lock
md = markdown.Markdown()
md_lock = Lock()
def search(request):
with connections["default"].cursor() as cursor:
@ -20,10 +15,4 @@ def search(request):
def content(request, slug):
page = get_object_or_404(Page, slug=slug)
if (cached_content := caches["default"].get(f"page-content-{slug}")) is None:
with md_lock:
cached_content = md.convert(page.content)
md.reset()
caches["default"].set(f"page-content-{slug}", cached_content)
return HttpResponse(cached_content, content_type="text/html")
return HttpResponse(page.content, content_type="text/html")