mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-28 18:21:00 +00:00
Prepend invisible URL protocol prefix (#586)
This commit is contained in:
parent
2040124147
commit
f88ad38294
3 changed files with 10 additions and 6 deletions
|
@ -167,9 +167,13 @@ class FediverseHtmlParser(HTMLParser):
|
||||||
"""
|
"""
|
||||||
looks_like_link = bool(self.URL_REGEX.match(content))
|
looks_like_link = bool(self.URL_REGEX.match(content))
|
||||||
if looks_like_link:
|
if looks_like_link:
|
||||||
content = content.split("://", 1)[1]
|
protocol, content = content.split("://", 1)
|
||||||
|
else:
|
||||||
|
protocol = ""
|
||||||
if (looks_like_link and len(content) > 30) or has_ellipsis:
|
if (looks_like_link and len(content) > 30) or has_ellipsis:
|
||||||
return f'<a href="{html.escape(href)}" rel="nofollow" class="ellipsis" title="{html.escape(content)}"><span class="ellipsis">{html.escape(content[:30])}</span><span class="invisible">{html.escape(content[30:])}</span></a>'
|
return f'<a href="{html.escape(href)}" rel="nofollow" class="ellipsis" title="{html.escape(content)}"><span class="invisible">{html.escape(protocol)}://</span><span class="ellipsis">{html.escape(content[:30])}</span><span class="invisible">{html.escape(content[30:])}</span></a>'
|
||||||
|
elif looks_like_link:
|
||||||
|
return f'<a href="{html.escape(href)}" rel="nofollow"><span class="invisible">{html.escape(protocol)}://</span>{html.escape(content)}</a>'
|
||||||
else:
|
else:
|
||||||
return f'<a href="{html.escape(href)}" rel="nofollow">{html.escape(content)}</a>'
|
return f'<a href="{html.escape(href)}" rel="nofollow">{html.escape(content)}</a>'
|
||||||
|
|
||||||
|
|
|
@ -170,8 +170,7 @@ def test_content_link(api_client, identity, remote_identity):
|
||||||
},
|
},
|
||||||
).json()
|
).json()
|
||||||
|
|
||||||
# temp fix
|
|
||||||
assert (
|
assert (
|
||||||
response["content"]
|
response["content"]
|
||||||
== '<p>Takahē - return to the wild - <a href="https://www.youtube.com/watch?v=IG423K3pmQI" rel="nofollow" class="ellipsis" title="www.youtube.com/watch?v=IG423K3pmQI"><span class="ellipsis">www.youtube.com/watch?v=IG423K</span><span class="invisible">3pmQI</span></a></p>'
|
== '<p>Takahē - return to the wild - <a href="https://www.youtube.com/watch?v=IG423K3pmQI" rel="nofollow" class="ellipsis" title="www.youtube.com/watch?v=IG423K3pmQI"><span class="invisible">https://</span><span class="ellipsis">www.youtube.com/watch?v=IG423K</span><span class="invisible">3pmQI</span></a></p>'
|
||||||
)
|
)
|
||||||
|
|
|
@ -37,7 +37,8 @@ def test_parser(identity):
|
||||||
assert parser.plain_text == "test.com"
|
assert parser.plain_text == "test.com"
|
||||||
parser = FediverseHtmlParser("<p>https://test.com</p>")
|
parser = FediverseHtmlParser("<p>https://test.com</p>")
|
||||||
assert (
|
assert (
|
||||||
parser.html == '<p><a href="https://test.com" rel="nofollow">test.com</a></p>'
|
parser.html
|
||||||
|
== '<p><a href="https://test.com" rel="nofollow"><span class="invisible">https://</span>test.com</a></p>'
|
||||||
)
|
)
|
||||||
assert parser.plain_text == "https://test.com"
|
assert parser.plain_text == "https://test.com"
|
||||||
|
|
||||||
|
@ -54,7 +55,7 @@ def test_parser(identity):
|
||||||
parser = FediverseHtmlParser(f"<p>{full_url}</p>")
|
parser = FediverseHtmlParser(f"<p>{full_url}</p>")
|
||||||
assert (
|
assert (
|
||||||
parser.html
|
parser.html
|
||||||
== f'<p><a href="{full_url}" rel="nofollow" class="ellipsis" title="{full_url.removeprefix("https://")}"><span class="ellipsis">social.example.com/a-long/path</span><span class="invisible">/that-should-be-shortened</span></a></p>'
|
== f'<p><a href="{full_url}" rel="nofollow" class="ellipsis" title="{full_url.removeprefix("https://")}"><span class="invisible">https://</span><span class="ellipsis">social.example.com/a-long/path</span><span class="invisible">/that-should-be-shortened</span></a></p>'
|
||||||
)
|
)
|
||||||
assert (
|
assert (
|
||||||
parser.plain_text
|
parser.plain_text
|
||||||
|
|
Loading…
Reference in a new issue