An article with lots of images embedded like this ![]() currently puts all the images in their full resolution in the article, wasting a lot of bandwidth.
How do I change my theme so that ![]() renders a downscaled version of the image with a link to its full resolution?
The only thing that I can think of that will do what you want is one that I do not recommend doing.
Basically, do a massive find and replace on the content of the page, doing something like the following:
Use regex to find every image in .Content
Parse the image tag to find the source of the image
If the image exists in the assets/ folder or the page bundle, continue
Create a Hugo asset out of the image, for processing
Use Hugo’s built-in image processing to shrink the image
Replace the original image tag with a new one using the smaller image, wrapped in a link to the original file
It’s a really hackish way to do it and is more likely to cause issues. The more sustainable option is to make a shortcode like @zwbetz suggested. But what you want to do is technically possible.
This works really well, the images can even be included withithout paths and it works on the front page. Awesome.
However, I’m still interested if I can get Hugo to read Markdown style images ![](). I tried replaceRE within the theme, but the content is already in HTML. Is there a way to run replaceRE before the markdown is rendered to HTML?