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,
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];
}
};
});

View file

@ -129,7 +129,7 @@ test("Mentions", function() {
"handles mentions in simple quotes");
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");
cooked("`code` is okay before @mention",
@ -152,6 +152,10 @@ test("Mentions", function() {
"<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.");
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() {