Skip to content

Commit

Permalink
Fix hashtagging of HTML entities
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Nov 29, 2022
1 parent 4420507 commit fb342cf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 3 additions & 3 deletions activities/models/hashtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class urls(urlman.Urls):
delete = "{edit}delete/"
timeline = "/tags/{self.hashtag}/"

hashtag_regex = re.compile(r"((?:\B#)([a-zA-Z0-9(_)]{1,}\b))")
hashtag_regex = re.compile(r"\B#([a-zA-Z0-9(_)]+\b)(?!;)")

def save(self, *args, **kwargs):
self.hashtag = self.hashtag.lstrip("#")
Expand Down Expand Up @@ -182,7 +182,7 @@ def hashtags_from_content(cls, content) -> List[str]:
@classmethod
def linkify_hashtags(cls, content) -> str:
def replacer(match):
hashtag = match.group()
return f'<a class="hashtag" href="/tags/{hashtag.lstrip("#").lower()}/">{hashtag}</a>'
hashtag = match.group(1)
return f'<a class="hashtag" href="/tags/{hashtag.lower()}/">#{hashtag}</a>'

return mark_safe(Hashtag.hashtag_regex.sub(replacer, content))
8 changes: 7 additions & 1 deletion tests/core/test_html.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from core.html import html_to_plaintext
from core.html import html_to_plaintext, sanitize_post


def test_html_to_plaintext():
Expand All @@ -13,3 +13,9 @@ def test_html_to_plaintext():
html_to_plaintext("<p>Hi!</p>\n\n<p>How are<br> you?</p><p>today</p>")
== "Hi!\n\nHow are\n you?\n\ntoday"
)


def test_sanitize_post():

assert sanitize_post("<p>Hello!</p>") == "<p>Hello!</p>"
assert sanitize_post("<p>It&#39;s great</p>") == "<p>It&#39;s great</p>"

0 comments on commit fb342cf

Please sign in to comment.