FIX: New lines in code blocks were somewhat unpredictable

This commit is contained in:
Robin Ward 2013-08-22 13:46:51 -04:00
parent 0f27232711
commit 5e50c4624f
2 changed files with 12 additions and 3 deletions

View File

@ -42,15 +42,19 @@ Discourse.Dialect.on("register", function(event) {
if (m[2]) { next.unshift(MD.mk_block(m[2], null, lineNumber + 1)); } if (m[2]) { next.unshift(MD.mk_block(m[2], null, lineNumber + 1)); }
lineNumber++;
while (next.length > 0) { while (next.length > 0) {
var b = next.shift(), var b = next.shift(),
n = b.match(/([^`]*)```([^`]*)/m), blockLine = b.lineNumber,
diff = ((typeof b.lineNumber === "undefined") ? lineNumber : b.lineNumber) - lineNumber; diff = ((typeof blockLine === "undefined") ? lineNumber : blockLine) - lineNumber;
b = b.replace(/ {2}\n/g, "\n");
var n = b.match(/([^`]*)```([^`]*)/m);
lineNumber = b.lineNumber;
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;
if (n) { if (n) {
if (n[2]) { if (n[2]) {
@ -63,6 +67,7 @@ Discourse.Dialect.on("register", function(event) {
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

@ -168,6 +168,10 @@ test("Oneboxing", function() {
test("Code Blocks", function() { test("Code Blocks", function() {
cooked("```\na\nb\nc\n\nd\n```",
"<p><pre><code class=\"lang-auto\">a\nb\nc\n\nd</code></pre></p>",
"it treats new lines properly");
cooked("```\ntest\n```", cooked("```\ntest\n```",
"<p><pre><code class=\"lang-auto\">test</code></pre></p>", "<p><pre><code class=\"lang-auto\">test</code></pre></p>",
"it supports basic code blocks"); "it supports basic code blocks");