From 68f9e5da778a5c48e5929a96e3b359aa01d24d01 Mon Sep 17 00:00:00 2001 From: Viktor Oreshkin Date: Fri, 18 Nov 2016 02:19:12 -0500 Subject: [PATCH 1/2] Add support for bold and italic w/o bots Signed-off-by: Viktor Oreshkin --- app/js/lib/ng_utils.js | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) mode change 100644 => 100755 app/js/lib/ng_utils.js diff --git a/app/js/lib/ng_utils.js b/app/js/lib/ng_utils.js old mode 100644 new mode 100755 index ef10984d..cf8f0318 --- a/app/js/lib/ng_utils.js +++ b/app/js/lib/ng_utils.js @@ -1238,7 +1238,7 @@ angular.module('izhukov.utils', []) var soundcloudRegExp = /^https?:\/\/(?:soundcloud\.com|snd\.sc)\/([a-zA-Z0-9%\-\_]+)\/([a-zA-Z0-9%\-\_]+)/i var spotifyRegExp = /(https?:\/\/(open\.spotify\.com|play\.spotify\.com|spoti\.fi)\/(.+)|spotify:(.+))/i - var markdownRegExp = /(^|\s)(````?)([\s\S]+?)(````?)([\s\n\.,:?!;]|$)|(^|\s)`([^\n]+?)`([\s\.,:?!;]|$)|@(\d+)\s*\((.+?)\)/ + var markdownRegExp = /(^|\s)(````?)([\s\S]+?)(````?)([\s\n\.,:?!;]|$)|(^|\s)([`*_])([^\n]+?)\7([\s\.,:?!;]|$)|@(\d+)\s*\((.+?)\)/ var siteHashtags = { Telegram: 'tg://search_hashtag?hashtag={1}', @@ -1254,6 +1254,12 @@ angular.module('izhukov.utils', []) GitHub: 'https://github.com/{1}' } + var markdownEntities = { + '`': 'messageEntityCode', + '*': 'messageEntityBold', + '_': 'messageEntityItalic' + } + return { wrapRichText: wrapRichText, wrapPlainText: wrapPlainText, @@ -1402,7 +1408,7 @@ angular.module('izhukov.utils', []) } function parseMarkdown (text, entities, noTrim) { - if (text.indexOf('`') == -1 && text.indexOf('@') == -1) { +     if (!text.test(/[`_*@]/)) { return noTrim ? text : text.trim() } var raw = text @@ -1414,7 +1420,7 @@ angular.module('izhukov.utils', []) matchIndex = rawOffset + match.index newText.push(raw.substr(0, match.index)) - var text = (match[3] || match[7] || match[10]) + var text = (match[3] || match[8] || match[11]) rawOffset -= text.length text = text.replace(/^\s+|\s+$/g, '') rawOffset += text.length @@ -1435,23 +1441,23 @@ angular.module('izhukov.utils', []) length: text.length }) rawOffset -= match[2].length + match[4].length - } else if (match[7]) { // code - newText.push(match[6] + text + match[8]) + } else if (match[9]) { // code + newText.push(match[6] + text + match[9]) entities.push({ - _: 'messageEntityCode', + _: markdownEntities[match[7]], offset: matchIndex + match[6].length, length: text.length }) rawOffset -= 2 - } else if (match[10]) { // custom mention + } else if (match[11]) { // custom mention newText.push(text) entities.push({ _: 'messageEntityMentionName', - user_id: match[9], + user_id: match[10], offset: matchIndex, length: text.length }) - rawOffset -= match[0] - text.length + rawOffset -= match[0].length - text.length } raw = raw.substr(match.index + match[0].length) rawOffset += match.index + match[0].length @@ -1808,6 +1814,18 @@ angular.module('izhukov.utils', []) ) break + case 'messageEntityBold': + code.push( + '*', entityText, '*' + ) + break + + case 'messageEntityItalic': + code.push( + '_', entityText, '_' + ) + break + case 'messageEntityPre': code.push( '```', entityText, '```' @@ -1943,4 +1961,4 @@ angular.module('izhukov.utils', []) }) return timeParams - }) + }) \ No newline at end of file From 071bc509c4e8d2b616a65531832f67fde8cd4064 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Sat, 19 Nov 2016 18:14:52 +0300 Subject: [PATCH 2/2] Bugfixes --- app/js/lib/ng_utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/js/lib/ng_utils.js b/app/js/lib/ng_utils.js index cf8f0318..f682e2bf 100755 --- a/app/js/lib/ng_utils.js +++ b/app/js/lib/ng_utils.js @@ -1238,6 +1238,7 @@ angular.module('izhukov.utils', []) var soundcloudRegExp = /^https?:\/\/(?:soundcloud\.com|snd\.sc)\/([a-zA-Z0-9%\-\_]+)\/([a-zA-Z0-9%\-\_]+)/i var spotifyRegExp = /(https?:\/\/(open\.spotify\.com|play\.spotify\.com|spoti\.fi)\/(.+)|spotify:(.+))/i + var markdownTestRegExp = /[`_*@]/; var markdownRegExp = /(^|\s)(````?)([\s\S]+?)(````?)([\s\n\.,:?!;]|$)|(^|\s)([`*_])([^\n]+?)\7([\s\.,:?!;]|$)|@(\d+)\s*\((.+?)\)/ var siteHashtags = { @@ -1408,7 +1409,7 @@ angular.module('izhukov.utils', []) } function parseMarkdown (text, entities, noTrim) { -     if (!text.test(/[`_*@]/)) { +     if (!markdownTestRegExp.test(text)) { return noTrim ? text : text.trim() } var raw = text