Fixes @mentions inside of lists

This commit is contained in:
Robin Ward 2013-08-22 17:50:07 -04:00
parent 86012ac579
commit 513f941f50
2 changed files with 21 additions and 74 deletions

View File

@ -11,87 +11,30 @@ Discourse.Dialect.on("register", function(event) {
var dialect = event.dialect, var dialect = event.dialect,
MD = event.MD; MD = event.MD;
/** dialect.inline['@'] = function(text, match, prev) {
Support for github style code blocks var args = Array.prototype.slice.call(arguments);
@method mentionSupport // We only care about mentions on word boundaries
@param {Markdown.Block} block the block to examine if (prev && (prev.length > 0)) {
@param {Array} next the next blocks in the sequence var last = prev[prev.length - 1];
@return {Array} the JsonML containing the markup or undefined if nothing changed. if (typeof last === "string" && (!last.match(/\W$/))) { return; }
@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;
if (block.match(/^ {3}/)) { return; } var pattern = /^(@[A-Za-z0-9][A-Za-z0-9_]{2,14})(?=(\W|$))/m,
if (block.match(/^\>/)) { return; } m = pattern.exec(text);
var pushIt = function(p) { result.push(p) }, if (m) {
backtickCount = 0, var username = m[1],
dirty = false; mentionLookup = dialect.options.mentionLookup || Discourse.Mention.lookupCache,
index = prev.indexOf(username);
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 (mentionLookup(username.substr(1))) {
return [username.length, ['a', {'class': 'mention', href: Discourse.getURL("/users/") + username.substr(1).toLowerCase()}, username]];
} else { } else {
remaining = remaining.slice(usernameIndex + username.length); return [username.length, ['span', {'class': 'mention'}, username]];
}
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];
} }
} }
if (dirty && result) {
if (remaining.length) {
this.processInline(remaining).forEach(pushIt);
}
return [result];
}
}; };
}); });

View File

@ -129,7 +129,7 @@ test("Mentions", function() {
"handles mentions in simple quotes"); "handles mentions in simple quotes");
cooked("> foo bar baz @eviltrout ohmagerd\nlook at this", cooked("> foo bar baz @eviltrout ohmagerd\nlook at this",
"<blockquote><p>foo bar baz <span class=\"mention\">@eviltrout</span></p><p> ohmagerd\nlook at this</p></blockquote>", "<blockquote><p>foo bar baz <span class=\"mention\">@eviltrout</span> ohmagerd\nlook at this</p></blockquote>",
"does mentions properly with trailing text within a simple quote"); "does mentions properly with trailing text within a simple quote");
cooked("`code` is okay before @mention", cooked("`code` is okay before @mention",
@ -152,6 +152,10 @@ test("Mentions", function() {
"<p><span class=\"mention\">@eviltrout</span> and <code>@eviltrout</code></p>", "<p><span class=\"mention\">@eviltrout</span> and <code>@eviltrout</code></p>",
"you can have a mention in an inline code block following a real mention."); "you can have a mention in an inline code block following a real mention.");
cooked("1. this is a list\n\n2. this is an @eviltrout mention\n",
"<ol><li><p>this is a list</p></li><li><p>this is an <span class=\"mention\">@eviltrout</span> mention </p></li></ol>",
"it mentions properly in a list.");
}); });
test("Oneboxing", function() { test("Oneboxing", function() {