diff --git a/app/assets/javascripts/discourse/dialects/mention_dialect.js b/app/assets/javascripts/discourse/dialects/mention_dialect.js index 554c6fe13db..29eef5f37f8 100644 --- a/app/assets/javascripts/discourse/dialects/mention_dialect.js +++ b/app/assets/javascripts/discourse/dialects/mention_dialect.js @@ -11,87 +11,30 @@ Discourse.Dialect.on("register", function(event) { var dialect = event.dialect, MD = event.MD; - /** - Support for github style code blocks + dialect.inline['@'] = function(text, match, prev) { + var args = Array.prototype.slice.call(arguments); - @method mentionSupport - @param {Markdown.Block} block the block to examine - @param {Array} next the next blocks in the sequence - @return {Array} the JsonML containing the markup or undefined if nothing changed. - @namespace Discourse.Dialect - **/ - dialect.block['mentions'] = function mentionSupport(block, next) { - var pattern = /(\W|^)(@[A-Za-z0-9][A-Za-z0-9_]{2,14})(?=(\W|$))/gm, - result, - remaining = block, - m, - mentionLookup = dialect.options.mentionLookup || Discourse.Mention.lookupCache; + // We only care about mentions on word boundaries + if (prev && (prev.length > 0)) { + var last = prev[prev.length - 1]; + if (typeof last === "string" && (!last.match(/\W$/))) { return; } + } - if (block.match(/^ {3}/)) { return; } - if (block.match(/^\>/)) { return; } + var pattern = /^(@[A-Za-z0-9][A-Za-z0-9_]{2,14})(?=(\W|$))/m, + m = pattern.exec(text); - var pushIt = function(p) { result.push(p) }, - backtickCount = 0, - dirty = false; - - while (m = pattern.exec(remaining)) { - result = result || ['p']; - - var username = m[2], - usernameIndex = remaining.indexOf(username), - before = remaining.slice(0, usernameIndex), - prevBacktickCount = backtickCount; - - - pattern.lastIndex = 0; - backtickCount = prevBacktickCount + (before.split('`').length - 1); - var dontMention = ((backtickCount % 2) === 1); - - if (dontMention) { - before = before + username; - remaining = remaining.slice(usernameIndex + username.length); - - var nextMention = remaining.indexOf("@"); - if (nextMention !== -1) { - before = before + remaining.slice(0, nextMention); - backtickCount = prevBacktickCount + (before.split('`').length - 1); - remaining = remaining.slice(nextMention); - this.processInline(before).forEach(pushIt); - continue; - } else { - before = before + remaining; - remaining = ""; - } + if (m) { + var username = m[1], + mentionLookup = dialect.options.mentionLookup || Discourse.Mention.lookupCache, + index = prev.indexOf(username); + if (mentionLookup(username.substr(1))) { + return [username.length, ['a', {'class': 'mention', href: Discourse.getURL("/users/") + username.substr(1).toLowerCase()}, username]]; } else { - remaining = remaining.slice(usernameIndex + username.length); - } - - if (before) { - this.processInline(before).forEach(pushIt); - } - - if (!dontMention) { - if (mentionLookup(username.substr(1))) { - result.push(['a', {'class': 'mention', href: Discourse.getURL("/users/") + username.substr(1).toLowerCase()}, username]); - } else { - result.push(['span', {'class': 'mention'}, username]); - } - dirty = true; - } - - if (remaining && remaining.match(/\n/)) { - next.unshift(MD.mk_block(remaining)); - return [result]; + return [username.length, ['span', {'class': 'mention'}, username]]; } } - if (dirty && result) { - if (remaining.length) { - this.processInline(remaining).forEach(pushIt); - } - return [result]; - } }; }); diff --git a/test/javascripts/components/markdown_test.js b/test/javascripts/components/markdown_test.js index 6d900d18c5b..f60bc7b4cf3 100644 --- a/test/javascripts/components/markdown_test.js +++ b/test/javascripts/components/markdown_test.js @@ -129,7 +129,7 @@ test("Mentions", function() { "handles mentions in simple quotes"); cooked("> foo bar baz @eviltrout ohmagerd\nlook at this", - "
", + "foo bar baz @eviltrout
ohmagerd\nlook at this
", "does mentions properly with trailing text within a simple quote"); cooked("`code` is okay before @mention", @@ -152,6 +152,10 @@ test("Mentions", function() { "foo bar baz @eviltrout ohmagerd\nlook at this
@eviltrout and @eviltrout
this is a list
this is an @eviltrout mention