Skip to content

Commit

Permalink
Strip query string from image URLs
Browse files Browse the repository at this point in the history
The URL of WordPress-hosted images are often adorned with query string
parameters like `?w=4096`, to set the size of the image that should be
retrieved.

In the modern era, it's far better to use srcset and to allow the client
to have some control over the most appropriate size to retrieve (based
on, amongst other things, the size of the viewport). Astro's `<Image/>`
tag provides us with some assistance in this department.

We can also benefit from those same smarts when loading local images out
of the `./src` directory, in Markdown files.

So, we don't need the query strings, and while they won't actually do
anything on a site served from Astro, let's keep things tidy and remove
them.
  • Loading branch information
gma committed Nov 10, 2023
1 parent 515fe48 commit b3d1274
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions tests/test_astro.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,14 @@ def test_replaces_wordpress_url_with_relative_path(self) -> None:
text = astro.HostedImageFilter(urls)(content)

assert text == f'<img class="wp-image-{image_id}" src="./{basename}">'

def test_strips_image_size_parameters_from_query_string(self) -> None:
image_id = '1234'
basename = 'image.jpg'
url = f'https://sitename.files.wordpress.com/{basename}'
urls = {image_id: url}
content = f'<img class="wp-image-{image_id}" src="{url}?w=1024">'

text = astro.HostedImageFilter(urls)(content)

assert text == f'<img class="wp-image-{image_id}" src="./{basename}">'
3 changes: 2 additions & 1 deletion wpsite/astro.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
import urllib.parse
import urllib.request

Expand Down Expand Up @@ -84,4 +85,4 @@ def replace_url(self, attachment_id: str) -> None:
logging.warning(f'Attachment missing from export: {attachment_id}')
else:
basename = PurePath(urllib.parse.urlparse(url).path).name
self.text = self.text.replace(url, f'./{basename}')
self.text = re.sub(rf'{url}(\?[^"]+)?', f'./{basename}', self.text)

0 comments on commit b3d1274

Please sign in to comment.