Skip to content

Instantly share code, notes, and snippets.

@lepture
Created July 9, 2013 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepture/5953897 to your computer and use it in GitHub Desktop.
Save lepture/5953897 to your computer and use it in GitHub Desktop.
var md5 = require('md5');
const SUBSTITUTE_BASE = 'https://example.com/';
const SUBSTITUTE_SECRET = 'a secret';
html = html.replace(/<img[^>]*src=('|")(.*?)\1[^>]*>/g, function(img) {
var src = RegExp.$2;
var digest = md5.hmac(SUBSTITUTE_SECRET, src);
var newSrc = SUBSTITUTE_BASE + digest + '/' + encodeURIComponent(src);
return img.replace(src, newSrc);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment