Disable linkify on text without https:// or http://

Fixes #374
This commit is contained in:
rubenwardy 2023-05-19 20:13:10 +01:00
parent 05bfa11d71
commit ff23f981e2

@ -64,6 +64,14 @@ ALLOWED_PROTOCOLS = {"http", "https", "mailto"}
md = None md = None
def linker_callback(attrs, new=False):
if new:
text = attrs.get("_text")
if not (text.startswith("http://") or text.startswith("https://")):
return None
return attrs
def render_markdown(source): def render_markdown(source):
html = md.convert(source) html = md.convert(source)
@ -71,7 +79,7 @@ def render_markdown(source):
tags=ALLOWED_TAGS, tags=ALLOWED_TAGS,
attributes=ALLOWED_ATTRIBUTES, attributes=ALLOWED_ATTRIBUTES,
protocols=ALLOWED_PROTOCOLS, protocols=ALLOWED_PROTOCOLS,
filters=[partial(LinkifyFilter, callbacks=bleach.linkifier.DEFAULT_CALLBACKS)]) filters=[partial(LinkifyFilter, callbacks=[linker_callback] + bleach.linkifier.DEFAULT_CALLBACKS, skip_tags={"pre"})])
return cleaner.clean(html) return cleaner.clean(html)