FIX: Allow backticks in code blocks

This commit is contained in:
Robin Ward 2013-08-26 16:53:10 -04:00
parent 1c3c468675
commit 6c3cb9023c
2 changed files with 12 additions and 6 deletions

View File

@ -49,26 +49,28 @@ Discourse.Dialect.on("register", function(event) {
blockLine = b.lineNumber, blockLine = b.lineNumber,
diff = ((typeof blockLine === "undefined") ? lineNumber : blockLine) - lineNumber; diff = ((typeof blockLine === "undefined") ? lineNumber : blockLine) - lineNumber;
b = b.replace(/ {2}\n/g, "\n"); var endFound = b.indexOf('```'),
var n = b.match(/([^`]*)```([^`]*)/m); leadingCode = b.slice(0, endFound),
trailingCode = b.slice(endFound+3);
for (var i=1; i<diff; i++) { for (var i=1; i<diff; i++) {
codeContents.push(""); codeContents.push("");
} }
lineNumber = blockLine + b.split("\n").length - 1; lineNumber = blockLine + b.split("\n").length - 1;
if (n) { if (endFound !== -1) {
if (n[2]) { if (trailingCode) {
next.unshift(MD.mk_block(n[2])); next.unshift(MD.mk_block(trailingCode));
} }
codeContents.push(n[1].replace(/\s+$/, "")); codeContents.push(leadingCode.replace(/\s+$/, ""));
break; break;
} else { } else {
codeContents.push(b); codeContents.push(b);
} }
} }
result.push(['p', ['pre', ['code', {'class': m[1] || 'lang-auto'}, codeContents.join("\n") ]]]); result.push(['p', ['pre', ['code', {'class': m[1] || 'lang-auto'}, codeContents.join("\n") ]]]);
return result; return result;
} }

View File

@ -231,6 +231,10 @@ test("Code Blocks", function() {
cooked("```ruby\ndef self.parse(text)\n\n text\nend\n```", cooked("```ruby\ndef self.parse(text)\n\n text\nend\n```",
"<p><pre><code class=\"ruby\">def self.parse(text)\n\n text\nend</code></pre></p>", "<p><pre><code class=\"ruby\">def self.parse(text)\n\n text\nend</code></pre></p>",
"it allows leading spaces on lines in a code block."); "it allows leading spaces on lines in a code block.");
cooked("```ruby\nhello `eviltrout`\n```",
"<p><pre><code class=\"ruby\">hello &#x60;eviltrout&#x60;</code></pre></p>",
"it allows code with backticks in it");
}); });
test("SanitizeHTML", function() { test("SanitizeHTML", function() {