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
43 changes: 22 additions & 21 deletions ietf/utils/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@
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,
check_url_validity,
bleach,
tlds_sorted,
protocols,
)
from mdx_linkify.mdx_linkify import LinkifyExtension # type: ignore
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):
Expand All @@ -27,23 +37,14 @@ def markdown(text):
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(
# keep these in sync with the bleach_linker initialization
linker_options={
"callbacks": [check_url_validity],
"url_re": bleach.linkifier.build_url_re(
tlds=tlds_sorted, protocols=protocols
),
"email_re": bleach.linkifier.build_email_re(
tlds=tlds_sorted
),
"parse_email": True,
}
),
LinkifyExtension(),
],
)
)
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ jwcrypto>=1.2 # for signed notifications - this is aspirational, and is not r
logging_tree>=1.9 # Used only by the showloggers management command
lxml>=4.8.0,<5
markdown>=3.3.6
mdx_linkify>=2.1
mock>=4.0.3 # Used only by tests, of course
mypy>=0.782,<0.790 # Version requirements determined by django-stubs.
mysqlclient>=2.1.0
Expand Down