Skip to content

Commit

Permalink
Keep mention attributes when filtering annotation markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Feb 20, 2025
1 parent 05dd62c commit ba4d1e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 1 addition & 2 deletions h/services/mention.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from h.models import Annotation, Mention
from h.services.html import parse_html_links
from h.services.user import UserService
from h.util.markdown_render import MENTION_ATTRIBUTE, MENTION_USERID

MENTION_ATTRIBUTE = "data-hyp-mention"
MENTION_USERID = "data-userid"
MENTION_LIMIT = 5

logger = logging.getLogger(__name__)
Expand Down
6 changes: 6 additions & 0 deletions h/util/markdown_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from markdown import Markdown

LINK_REL = "nofollow noopener"
MENTION_ATTRIBUTE = "data-hyp-mention"
MENTION_USERID = "data-userid"

MARKDOWN_TAGS = [
"a",
Expand Down Expand Up @@ -55,6 +57,10 @@ def _filter_link_attributes(_tag, name, value):
if name in ["href", "title"]:
return True

# Keep attributes used in mention tags
if name in [MENTION_ATTRIBUTE, MENTION_USERID]:
return True

if name == "target" and value == "_blank":
return True

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/h/util/markdown_render_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ def test_it_ignores_inline_math(self):
actual = markdown_render.render(r"Foobar \(1 + 1 = 2\)")
assert actual == "<p>Foobar \\(1 + 1 = 2\\)</p>"

def test_it_preserves_mention_attributes(self):
actual = markdown_render.render(
'<a data-hyp-mention="" data-userid="acct:username@example.com">@username</a>'
)
assert (
actual
== '<p><a data-hyp-mention="" data-userid="acct:username@example.com">@username</a></p>'
)

@pytest.mark.parametrize(
"text",
[
'<p><a href="mailto:foo@example.net">example</a></p>', # Don't add rel and target attrs to mailto: links
'<p><a data-hyp-mention="" data-userid="acct:username@example.com">@username</a></p>',
'<p><a title="foobar">example</a></p>',
'<p><a href="https://example.org" rel="nofollow noopener" target="_blank" title="foobar">example</a></p>',
"<blockquote>Foobar</blockquote>",
Expand Down

0 comments on commit ba4d1e2

Please sign in to comment.