2021-11-25 13:35:56 +01:00
|
|
|
import re
|
|
|
|
|
2021-08-22 20:10:29 +02:00
|
|
|
import bleach
|
|
|
|
import markdown
|
|
|
|
from bleach_allowlist import markdown_tags, markdown_attrs
|
|
|
|
|
2021-12-09 15:07:22 +01:00
|
|
|
custom_allowed_tags = ["del", "ins"]
|
|
|
|
|
2021-08-22 20:10:29 +02:00
|
|
|
|
|
|
|
def md_to_html(md: str) -> str:
|
2021-09-26 19:02:25 +02:00
|
|
|
md = autolink(md)
|
2021-08-22 20:10:29 +02:00
|
|
|
html = markdown.markdown(
|
|
|
|
md,
|
|
|
|
output_format="html",
|
|
|
|
extensions=[
|
|
|
|
"nl2br",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
html = bleach.clean(
|
|
|
|
html,
|
2021-12-09 15:07:22 +01:00
|
|
|
tags=markdown_tags + custom_allowed_tags,
|
2021-08-22 20:10:29 +02:00
|
|
|
attributes=markdown_attrs
|
|
|
|
)
|
|
|
|
return html
|
2021-09-26 19:02:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def autolink(md: str) -> str:
|
|
|
|
from utils.urls import name2url
|
2022-04-10 17:15:03 +02:00
|
|
|
links = {}
|
|
|
|
i = 0
|
2021-09-26 19:02:25 +02:00
|
|
|
for name, url in name2url().items():
|
2021-11-25 13:35:56 +01:00
|
|
|
regex = r"\bWORD\b".replace("WORD", name)
|
2022-04-10 17:15:03 +02:00
|
|
|
placeholder = f"SOME{i}LINK"
|
|
|
|
md = re.sub(regex, placeholder, md)
|
|
|
|
links[placeholder] = f"[{name}]({url})"
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
for placeholder, value in links.items():
|
|
|
|
md = md.replace(placeholder, value)
|
2021-09-26 19:02:25 +02:00
|
|
|
return md
|