2021-02-02 19:16:55 +01:00
|
|
|
from functools import partial
|
|
|
|
|
2020-01-22 23:10:02 +01:00
|
|
|
import bleach
|
2021-02-02 19:16:55 +01:00
|
|
|
from bleach import Cleaner
|
|
|
|
from bleach.linkifier import LinkifyFilter
|
2021-02-02 21:05:24 +01:00
|
|
|
from bs4 import BeautifulSoup
|
2020-01-22 23:10:02 +01:00
|
|
|
from markdown import Markdown
|
|
|
|
from flask import Markup
|
2021-08-21 06:40:20 +02:00
|
|
|
from markdown.extensions import Extension
|
|
|
|
from markdown.inlinepatterns import SimpleTagInlineProcessor
|
|
|
|
|
2020-01-22 23:10:02 +01:00
|
|
|
|
2021-02-02 19:16:55 +01:00
|
|
|
# Based on
|
2020-01-22 23:10:02 +01:00
|
|
|
# https://github.com/Wenzil/mdx_bleach/blob/master/mdx_bleach/whitelist.py
|
2021-02-02 19:16:55 +01:00
|
|
|
#
|
|
|
|
# License: MIT
|
2020-01-22 23:10:02 +01:00
|
|
|
|
|
|
|
ALLOWED_TAGS = [
|
2021-02-02 18:09:19 +01:00
|
|
|
"h1", "h2", "h3", "h4", "h5", "h6", "hr",
|
|
|
|
"ul", "ol", "li",
|
|
|
|
"p",
|
|
|
|
"br",
|
|
|
|
"pre",
|
|
|
|
"code",
|
|
|
|
"blockquote",
|
|
|
|
"strong",
|
|
|
|
"em",
|
|
|
|
"a",
|
|
|
|
"img",
|
2021-02-02 19:16:55 +01:00
|
|
|
"table", "thead", "tbody", "tr", "th", "td",
|
2021-08-21 06:40:20 +02:00
|
|
|
"div", "span", "del", "s",
|
2021-02-02 19:16:55 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
ALLOWED_CSS = [
|
|
|
|
"highlight", "codehilite",
|
|
|
|
"hll", "c", "err", "g", "k", "l", "n", "o", "x", "p", "ch", "cm", "cp", "cpf", "c1", "cs",
|
|
|
|
"gd", "ge", "gr", "gh", "gi", "go", "gp", "gs", "gu", "gt", "kc", "kd", "kn", "kp", "kr",
|
|
|
|
"kt", "ld", "m", "s", "na", "nb", "nc", "no", "nd", "ni", "ne", "nf", "nl", "nn", "nx",
|
|
|
|
"py", "nt", "nv", "ow", "w", "mb", "mf", "mh", "mi", "mo", "sa", "sb", "sc", "dl", "sd",
|
|
|
|
"s2", "se", "sh", "si", "sx", "sr", "s1", "ss", "bp", "fm", "vc", "vg", "vi", "vm", "il",
|
2020-01-22 23:10:02 +01:00
|
|
|
]
|
|
|
|
|
2021-02-02 19:16:55 +01:00
|
|
|
def allow_class(_tag, name, value):
|
|
|
|
return name == "class" and value in ALLOWED_CSS
|
|
|
|
|
2020-01-22 23:10:02 +01:00
|
|
|
ALLOWED_ATTRIBUTES = {
|
2021-02-02 21:05:24 +01:00
|
|
|
"h1": ["id"],
|
|
|
|
"h2": ["id"],
|
|
|
|
"h3": ["id"],
|
|
|
|
"h4": ["id"],
|
2021-02-02 18:09:19 +01:00
|
|
|
"a": ["href", "title"],
|
2021-02-02 19:16:55 +01:00
|
|
|
"img": ["src", "title", "alt"],
|
|
|
|
"code": allow_class,
|
|
|
|
"div": allow_class,
|
|
|
|
"span": allow_class,
|
2020-01-22 23:10:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:09:19 +01:00
|
|
|
ALLOWED_PROTOCOLS = ["http", "https", "mailto"]
|
2020-01-22 23:10:02 +01:00
|
|
|
|
2021-02-02 18:09:19 +01:00
|
|
|
md = None
|
2020-01-22 23:10:02 +01:00
|
|
|
|
2021-02-02 21:05:24 +01:00
|
|
|
|
2020-01-22 23:10:02 +01:00
|
|
|
def render_markdown(source):
|
2021-02-02 19:16:55 +01:00
|
|
|
html = md.convert(source)
|
|
|
|
|
|
|
|
cleaner = Cleaner(
|
|
|
|
tags=ALLOWED_TAGS,
|
|
|
|
attributes=ALLOWED_ATTRIBUTES,
|
|
|
|
protocols=ALLOWED_PROTOCOLS,
|
|
|
|
filters=[partial(LinkifyFilter, callbacks=bleach.linkifier.DEFAULT_CALLBACKS)])
|
|
|
|
return cleaner.clean(html)
|
2020-01-22 23:10:02 +01:00
|
|
|
|
2021-02-02 21:05:24 +01:00
|
|
|
|
2021-08-21 06:40:20 +02:00
|
|
|
class DelInsExtension(Extension):
|
|
|
|
def extendMarkdown(self, md):
|
|
|
|
del_proc = SimpleTagInlineProcessor(r'(\~\~)(.+?)(\~\~)', 'del')
|
|
|
|
md.inlinePatterns.register(del_proc, 'del', 200)
|
|
|
|
|
|
|
|
ins_proc = SimpleTagInlineProcessor(r'(\+\+)(.+?)(\+\+)', 'ins')
|
|
|
|
md.inlinePatterns.register(ins_proc, 'ins', 200)
|
|
|
|
|
|
|
|
|
|
|
|
MARKDOWN_EXTENSIONS = ["fenced_code", "tables", "codehilite", "toc", DelInsExtension()]
|
|
|
|
MARKDOWN_EXTENSION_CONFIG = {
|
|
|
|
"fenced_code": {},
|
|
|
|
"tables": {},
|
|
|
|
"codehilite": {
|
|
|
|
"guess_lang": False,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def init_markdown(app):
|
2021-02-02 18:09:19 +01:00
|
|
|
global md
|
|
|
|
|
2021-08-21 06:40:20 +02:00
|
|
|
md = Markdown(extensions=MARKDOWN_EXTENSIONS,
|
|
|
|
extension_configs=MARKDOWN_EXTENSION_CONFIG,
|
2021-02-28 22:24:52 +01:00
|
|
|
output_format="html5")
|
2021-02-02 18:09:19 +01:00
|
|
|
|
2020-01-22 23:10:02 +01:00
|
|
|
@app.template_filter()
|
|
|
|
def markdown(source):
|
|
|
|
return Markup(render_markdown(source))
|
2021-02-02 21:05:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_headings(html: str):
|
|
|
|
soup = BeautifulSoup(html, "html.parser")
|
|
|
|
headings = soup.find_all(["h1", "h2", "h3"])
|
|
|
|
|
|
|
|
root = []
|
|
|
|
stack = []
|
|
|
|
for heading in headings:
|
|
|
|
this = { "link": heading.get("id") or "", "text": heading.text, "children": [] }
|
|
|
|
this_level = int(heading.name[1:]) - 1
|
|
|
|
|
|
|
|
while this_level <= len(stack):
|
|
|
|
stack.pop()
|
|
|
|
|
|
|
|
if len(stack) > 0:
|
|
|
|
stack[-1]["children"].append(this)
|
|
|
|
else:
|
|
|
|
root.append(this)
|
|
|
|
|
|
|
|
stack.append(this)
|
|
|
|
|
|
|
|
return root
|