Replace a part of string upon site build/deployment

I use CDN for my images. I have a custom CNAME so that in the image URL is cdn.mywebsite.com/path/to/my/image/.

However, my CDN provider does not allow me to copy the path with my custom CNAME by default, so when I copy the URL of the image manually, the link will look like this:

server-a.mycdn.com/path/to/my/image/

Obviously, I don’t want to have image source URLs pointing to my CDN provider, so I only want to rely on my custom CNAME URLs. And changing manually this URL to my custom CNAME is time-consuming.

The easiest solution that comes up to my mind would be to simply find all the references of server-a.mycdn.com across the site and replace it with cdn.mywebsite.com upon the build of the site.

How can I achieve it?

I guess this could be achieved with the Replace or ReplaceRE function but I only know how to do this locally on a single string - I don’t know how to make it work across the entire website.

OK, I achieved it at least for the actual blog pages. This is my solution:

beginning of my blog/single.html file:

{{ $find := "server-b.mycdn.com" }}
{{ $replacewith := "cdn.mywebsite.com" }}

and then in the line where I call .Content, I have:

{{ replace .Content $source $target | safeHTML }}

It works but I am not sure this is the prettiest solution. Also, this will work only within the content directory. Any other suggestions how to make it better and less hacky?

As with your other question, I hope my post Work in progress markdown responsive images (including figure shortcode) can help you, with a render-image render-hook.