summaryrefslogtreecommitdiffhomepage
path: root/apioforum/mdrender.py
blob: 5f5292d71080032fbeee1ee4ee946eda9481cf98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import bleach

allowed_tags = [
    'p',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'pre',
    'del',
    'mark',
    'img',
]

allowed_attributes = bleach.sanitizer.ALLOWED_ATTRIBUTES.copy()
allowed_attributes.update(
    img='src',
)

allowed_tags.extend(bleach.sanitizer.ALLOWED_TAGS)

cleaner = bleach.sanitizer.Cleaner(tags=allowed_tags,attributes=allowed_attributes)

import markdown
md = markdown.Markdown(extensions=['pymdownx.tilde','fenced_code'])

def render(text):
    text = md.reset().convert(text)
    text = cleaner.clean(text)
    return text