Created
January 19, 2018 02:02
-
-
Save oscarr-reyes/a84f1402e78ed1380d028bf7316358a2 to your computer and use it in GitHub Desktop.
Link preview service for Angular.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.factory("$Link", function($q, $http){ | |
var LINKREGEX = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+ = &%@!\-\/]))?/g; | |
var YOUTUBEREGEX = /(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'<> #]+)/; | |
var IMAGEREGEX = /(.+\w+\.(png|jpg|jpeg|ico|svg)$)/; | |
var SERVICE = "https://api.linkpreview.net"; | |
var KEY = "<api-key>"; | |
var $Link = {}; | |
/** | |
* Verifies if the provided text contains a link | |
* | |
* @param {String} text The text to check for links | |
* @return {Boolean} Whether the text has links | |
*/ | |
$Link.isLink = function(text){ | |
var result = LINKREGEX.test(text); | |
LINKREGEX.lastIndex = 0; | |
return result; | |
}; | |
/** | |
* Verifies if the provided text contains a youtube link | |
* | |
* @param {String} text The text to check for youtube links | |
* @return {Boolean} Whether the text has youtube links | |
*/ | |
$Link.isYoutube = function(text){ | |
if(!this.isLink(text)){ | |
return false; | |
} | |
else{ | |
return YOUTUBEREGEX.test(text); | |
} | |
}; | |
/** | |
* Verifies if the provided text contains an image link | |
* | |
* @param {String} text The text to check for an image link | |
* @return {Boolean} Whether the text has image links | |
*/ | |
$Link.isImage = function(text){ | |
if(!this.isLink(text)){ | |
return false; | |
} | |
else{ | |
return IMAGEREGEX.test(text); | |
} | |
}; | |
/** | |
* Gets the video id of the provided youtube link | |
* | |
* @param {String} link The text youtube link | |
* @return {String|Null} The id of the video, returns null if not found | |
*/ | |
$Link.getYoutubeId = function(link){ | |
if(!this.isYoutube(link)){ | |
return null; | |
} | |
else{ | |
return YOUTUBEREGEX.exec(link)[1]; | |
} | |
}; | |
/** | |
* Gets all the detected url links in a text | |
* | |
* @param {String} text The text where the links are contained | |
* @return {Array} The list of links detected | |
*/ | |
$Link.getLinks = function(text){ | |
var matches = []; | |
var match; | |
while((match = LINKREGEX.exec(text)) != null){ | |
matches.push(match[0]); | |
} | |
LINKREGEX.lastIndex = 0; | |
return matches; | |
}; | |
/** | |
* Parses all links in a text and returns a new text with formated links | |
* | |
* @param {String} text The text where the links are contained | |
* @return {String} The new text with HTML formatted links | |
*/ | |
$Link.parseLinks = function(text){ | |
if(!this.isLink(text)){ | |
return text; | |
} | |
else{ | |
return text.replace(LINKREGEX, function(match){ | |
var link = window.document.createElement("a"); | |
var text = window.document.createTextNode(match); | |
link.href = match; | |
link.target = "_blank"; | |
link.appendChild(text); | |
return link.outerHTML; | |
}); | |
} | |
}; | |
/** | |
* Requests the service for a link preview | |
* | |
* @param {String} text The link text | |
* @return {Promise} The result of the request | |
*/ | |
$Link.getPreview = function(text){ | |
if(!this.isLink(text)){ | |
return $q.reject(new Error("Cannot preview from non-link text")); | |
} | |
else{ | |
var options = { | |
params: { | |
key: KEY, | |
q: text | |
} | |
}; | |
return $http.get(SERVICE, options) | |
.then(function(response){ | |
return response.data; | |
}); | |
} | |
}; | |
/** | |
* Creates an iframe tag with the embeded youtube video | |
* | |
* @param {String} link The text where the youtube video is contained | |
* @return {String|Null} The iframe where the youtuve video is embeded, returns null if not youtube link | |
*/ | |
$Link.toYoutube = function(link){ | |
if(!this.isYoutube(link)){ | |
return null; | |
} | |
else{ | |
return "https://youtube.com/embed/" + this.getYoutubeId(link); | |
} | |
}; | |
/** | |
* Creates a img tag with the image | |
* | |
* @param {String} link The text where the image link is contained | |
* @return {String|Null} The img tag where the image is, returns null if not image link | |
*/ | |
$Link.toImage = function(link){ | |
if(!this.isImage(link)){ | |
return null; | |
} | |
else{ | |
var img = window.document.createElement("img"); | |
img.src = link; | |
return img.outerHTML; | |
} | |
}; | |
return $Link; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment