Consistent markdown parsing, docs on markdown and display settings

This commit is contained in:
Raphael Michel
2017-12-03 15:19:15 +01:00
parent 1aef721794
commit 98a58779ad
14 changed files with 259 additions and 10 deletions

View File

@@ -16,6 +16,7 @@ from pretix.base.i18n import language
from pretix.base.models import Event, Invoice, InvoiceAddress, Order
from pretix.base.services.invoices import invoice_pdf_task
from pretix.base.signals import email_filter
from pretix.base.templatetags.rich_text import markdown_compile
from pretix.celery_app import app
from pretix.multidomain.urlreverse import build_absolute_uri
@@ -210,11 +211,8 @@ def render_mail(template, context):
body = str(template)
if context:
body = body.format_map(TolerantDict(context))
body_md = bleach.linkify(bleach.clean(markdown.markdown(body), tags=bleach.ALLOWED_TAGS + [
'p', 'pre'
]))
else:
tpl = get_template(template)
body = tpl.render(context)
body_md = bleach.linkify(markdown.markdown(body))
body_md = bleach.linkify(markdown_compile(body))
return body, body_md

View File

@@ -35,12 +35,14 @@ ALLOWED_TAGS = [
'th',
'div',
'span',
'hr',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
# Update doc/user/markdown.rst if you change this!
]
ALLOWED_ATTRIBUTES = {
@@ -52,6 +54,7 @@ ALLOWED_ATTRIBUTES = {
'div': ['class'],
'p': ['class'],
'span': ['class'],
# Update doc/user/markdown.rst if you change this!
}
@@ -72,15 +75,28 @@ def abslink_callback(attrs, new=False):
return attrs
def markdown_compile(source):
return bleach.clean(
markdown.markdown(
source,
extensions=[
'markdown.extensions.sane_lists',
# 'markdown.extensions.nl2br', # TODO: Enable, but check backwards-compatibility issues e.g. with mails
]
),
tags=ALLOWED_TAGS,
attributes=ALLOWED_ATTRIBUTES
)
@register.filter
def rich_text(text: str, **kwargs):
"""
Processes markdown and cleans HTML in a text input.
"""
text = str(text)
body_md = bleach.linkify(bleach.clean(
markdown.markdown(text),
tags=ALLOWED_TAGS,
attributes=ALLOWED_ATTRIBUTES,
), callbacks=DEFAULT_CALLBACKS + ([safelink_callback] if kwargs.get('safelinks', True) else [abslink_callback]))
body_md = bleach.linkify(
markdown_compile(text),
callbacks=DEFAULT_CALLBACKS + ([safelink_callback] if kwargs.get('safelinks', True) else [abslink_callback])
)
return mark_safe(body_md)