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)); }
lineNumber++;
while (next.length > 0) {
var b = next.shift(),
n = b.match(/([^`]*)```([^`]*)/m),
diff = ((typeof b.lineNumber === "undefined") ? lineNumber : b.lineNumber) - lineNumber;
blockLine = b.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++) {
codeContents.push("");
}
lineNumber = blockLine + b.split("\n").length - 1;
if (n) {
if (n[2]) {
@ -63,6 +67,7 @@ Discourse.Dialect.on("register", function(event) {
codeContents.push(b);
}
}
result.push(['p', ['pre', ['code', {'class': m[1] || 'lang-auto'}, codeContents.join("\n") ]]]);
return result;
}

View File

@ -168,6 +168,10 @@ test("Oneboxing", 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```",
"<p><pre><code class=\"lang-auto\">test</code></pre></p>",
"it supports basic code blocks");