Commit 5117cfa4 authored by berumuron's avatar berumuron
Browse files

add: Convert description to Markdown

Content is sanitized to avoid XSS attacks and tags which could break the
layout or design (such as h* titles and images).
parent 0176a989
bleach==3.1.0
Click==7.0
Flask==1.0.3
itsdangerous==1.1.0
Jinja2==2.10.1
Markdown==3.1.1
MarkupSafe==1.1.1
six==1.12.0
webencodings==0.5.1
Werkzeug==0.15.4
......@@ -25,3 +25,5 @@ packages = find:
python_requires = >= 3.6
install_requires =
Flask>=1.0.3
Markdown>=3.1.1
bleach>=3.1.0
from datetime import datetime
from flask import Markup
import bleach
import markdown
ALLOWED_TAGS = [
"ul",
"ol",
"li",
"p",
"pre",
"code",
"blockquote",
"br",
"strong",
"em",
"a",
]
def to_month_filter(number):
"""Convert a number to its equivalent month string."""
return datetime.strptime(str(number), "%m").strftime("%B")
_markdown_instance = None
def markdown_filter(text):
global _markdown_instance
if _markdown_instance is None:
_markdown_instance = markdown.Markdown(extensions=["nl2br"])
unsafe_html = _markdown_instance.convert(text)
sanitized_html = bleach.clean(unsafe_html, tags=ALLOWED_TAGS)
return Markup(sanitized_html)
def init_app(app):
"""Register filters to the application Jinja."""
app.jinja_env.filters["to_month"] = to_month_filter
app.jinja_env.filters["markdown"] = markdown_filter
......@@ -43,9 +43,9 @@
{{ event.title }}
</button>
<p class="event-description folder-container">
{{ event.description|replace('\n', '<br />'|safe) }}
</p>
<div class="event-description folder-container">
{{ event.description|markdown }}
</div>
{% else %}
{{ event.title }}
{% endif %}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment