simplify regex matching and increase readability of the giphy plugin and have plugin link to original userfacing url, not cdn image file

This commit is contained in:
Colin Arnott 2015-09-26 22:40:30 +00:00
parent 03a6e391f6
commit 875001f1b2

View file

@ -358,17 +358,14 @@ plugins.factory('userPlugins', function() {
* sample output: https://media.giphy.com/media/feqkVgjJpYtjy/giphy.gif * sample output: https://media.giphy.com/media/feqkVgjJpYtjy/giphy.gif
*/ */
var giphyPlugin = new UrlPlugin('Giphy', function(url) { var giphyPlugin = new UrlPlugin('Giphy', function(url) {
var regex = /^https?:\/\/giphy.com\/gifs\/.*-.*\/?/i; var regex = /^https?:\/\/giphy.com\/gifs\/.*-(.*)\/?/i;
if (url.match(regex)) { // on match, id will contain the entire url in [0] and the giphy id in [1]
/* if the url does not contain a trailing slash, add it */ var id = url.match(regex);
if (! url.match(/\/$/i)) { url = url + "/"; } if (id) {
/* upgrade any http links to tls, yeah crypto */ var src = "https://media.giphy.com/media/" + id[1] + "/giphy.gif";
if (url.match(/^http:/i)) { url.replace(/http:/, "https:"); } /* upgrade any http url to tls, yeah crypto */
/* change the url to refrence the giphy cdn url, not the userfacing webserver */ if (url.match(/^http:/i)) { url.replace(/^http:/, "https:"); }
url = url.replace(/\/giphy.com\//i,"/media.giphy.com/"); return '<a target="_blank" href="'+url+'"><img class="embed" src="' + src + '"></a>';
url = url.replace(/\/gifs\/.*-/i,"/media/");
url = url + "giphy.gif";
return '<a target="_blank" href="'+url+'"><img class="embed" src="' + url + '"></a>';
} }
}); });