public
Created

  • Download Gist
substitute-example.js
JavaScript
1 2 3 4 5 6 7 8 9 10
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);
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.