diff --git a/app/assets/javascripts/discourse/dialects/dialect.js b/app/assets/javascripts/discourse/dialects/dialect.js index d5d785e2a3..9d0cbef8d7 100644 --- a/app/assets/javascripts/discourse/dialects/dialect.js +++ b/app/assets/javascripts/discourse/dialects/dialect.js @@ -167,6 +167,11 @@ function outdent(t) { return t.replace(/^([ ]{4}|\t)/gm, ""); } +function removeEmptyLines(t) { + return t.replace(/^\n+/, "") + .replace(/\s+$/, ""); +} + function hideBackslashEscapedCharacters(t) { return t.replace(/\\\\/g, "\u1E800") .replace(/\\`/g, "\u1E8001"); @@ -186,14 +191,14 @@ function hoistCodeBlocksAndSpans(text) { //
...code blocks text = text.replace(/(^\n*|\n\n)
([\s\S]*?)<\/pre>/ig, function(_, before, content) {
var hash = md5(content);
- hoisted[hash] = escape(showBackslashEscapedCharacters(content.trim()));
+ hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
return before + "" + hash + "
";
});
// fenced code blocks (AKA GitHub code blocks)
text = text.replace(/(^\n*|\n\n)```([a-z0-9\-]*)\n([\s\S]*?)\n```/g, function(_, before, language, content) {
var hash = md5(content);
- hoisted[hash] = escape(showBackslashEscapedCharacters(content.trim()));
+ hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
return before + "```" + language + "\n" + hash + "\n```";
});
@@ -209,9 +214,7 @@ function hoistCodeBlocksAndSpans(text) {
}
// we can safely hoist the code block
var hash = md5(content);
- // only remove trailing whitespace
- content = content.replace(/\s+$/, "");
- hoisted[hash] = escape(outdent(showBackslashEscapedCharacters(content)));
+ hoisted[hash] = escape(outdent(showBackslashEscapedCharacters(removeEmptyLines(content))));
return before + " " + hash + "\n";
});
diff --git a/test/javascripts/lib/markdown-test.js.es6 b/test/javascripts/lib/markdown-test.js.es6
index 6ae727c550..f5859165f1 100644
--- a/test/javascripts/lib/markdown-test.js.es6
+++ b/test/javascripts/lib/markdown-test.js.es6
@@ -529,6 +529,6 @@ test("censoring", function() {
test("code blocks/spans hoisting", function() {
cooked("```\n\n some code\n```",
- "some code
",
+ " some code
",
"it works when nesting standard markdown code blocks within a fenced code block");
});