]> git.lizzy.rs Git - cheatdb.git/blob - app/markdown.py
Fix typo
[cheatdb.git] / app / markdown.py
1 import bleach
2 from markdown import Markdown
3 from flask import Markup
4
5 # Whitelist source: MIT
6 #
7 # https://github.com/Wenzil/mdx_bleach/blob/master/mdx_bleach/whitelist.py
8
9 """
10 Default whitelist of allowed HTML tags. Any other HTML tags will be escaped or
11 stripped from the text. This applies to the html output that Markdown produces.
12 """
13 ALLOWED_TAGS = [
14         'ul',
15         'ol',
16         'li',
17         'p',
18         'pre',
19         'code',
20         'blockquote',
21         'h1',
22         'h2',
23         'h3',
24         'h4',
25         'h5',
26         'h6',
27         'hr',
28         'br',
29         'strong',
30         'em',
31         'a',
32         'img'
33 ]
34
35 """
36 Default whitelist of attributes. It allows the href and title attributes for <a>
37 tags and the src, title and alt attributes for <img> tags. Any other attribute
38 will be stripped from its tag.
39 """
40 ALLOWED_ATTRIBUTES = {
41         'a': ['href', 'title'],
42         'img': ['src', 'title', 'alt']
43 }
44
45 """
46 If you allow tags that have attributes containing a URI value
47 (like the href attribute of an anchor tag,) you may want to adapt
48 the accepted protocols. The default list only allows http, https and mailto.
49 """
50 ALLOWED_PROTOCOLS = ['http', 'https', 'mailto']
51
52
53 md = Markdown(extensions=["fenced_code"], output_format="html5")
54
55 def render_markdown(source):
56         return bleach.clean(md.convert(source), \
57                 tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, \
58                 styles=[], protocols=ALLOWED_PROTOCOLS)
59
60 def init_app(app):
61         @app.template_filter()
62         def markdown(source):
63                 return Markup(render_markdown(source))