FIX: @username mentions when there are multiple code blocks in a post.

Meta: [All of my internal users show as coming from 127.0.0.1!](http://meta.discourse.org/t/all-of-my-internal-users-show-as-coming-from-127-0-0-1/6607)

This fixes the regular expression used to identify @ inside <code> or <pre> blocks in the `postConversion` hook.
This commit is contained in:
Régis Hanol 2013-05-16 01:59:07 +02:00
parent 0a56f64cee
commit 27dfb7a948
2 changed files with 10 additions and 2 deletions

View File

@ -143,8 +143,8 @@ Discourse.Markdown = {
if (!text) return "";
// don't do @username mentions inside <pre> or <code> blocks
text = text.replace(/<(pre|code)>([\s\S]*?@[\s\S]*?)<\/(pre|code)>/gi, function(wholeMatch, m1, m2, m3) {
return "<" + m1 + ">" + (m2.replace(/@/g, '&#64;')) + "</" + m3 + ">";
text = text.replace(/<(pre|code)>([\s\S](?!<(pre|code)>))*?@([\s\S](?!<(pre|code)>))*?<\/(pre|code)>/gi, function(m) {
return m.replace(/@/g, '&#64;');
});
// add @username mentions, if valid; must be bounded on left and right by non-word characters

View File

@ -92,6 +92,14 @@ describe("Discourse.Markdown", function() {
expect(cook("@EvilTrout yo")).toBe("<p><span class='mention'>@EvilTrout</span> yo</p>");
});
it("doesn't do @username mentions inside <pre> or <code> blocks", function() {
expect(cook("`@EvilTrout yo`")).toBe("<p><code>&#64;EvilTrout yo</code></p>");
});
it("deals correctly with multiple <code> blocks", function() {
expect(cook("`evil` @EvilTrout `trout`")).toBe("<p><code>evil</code> <span class='mention'>@EvilTrout</span> <code>trout</code></p>");
});
});
describe("Oneboxing", function() {