diff --git a/app/ap_object.py b/app/ap_object.py index 72fd871..f96b5c6 100644 --- a/app/ap_object.py +++ b/app/ap_object.py @@ -2,7 +2,6 @@ import hashlib from datetime import datetime from functools import cached_property from typing import Any -from urllib.parse import urlparse import pydantic from bs4 import BeautifulSoup # type: ignore @@ -12,8 +11,8 @@ from app import activitypub as ap from app.actor import LOCAL_ACTOR from app.actor import Actor from app.actor import RemoteActor -from app.config import PRIVACY_REPLACE from app.media import proxied_media_url +from app.utils import privacy_replace from app.utils.datetime import now from app.utils.datetime import parse_isoformat @@ -179,20 +178,7 @@ class Object: if self.ap_object.get("mediaType") == "text/markdown": content = markdown(content, extensions=["mdx_linkify"]) - if not PRIVACY_REPLACE: - return content - - soup = BeautifulSoup(content, "html5lib") - links = soup.find_all("a", href=True) - - for link in links: - parsed_href = urlparse(link.attrs["href"]) - if new_netloc := PRIVACY_REPLACE.get( - parsed_href.netloc.removeprefix("www.") - ): - link.attrs["href"] = parsed_href._replace(netloc=new_netloc).geturl() - - return soup.find("body").decode_contents() + return privacy_replace.replace_content(content) @property def summary(self) -> str | None: diff --git a/app/templates.py b/app/templates.py index 2c51bbe..30af066 100644 --- a/app/templates.py +++ b/app/templates.py @@ -32,6 +32,7 @@ from app.config import generate_csrf_token from app.config import session_serializer from app.database import AsyncSession from app.media import proxied_media_url +from app.utils import privacy_replace from app.utils.datetime import now from app.utils.highlight import HIGHLIGHT_CSS from app.utils.highlight import highlight @@ -400,3 +401,4 @@ _templates.env.filters["emojify"] = _emojify _templates.env.filters["pluralize"] = _pluralize _templates.env.filters["parse_datetime"] = _parse_datetime _templates.env.filters["poll_item_pct"] = _poll_item_pct +_templates.env.filters["privacy_replace_url"] = privacy_replace.replace_url diff --git a/app/templates/utils.html b/app/templates/utils.html index bfc6331..a2040a5 100644 --- a/app/templates/utils.html +++ b/app/templates/utils.html @@ -317,7 +317,7 @@ {% endif %}