Skip to content

Commit

Permalink
Merge pull request #179 from canonical/handle-images
Browse files Browse the repository at this point in the history
Handle images in links
  • Loading branch information
Lukewh authored Nov 21, 2023
2 parents 66e2d74 + e2f3d5a commit 96c8c01
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
28 changes: 27 additions & 1 deletion canonicalwebteam/discourse/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ def _process_topic_soup(self, soup):

soup = self._replace_notifications(soup)
soup = self._replace_notes_to_editors(soup)
soup = self._replace_image_src(soup)
soup = self._replace_links(soup)
soup = self._replace_polls(soup)

Expand All @@ -556,8 +557,20 @@ def _replace_links(self, soup, topics=[]):
full_link = a.get("href", "")
self._replace_text_link(a, topics)

# For images, link to the uploaded file
if a.find("img") and full_link.startswith("/uploads/"):
a["href"] = os.path.join(
self.api.base_url, full_link.lstrip("/")
)
continue

# For user references link to discourse profile pages
if full_link.startswith("/u") and a.string.startswith("@"):
if (
full_link
and full_link.startswith("/u/")
and a.string
and a.string.startswith("@")
):
a["href"] = os.path.join(
self.api.base_url, full_link.lstrip("/")
)
Expand Down Expand Up @@ -592,6 +605,19 @@ def _replace_links(self, soup, topics=[]):

return soup

def _replace_image_src(self, soup):
"""
Given some HTML soup, replace relative image srcs
"""
for img in soup.findAll("img"):
src = img.get("src", "")
if src and src.startswith("/"):
img["src"] = f"{self.api.base_url}{src}"
if img["srcset"]:
del img["srcset"]

return soup

@cached_property
def _notification_template(self):
notification_html = (
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="canonicalwebteam.discourse",
version="5.4.5",
version="5.4.6",
author="Canonical webteam",
author_email="[email protected]",
url="https://github.com/canonical/canonicalwebteam.discourse",
Expand Down
42 changes: 42 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,48 @@ def test_parser_username_link(self):
parsed_topic["body_html"],
)

def test_parser_upload(self):
discourse_api = DiscourseAPI("https://base.url", session=MagicMock())

parser = BaseParser(
api=discourse_api,
index_topic_id=1,
url_prefix="/",
)

parsed_topic = parser.parse_topic(
{
"id": 1,
"category_id": 1,
"title": "Sample",
"slug": "sample—text",
"post_stream": {
"posts": [
{
"id": 11,
"cooked": (
"<a href='/uploads/test.png'>"
"<img src='test.png' srcset='test.png' />"
"</a>"
"<a>No Link</a>"
"<a></a>"
),
"updated_at": "2018-10-02T12:45:44.259Z",
}
],
},
}
)

self.assertIn(
(
'<a href="https://base.url/uploads/test.png">'
'<img src="test.png"/>'
"</a>"
),
parsed_topic["body_html"],
)

def test_emdash_in_slug(self):
discourse_api = DiscourseAPI("https://base.url", session=MagicMock())

Expand Down

0 comments on commit 96c8c01

Please sign in to comment.