FIX: @mentions within backticks followed by proper @mentions.

This commit is contained in:
Robin Ward 2013-08-22 15:54:41 -04:00
parent 32655e9dad
commit 6fe0d29a01
2 changed files with 36 additions and 11 deletions

View File

@ -30,30 +30,51 @@ Discourse.Dialect.on("register", function(event) {
if (block.match(/^ {3}/)) { return; } if (block.match(/^ {3}/)) { return; }
if (block.match(/^\>/)) { return; } if (block.match(/^\>/)) { return; }
var pushIt = function(p) { result.push(p) }; var pushIt = function(p) { result.push(p) },
backtickCount = 0,
dirty = false;
while (m = pattern.exec(remaining)) { while (m = pattern.exec(remaining)) {
result = result || ['p']; result = result || ['p'];
var username = m[2], var username = m[2],
usernameIndex = remaining.indexOf(username), usernameIndex = remaining.indexOf(username),
before = remaining.slice(0, usernameIndex); before = remaining.slice(0, usernameIndex),
prevBacktickCount = backtickCount;
// Break out if there is an uneven amount of backticks before
var backtickCount = before.split('`').length - 1;
if ((backtickCount % 2) === 1) { return; }
pattern.lastIndex = 0; pattern.lastIndex = 0;
remaining = remaining.slice(usernameIndex + username.length); 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 {
remaining = remaining.slice(usernameIndex + username.length);
}
if (before) { if (before) {
this.processInline(before).forEach(pushIt); this.processInline(before).forEach(pushIt);
} }
if (mentionLookup(username.substr(1))) { if (!dontMention) {
result.push(['a', {'class': 'mention', href: Discourse.getURL("/users/") + username.substr(1).toLowerCase()}, username]); if (mentionLookup(username.substr(1))) {
} else { result.push(['a', {'class': 'mention', href: Discourse.getURL("/users/") + username.substr(1).toLowerCase()}, username]);
result.push(['span', {'class': 'mention'}, username]); } else {
result.push(['span', {'class': 'mention'}, username]);
}
dirty = true;
} }
if (remaining && remaining.match(/\n/)) { if (remaining && remaining.match(/\n/)) {
@ -62,7 +83,7 @@ Discourse.Dialect.on("register", function(event) {
} }
} }
if (result) { if (dirty && result) {
if (remaining.length) { if (remaining.length) {
this.processInline(remaining).forEach(pushIt); this.processInline(remaining).forEach(pushIt);
} }

View File

@ -144,6 +144,10 @@ test("Mentions", function() {
"<p>don't <code>@mention</code></p>", "<p>don't <code>@mention</code></p>",
"Does not mention in an inline code block"); "Does not mention in an inline code block");
cooked("Yes `@this` should be code @eviltrout",
"<p>Yes <code>@this</code> should be code <span class=\"mention\">@eviltrout</span></p>",
"Does not mention in an inline code block");
}); });
test("Oneboxing", function() { test("Oneboxing", function() {