Render hashtag links with data-mention="hashtag" attribute

This commit is contained in:
Christof Dorner 2023-02-18 00:18:32 +01:00
parent 276b255f32
commit f3334b1550
5 changed files with 13 additions and 7 deletions

View file

@ -62,7 +62,7 @@ class Note(ActivityObject):
changed_content = False
for hashtag in instance.mention_hashtags.all():
updated_content = re.sub(
rf'(<a href=")[^"]*/hashtag/[^"]*(">{hashtag.name}</a>)',
rf'(<a href=")[^"]*(" data-mention="hashtag">{hashtag.name}</a>)',
rf"\1{hashtag.remote_id}\2",
instance.content,
)

View file

@ -33,7 +33,8 @@ class Note(TestCase):
attributedTo=self.user.remote_id,
inReplyToBook=self.book.remote_id,
content="<p>This is interesting "
+ '<a href="https://test-instance.org/hashtag/2">#BookClub</a></p>',
+ '<a href="https://test-instance.org/hashtag/2" data-mention="hashtag">'
+ "#BookClub</a></p>",
published="2023-02-17T23:12:59.398030+00:00",
to=[],
cc=[],
@ -57,5 +58,7 @@ class Note(TestCase):
self.assertIsNotNone(hashtag)
self.assertEqual(
instance.content,
f'<p>This is interesting <a href="{hashtag.remote_id}">#BookClub</a></p>',
"<p>This is interesting "
+ f'<a href="{hashtag.remote_id}" data-mention="hashtag">'
+ "#BookClub</a></p>",
)

View file

@ -359,8 +359,10 @@ class StatusViews(TestCase):
hashtag_new = models.Hashtag.objects.filter(name="#new").first()
self.assertEqual(
status.content,
f'<p>this is an <a href="{hashtag_exising.remote_id}">#existing</a> '
+ f'hashtag, this one is <a href="{hashtag_new.remote_id}">#new</a>.</p>',
"<p>this is an "
+ f'<a href="{hashtag_exising.remote_id}" data-mention="hashtag">'
+ "#existing</a> hashtag, this one is "
+ f'<a href="{hashtag_new.remote_id}" data-mention="hashtag">#new</a>.</p>',
)
def test_find_hashtags(self, *_):

View file

@ -21,6 +21,6 @@ def clean(input_text):
"ol",
"li",
],
attributes=["href", "rel", "src", "alt"],
attributes=["href", "rel", "src", "alt", "data-mention"],
strip=True,
)

View file

@ -123,7 +123,8 @@ class CreateStatus(View):
# turn the mention into a link
content = re.sub(
rf"{mention_text}\b(?!@)",
rf'<a href="{mention_hashtag.remote_id}">{mention_text}</a>',
rf'<a href="{mention_hashtag.remote_id}" data-mention="hashtag">'
+ rf"{mention_text}</a>",
content,
)