diff --git a/app/assets/javascripts/discourse/dialects/dialect.js b/app/assets/javascripts/discourse/dialects/dialect.js index 62febca1a7..b54011180a 100644 --- a/app/assets/javascripts/discourse/dialects/dialect.js +++ b/app/assets/javascripts/discourse/dialects/dialect.js @@ -189,14 +189,15 @@ function hoistCodeBlocksAndSpans(text) { // /!\ the order is important /!\ //
...
code blocks - text = text.replace(/(^\n*|\n\n)
([\s\S]*?)<\/pre>/ig, function(_, before, content) {
+  text = text.replace(/(\s|^)
([\s\S]*?)<\/pre>/ig, function(_, before, content) {
     var hash = md5(content);
     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) { + text = text.replace(/(^\n*|\n)```([a-z0-9\-]*)\n([\s\S]*?)\n```/g, function(_, before, language, content) { var hash = md5(content); hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content))); return before + "```" + language + "\n" + hash + "\n```"; @@ -277,11 +278,19 @@ Discourse.Dialect = { // If we hoisted out anything, put it back var keys = Object.keys(hoisted); if (keys.length) { - keys.forEach(function(key) { + var found = true; + + var unhoist = function(key) { result = result.replace(new RegExp(key, "g"), function() { + found = true; return hoisted[key]; }); - }); + }; + + while(found) { + found = false; + keys.forEach(unhoist); + } } return result.trim(); diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 8cffddd192..f9c7333c61 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -322,6 +322,12 @@ describe PrettyText do expect(PrettyText.cook("```cpp\ncpp\n```")).to match_html("

cpp
") end + it 'indents code correctly' do + code = "X\n```\n\n #\n x\n```" + cooked = PrettyText.cook(code) + expect(cooked).to match_html("

X

\n\n

    #\n    x
") + end + it 'can substitute s3 cdn correctly' do SiteSetting.enable_s3_uploads = true SiteSetting.s3_access_key_id = "XXX"