Allow to replace URL dynamically (for Nitter, Teddit...)
This commit is contained in:
parent
4de4178c3d
commit
8cfac8df6a
3 changed files with 48 additions and 2 deletions
|
@ -2,6 +2,7 @@ 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
|
||||
|
@ -11,6 +12,7 @@ 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.datetime import now
|
||||
from app.utils.datetime import parse_isoformat
|
||||
|
@ -175,9 +177,22 @@ class Object:
|
|||
|
||||
# PeerTube returns the content as markdown
|
||||
if self.ap_object.get("mediaType") == "text/markdown":
|
||||
return markdown(content, extensions=["mdx_linkify"])
|
||||
content = markdown(content, extensions=["mdx_linkify"])
|
||||
|
||||
return content
|
||||
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()
|
||||
|
||||
@property
|
||||
def summary(self) -> str | None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue