Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created September 22, 2013 21:10
Show Gist options
  • Select an option

  • Save clouddueling/6663844 to your computer and use it in GitHub Desktop.

Select an option

Save clouddueling/6663844 to your computer and use it in GitHub Desktop.
detecting multimedia
$['Redactor']['fn']['formatLinkify'] = function (protocol, convertLinks, convertImageLinks, convertVideoLinks) {
var url1 = /(^|<|\s)(www\..+?\..+?)(\s|>|$)/g,
url2 = /(^|<|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|>|$)/g,
urlImage = /(https?:\/\/.*\.(?:png|jpg|jpeg|gif))/gi,
urlYoutube = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/,
urlVimeo = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
var childNodes = (this['$editor'] ? this['$editor']['get'](0) : this)['childNodes'],
i = childNodes['length'];
while (i--) {
var n = childNodes[i];
if (n['nodeType'] === 3) {
var html = n['nodeValue'];
if (convertVideoLinks && html) {
var iframeStart = '<iframe width="500" height="281" src="',
iframeEnd = '" frameborder="0" allowfullscreen></iframe>';
if (html['match'](urlYoutube)) {
html = html['replace'](urlYoutube, iframeStart + '//www.youtube.com/embed/$2' + iframeEnd);
$(n)['after'](html)['remove']();
} else {
if (html['match'](urlVimeo)) {
html = html['replace'](urlVimeo, iframeStart + '//player.vimeo.com/video/$2' + iframeEnd);
$(n)['after'](html)['remove']();
};
};
};
if (convertImageLinks && html && html['match'](urlImage)) {
html = html['replace'](urlImage, '<img src="$1">');
$(n)['after'](html)['remove']();
};
if (convertLinks && html && (html['match'](url1) || html['match'](url2))) {
var href = (html['match'](url1) || html['match'](url2));
href = href[0];
if (href['length'] > 50) {
href = href['substring'](0, 50) + '...';
};
html = html['replace'](/&/g, '&amp;')['replace'](/</g, '&lt;')['replace'](/>/g, '&gt;')['replace'](url1, '$1<a href="' + protocol + '$2">' + href + '</a>$3')['replace'](url2, '$1<a href="$2">' + href + '</a>$5');
$(n)['after'](html)['remove']();
};
} else {
if (n['nodeType'] === 1 && !/^(a|button|textarea)$/i ['test'](n['tagName'])) {
$['Redactor']['fn']['formatLinkify']['call'](n, protocol, convertLinks, convertImageLinks, convertVideoLinks);
};
};
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment