Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Need to linkify during Markdown rendering #5066

Merged
merged 9 commits into from
Feb 11, 2023
37 changes: 31 additions & 6 deletions ietf/utils/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,46 @@
the datatracker.
"""
import markdown as python_markdown
from markdown.extensions import Extension
from markdown.postprocessors import Postprocessor

from django.utils.safestring import mark_safe

from ietf.doc.templatetags.ietf_filters import urlize_ietf_docs
from ietf.utils.text import bleach_cleaner, bleach_linker


class LinkifyExtension(Extension):
"""
Simple Markdown extension inspired by https://github.com/daGrevis/mdx_linkify,
but using our bleach_linker directly.
"""

def extendMarkdown(self, md):
md.postprocessors.register(LinkifyPostprocessor(md), "linkify", 50)


class LinkifyPostprocessor(Postprocessor):
def run(self, text):
return bleach_linker.linkify(text)


def markdown(text):
return mark_safe(
bleach_linker.linkify(
urlize_ietf_docs(
bleach_cleaner.clean(
python_markdown.markdown(
text, extensions=["extra", "nl2br", "sane_lists", "toc"]
)
urlize_ietf_docs(
bleach_cleaner.clean(
python_markdown.markdown(
text,
extensions=[
# TODO: discuss which extensions we want to enable, see
larseggert marked this conversation as resolved.
Show resolved Hide resolved
# https://python-markdown.github.io/extensions/ and
# https://github.com/Python-Markdown/markdown/wiki/Third-Party-Extensions
"extra",
"nl2br",
"sane_lists",
"toc",
LinkifyExtension(),
],
)
)
)
Expand Down