From 84e3ec7856d13886db4b2187746445d3e1d0b6d4 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Tue, 16 Dec 2014 00:20:03 -0800 Subject: [PATCH] Highlighting usernames in text --- app/js/services.js | 56 ++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/app/js/services.js b/app/js/services.js index 8f5afbe7..75b5e73c 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -3233,7 +3233,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) "\\uffa1-\\uffdc"; // half width Hangul (Korean) var regexAlphaNumericChars = "0-9\.\_" + regexAlphaChars; - var regExp = new RegExp('((?:(ftp|https?)://|(?:mailto:)?([A-Za-z0-9._%+-]+@))(\\S*\\.\\S*[^\\s.;,(){}<>"\']))|(\\n)|(' + emojiUtf.join('|') + ')|(^|\\s)(#[' + regexAlphaNumericChars + ']{2,20})', 'i'); + var regExp = new RegExp('(^|\\s)((?:https?://)?telegram\\.me/|@)([a-zA-Z\\d_\\.]{5,32})|((?:(ftp|https?)://|(?:mailto:)?([A-Za-z0-9._%+-]+@))(\\S*\\.\\S*[^\\s.;,(){}<>"\']))|(\\n)|(' + emojiUtf.join('|') + ')|(^|\\s)(#[' + regexAlphaNumericChars + ']{2,20})', 'i'); + var youtubeRegex = /(?:https?:\/\/)?(?:www\.)?youtu(?:|\.be|be\.com|\.b)(?:\/v\/|\/watch\\?v=|e\/|(?:\/\??#)?\/watch(?:.+)v=)(.{11})(?:\&[^\s]*)?/; var vimeoRegex = /(?:https?:\/\/)?(?:www\.)?vimeo\.com\/(\d+)/; var instagramRegex = /https?:\/\/(?:instagr\.am\/p\/|instagram\.com\/p\/)([a-zA-Z0-9\-\_]+)/i; @@ -3285,22 +3286,39 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) // console.log(2, match); html.push(encodeEntities(raw.substr(0, match.index))); - if (match[1]) { // URL + if (match[3]) { // telegram.me links + if (!options.noLinks) { + html.push( + match[1], + '', + encodeEntities(match[2] + match[3]), + '' + ); + } else { + html.push( + match[1], + encodeEntities(match[2] + match[3]) + ); + } + } + else if (match[4]) { // URL if (!options.noLinks) { - if (match[3]) { + if (match[6]) { html.push( '', - encodeEntities(match[3] + match[4]), + encodeEntities(match[6] + match[7]), '' ); } else { html.push( '', - encodeEntities(match[2] + '://' + match[4]), + encodeEntities(match[5] + '://' + match[7]), '' ); } @@ -3308,16 +3326,16 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) html.push(encodeEntities(match[0])); } } - else if (match[5]) { // New line + else if (match[8]) { // New line if (!options.noLinebreaks) { html.push('
'); } else { html.push(' '); } } - else if (match[6]) { + else if (match[9]) { - if ((emojiCode = emojiMap[match[6]]) && + if ((emojiCode = emojiMap[match[9]]) && (emojiCoords = getEmojiSpritesheetCoords(emojiCode))) { emojiTitle = encodeEntities(emojiData[emojiCode][1][0]); @@ -3334,23 +3352,23 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) ':', emojiTitle, ':' ); } else { - html.push(encodeEntities(match[6])); + html.push(encodeEntities(match[9])); } } - else if (match[8]) { + else if (match[11]) { if (!options.noLinks) { html.push( - match[7], + match[10], '', - encodeEntities(match[8]), + encodeEntities(match[11]), '' ); } else { html.push( - match[7], - encodeEntities(match[8]) + match[10], + encodeEntities(match[11]) ); } } @@ -3430,8 +3448,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) while ((match = raw.match(regExp))) { text.push(raw.substr(0, match.index)); - if (match[6]) { - if ((emojiCode = emojiMap[match[6]]) && + if (match[9]) { + if ((emojiCode = emojiMap[match[9]]) && (emojiTitle = emojiData[emojiCode][1][0])) { text.push(':' + emojiTitle + ':'); } else {