mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-04-24 19:24:06 +00:00
fix link formatting issue, for real this time
This commit is contained in:
parent
c6d08050e2
commit
4f321e5f33
2 changed files with 24 additions and 19 deletions
|
@ -307,14 +307,14 @@ class StatusViews(TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
views.status.format_links(url), '<a href="%s">tech.lgbt/@bookwyrm</a>' % url
|
views.status.format_links(url), '<a href="%s">tech.lgbt/@bookwyrm</a>' % url
|
||||||
)
|
)
|
||||||
url = "users.speakeasy.net/~lion/nb/book.pdf"
|
url = "https://users.speakeasy.net/~lion/nb/book.pdf"
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
views.status.format_links(url),
|
views.status.format_links(url),
|
||||||
'<a href="%s">users.speakeasy.net/~lion/nb/book.pdf</a>' % url,
|
'<a href="%s">users.speakeasy.net/~lion/nb/book.pdf</a>' % url,
|
||||||
)
|
)
|
||||||
url = "pkm.one/#/page/The%20Book%20which%20launched%20a%201000%20Note%20taking%20apps"
|
url = "https://pkm.one/#/page/The%20Book%20which%20launched%20a%201000%20Note%20taking%20apps"
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
views.status.format_links(url), '<a href="%s">%s</a>' % (url, url)
|
views.status.format_links(url), '<a href="%s">%s</a>' % (url, url[8:])
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_to_markdown(self, *_):
|
def test_to_markdown(self, *_):
|
||||||
|
|
|
@ -153,32 +153,37 @@ def find_mentions(content):
|
||||||
|
|
||||||
def format_links(content):
|
def format_links(content):
|
||||||
"""detect and format links"""
|
"""detect and format links"""
|
||||||
|
validator = URLValidator()
|
||||||
formatted_content = ""
|
formatted_content = ""
|
||||||
for potential_link in content.split():
|
split_content = content.split()
|
||||||
try:
|
|
||||||
# raises an error on anything that's not a valid
|
for index, potential_link in enumerate(split_content):
|
||||||
URLValidator(potential_link)
|
|
||||||
except (ValidationError, UnicodeError):
|
|
||||||
formatted_content += potential_link + " "
|
|
||||||
continue
|
|
||||||
wrapped = _wrapped(potential_link)
|
wrapped = _wrapped(potential_link)
|
||||||
if wrapped:
|
if wrapped:
|
||||||
wrapper_close = potential_link[-1]
|
wrapper_close = potential_link[-1]
|
||||||
formatted_content += potential_link[0]
|
formatted_content += potential_link[0]
|
||||||
potential_link = potential_link[1:-1]
|
potential_link = potential_link[1:-1]
|
||||||
|
|
||||||
# so we can use everything but the scheme in the presentation of the link
|
try:
|
||||||
url = urlparse(potential_link)
|
# raises an error on anything that's not a valid link
|
||||||
link = url.netloc + url.path + url.params
|
validator(potential_link)
|
||||||
if url.query != "":
|
|
||||||
link += "?" + url.query
|
|
||||||
if url.fragment != "":
|
|
||||||
link += "#" + url.fragment
|
|
||||||
|
|
||||||
formatted_content += '<a href="%s">%s</a>' % (potential_link, link)
|
# use everything but the scheme in the presentation of the link
|
||||||
|
url = urlparse(potential_link)
|
||||||
|
link = url.netloc + url.path + url.params
|
||||||
|
if url.query != "":
|
||||||
|
link += "?" + url.query
|
||||||
|
if url.fragment != "":
|
||||||
|
link += "#" + url.fragment
|
||||||
|
|
||||||
|
formatted_content += '<a href="%s">%s</a>' % (potential_link, link)
|
||||||
|
except (ValidationError, UnicodeError):
|
||||||
|
formatted_content += potential_link
|
||||||
|
|
||||||
if wrapped:
|
if wrapped:
|
||||||
formatted_content += wrapper_close
|
formatted_content += wrapper_close
|
||||||
|
if index < len(split_content) - 1:
|
||||||
|
formatted_content += " "
|
||||||
|
|
||||||
return formatted_content
|
return formatted_content
|
||||||
|
|
||||||
|
@ -194,8 +199,8 @@ def _wrapped(text):
|
||||||
|
|
||||||
def to_markdown(content):
|
def to_markdown(content):
|
||||||
"""catch links and convert to markdown"""
|
"""catch links and convert to markdown"""
|
||||||
content = markdown(content)
|
|
||||||
content = format_links(content)
|
content = format_links(content)
|
||||||
|
content = markdown(content)
|
||||||
# sanitize resulting html
|
# sanitize resulting html
|
||||||
sanitizer = InputHtmlParser()
|
sanitizer = InputHtmlParser()
|
||||||
sanitizer.feed(content)
|
sanitizer.feed(content)
|
||||||
|
|
Loading…
Reference in a new issue