From 578229810de3bc34a8d5f0e59ce028a31941a5e0 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Wed, 7 Oct 2015 21:16:48 +0300 Subject: [PATCH] Fixed markdown parsing Closes #916 --- app/js/lib/ng_utils.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/app/js/lib/ng_utils.js b/app/js/lib/ng_utils.js index 40fd51e2..d963e7b1 100644 --- a/app/js/lib/ng_utils.js +++ b/app/js/lib/ng_utils.js @@ -1146,7 +1146,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]+?)`/; + var markdownRegExp = /(^|\s)(````?)([\s\S]+?)(````?)([\s\.,:?!;]|$)|(^|\s)`([^\n]+?)`([\s\.,:?!;]|$)/; var siteHashtags = { Telegram: '#/im?q=%23{1}', @@ -1325,21 +1325,25 @@ angular.module('izhukov.utils', []) matchIndex = rawOffset + match.index; newText.push(raw.substr(0, match.index)); - if (match[3]) { // pre - newText.push(match[1] + match[3]); + var text = (match[3] || match[7]).replace(/^\s+|\s+$/g, ''); + if (text.match(/^`*$/)) { + newText.push(match[0]); + } + else if (match[3]) { // pre + newText.push(match[1] + text + match[5]); entities.push({ _: 'messageEntityPre', language: '', offset: matchIndex + match[1].length, - length: match[3].length + length: text.length }); rawOffset -= match[2].length + match[4].length; } else { // code - newText.push(match[5] + match[6]); + newText.push(match[6] + text + match[8]); entities.push({ _: 'messageEntityCode', - offset: matchIndex + match[5].length, - length: match[6].length + offset: matchIndex + match[6].length, + length: text.length }); rawOffset -= 2; } @@ -1347,7 +1351,13 @@ angular.module('izhukov.utils', []) rawOffset += match.index + match[0].length; } newText.push(raw); - return newText.join(''); + newText = newText.join(''); + + if (!newText.replace(/\s+/g, '').length) { + newText = text; + entities.splice(0, entities.length); + } + return newText; } function mergeEntities (currentEntities, newEntities, fromApi) {