2020-09-18 19:43:10 +02:00
|
|
|
from app import app
|
|
|
|
from markupsafe import Markup
|
|
|
|
from markdown import markdown
|
2021-02-20 15:39:52 +01:00
|
|
|
from markdown.extensions.codehilite import CodeHiliteExtension
|
|
|
|
from markdown.extensions.footnotes import FootnoteExtension
|
|
|
|
from markdown.extensions.toc import TocExtension
|
2021-02-23 13:30:09 +01:00
|
|
|
from bleach import clean
|
|
|
|
from app.utils.bleach_allowlist import markdown_tags, markdown_attrs
|
2021-02-20 15:39:52 +01:00
|
|
|
|
2021-02-20 17:17:33 +01:00
|
|
|
from app.utils.markdown_extensions.pclinks import PCLinkExtension
|
2021-02-24 00:06:49 +01:00
|
|
|
from app.utils.markdown_extensions.hardbreaks import HardBreakExtension
|
|
|
|
from app.utils.markdown_extensions.escape_html import EscapeHtmlExtension
|
2021-02-24 00:38:26 +01:00
|
|
|
from app.utils.markdown_extensions.linkify import LinkifyExtension
|
2021-02-20 17:17:33 +01:00
|
|
|
|
2020-09-18 19:43:10 +02:00
|
|
|
|
|
|
|
@app.template_filter('md')
|
|
|
|
def md(text):
|
|
|
|
"""
|
|
|
|
Converts markdown to html5
|
|
|
|
"""
|
2021-02-20 15:39:52 +01:00
|
|
|
|
|
|
|
options = 0
|
|
|
|
extensions = [
|
|
|
|
# 'admonition',
|
|
|
|
'fenced_code',
|
2021-02-23 13:30:09 +01:00
|
|
|
# 'nl2br',
|
2021-02-20 15:39:52 +01:00
|
|
|
'sane_lists',
|
|
|
|
'tables',
|
|
|
|
CodeHiliteExtension(linenums=True, use_pygments=True),
|
2021-02-24 00:06:49 +01:00
|
|
|
EscapeHtmlExtension(),
|
2021-02-20 15:39:52 +01:00
|
|
|
FootnoteExtension(UNIQUE_IDS=True),
|
2021-02-24 00:06:49 +01:00
|
|
|
HardBreakExtension(),
|
2021-02-24 00:38:26 +01:00
|
|
|
LinkifyExtension(),
|
2021-02-20 15:39:52 +01:00
|
|
|
TocExtension(baselevel=2),
|
2021-02-20 17:17:33 +01:00
|
|
|
PCLinkExtension(),
|
2021-02-20 15:39:52 +01:00
|
|
|
]
|
|
|
|
|
2021-02-23 13:30:09 +01:00
|
|
|
html = markdown(text, options=options, extensions=extensions)
|
|
|
|
out = clean(html, markdown_tags, markdown_attrs)
|
2021-02-20 15:39:52 +01:00
|
|
|
|
|
|
|
return Markup(out)
|