9
9
from django .utils .translation import gettext_lazy as _
10
10
from django_hosts .resolvers import reverse
11
11
from docutils .core import publish_parts
12
+ from markdown import markdown
13
+ from markdown .extensions .toc import TocExtension , slugify as _md_title_slugify
12
14
13
15
BLOG_DOCUTILS_SETTINGS = {
14
16
"doctitle_xform" : False ,
20
22
BLOG_DOCUTILS_SETTINGS .update (getattr (settings , "BLOG_DOCUTILS_SETTINGS" , {}))
21
23
22
24
25
+ def _md_slugify (value , separator ):
26
+ # matches the `id_prefix` setting of BLOG_DOCUTILS_SETTINGS
27
+ return "s" + separator + _md_title_slugify (value , separator )
28
+
29
+
23
30
class EntryQuerySet (models .QuerySet ):
24
31
def published (self ):
25
32
return self .active ().filter (pub_date__lte = timezone .now ())
@@ -31,6 +38,7 @@ def active(self):
31
38
class ContentFormat (models .TextChoices ):
32
39
REST = "reST" , "reStructuredText"
33
40
HTML = "html" , "Raw HTML"
41
+ MARKDOWN = "md" , "Markdown"
34
42
35
43
@classmethod
36
44
def to_html (cls , fmt , source ):
@@ -45,6 +53,15 @@ def to_html(cls, fmt, source):
45
53
writer_name = "html" ,
46
54
settings_overrides = BLOG_DOCUTILS_SETTINGS ,
47
55
)["fragment" ]
56
+ if fmt == cls .MARKDOWN :
57
+ return markdown (
58
+ source ,
59
+ output_format = "html" ,
60
+ extensions = [
61
+ # baselevel matches `initial_header_level` from BLOG_DOCUTILS_SETTINGS
62
+ TocExtension (baselevel = 3 , slugify = _md_slugify ),
63
+ ],
64
+ )
48
65
raise ValueError (f"Unsupported format { fmt } " )
49
66
50
67
0 commit comments