From 19c74af27a2f40da94fd97e1b4c0418fc106e896 Mon Sep 17 00:00:00 2001 From: Juanfran Date: Wed, 22 Mar 2017 11:01:18 +0100 Subject: [PATCH] allow dot mention overriding Autolinker parser --- app/js/medium-mention.js | 2 +- .../wysiwyg/wysiwyg-mention.service.coffee | 3 ++ .../components/wysiwyg/wysiwyg.service.coffee | 42 ++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/js/medium-mention.js b/app/js/medium-mention.js index 5b0fe821..fd549b40 100644 --- a/app/js/medium-mention.js +++ b/app/js/medium-mention.js @@ -46,8 +46,8 @@ var MentionExtension = MediumEditor.Extension.extend({ var endChar = this.selection.getRangeAt(0).startOffset; var textContent = this.selection.focusNode.textContent; - this.word = this.getLastWord(textContent); textContent = textContent.substring(0, endChar); + this.word = this.getLastWord(textContent); if (this.word.length > 1 && ['@', '#', ':'].indexOf(this.word[0]) != -1) { this.wrap(); diff --git a/app/modules/components/wysiwyg/wysiwyg-mention.service.coffee b/app/modules/components/wysiwyg/wysiwyg-mention.service.coffee index d450d095..0e1a2073 100644 --- a/app/modules/components/wysiwyg/wysiwyg-mention.service.coffee +++ b/app/modules/components/wysiwyg/wysiwyg-mention.service.coffee @@ -46,6 +46,9 @@ class WysiwygMentionService for prop in searchProps if taiga.slugify(user[prop]).indexOf(term) >= 0 return true + else if user[prop].indexOf(term) >= 0 + return true + return false users = users.slice(0, 10).map (it) => diff --git a/app/modules/components/wysiwyg/wysiwyg.service.coffee b/app/modules/components/wysiwyg/wysiwyg.service.coffee index 509091c6..ee372337 100644 --- a/app/modules/components/wysiwyg/wysiwyg.service.coffee +++ b/app/modules/components/wysiwyg/wysiwyg.service.coffee @@ -145,8 +145,43 @@ class WysiwygService return markdown + parseMentionMatches: (text) -> + serviceName = 'twitter' + tagBuilder = this.tagBuilder + matches = [] + + regex = /@[^\s]{1,50}[^.\s]/g + m = regex.exec(text) + + while m != null + offset = m.index + prevChar = text.charAt( offset - 1 ) + + if m.index == regex.lastIndex + regex.lastIndex++ + + m.forEach (match, groupIndex) -> + matches.push( new Autolinker.match.Mention({ + tagBuilder : tagBuilder, + matchedText : match, + offset : offset, + serviceName : serviceName, + mention : match.slice(1) + })) + + m = regex.exec(text) + + return matches + autoLinkHTML: (html) -> - return Autolinker.link(html, { + # override Autolink parser + + matchRegexStr = String(Autolinker.matcher.Mention.prototype.matcherRegexes.twitter) + if matchRegexStr.indexOf('.') == -1 + matchRegexStr = '@[^\s]{1,50}[^.\s]' + #Autolinker.matcher.Mention.prototype.matcherRegexes.twitter = new RegExp(matchRegexStr, 'g') + + autolinker = new Autolinker({ mention: 'twitter', hashtag: 'twitter', replaceFn: (match) => @@ -166,6 +201,10 @@ class WysiwygService return '#' + match.getHashtag() + '' }) + Autolinker.matcher.Mention.prototype.parseMatches = @.parseMentionMatches.bind(autolinker) + + return autolinker.link(html); + getHTML: (text) -> return "" if !text || !text.length @@ -181,7 +220,6 @@ class WysiwygService }) md.use(window.markdownitLazyHeaders) - result = md.render(text) result = @.searchWikiLinks(result)