diff --git a/core/html.py b/core/html.py index 55adfd2..c760117 100644 --- a/core/html.py +++ b/core/html.py @@ -98,6 +98,7 @@ class FediverseHtmlParser(HTMLParser): domain = mention.domain_id.lower() self.mention_matches[f"{username}"] = url self.mention_matches[f"{username}@{domain}"] = url + self.mention_matches[mention.absolute_profile_uri()] = url def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: if tag in self.REWRITE_TO_P: @@ -126,7 +127,7 @@ class FediverseHtmlParser(HTMLParser): has_ellipsis = "ellipsis" in self._pending_a["attrs"].get("class", "") # Is it a mention? if content.lower().lstrip("@") in self.mention_matches: - self.html_output += self.create_mention(content) + self.html_output += self.create_mention(content, href) self.text_output += content # Is it a hashtag? elif self.HASHTAG_REGEX.match(content): @@ -172,7 +173,7 @@ class FediverseHtmlParser(HTMLParser): else: return f'{html.escape(content)}' - def create_mention(self, handle) -> str: + def create_mention(self, handle, href: str | None = None) -> str: """ Generates a mention link. Handle should have a leading @. @@ -187,6 +188,9 @@ class FediverseHtmlParser(HTMLParser): short_hash = short_handle.lower() self.mentions.add(handle_hash) url = self.mention_matches.get(handle_hash) + # If we have a captured link out, use that as the actual resolver + if href and href in self.mention_matches: + url = self.mention_matches[href] if url: if short_hash not in self.mention_aliases: self.mention_aliases[short_hash] = handle_hash diff --git a/tests/core/test_html.py b/tests/core/test_html.py index 9df6aa3..9776539 100644 --- a/tests/core/test_html.py +++ b/tests/core/test_html.py @@ -115,3 +115,22 @@ def test_parser(identity): ) assert parser.html == "

List:

One
Two
Three

End!

" assert parser.plain_text == "List:\n\nOne\nTwo\nThree\n\nEnd!" + + +@pytest.mark.django_db +def test_parser_same_name_mentions(remote_identity, remote_identity2): + """ + Ensure mentions that differ only by link are parsed right + """ + + parser = FediverseHtmlParser( + '@test @test', + mentions=[remote_identity, remote_identity2], + find_hashtags=True, + find_emojis=True, + ) + assert ( + parser.html + == '@test @test' + ) + assert parser.plain_text == "@test @test"