From 1f15df41ef8246fc16908f1dddcd5544cef03b69 Mon Sep 17 00:00:00 2001 From: Zach Geis Date: Fri, 25 Oct 2013 14:54:19 -0500 Subject: [PATCH] Avoid filtering markdown code blocks closes #1045 - Fixed markdown code block format issue. - Added test case to verify fix. --- .../vendor/showdown/extensions/github.js | 20 +++++++++++++++---- core/test/unit/client_showdown_int_spec.js | 13 +++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/core/shared/vendor/showdown/extensions/github.js b/core/shared/vendor/showdown/extensions/github.js index f0a8b1bfea..34c1f79265 100644 --- a/core/shared/vendor/showdown/extensions/github.js +++ b/core/shared/vendor/showdown/extensions/github.js @@ -19,7 +19,7 @@ // GFM newline and underscore modifications, happen BEFORE showdown type : 'lang', filter : function (text) { - var preExtractions = {}, + var extractions = {}, imageMarkdownRegex = /^(?:\{(.*?)\})?!(?:\[([^\n\]]*)\])(?:\(([^\n\]]*)\))?$/gim, hashID = 0; @@ -30,15 +30,27 @@ // Extract pre blocks text = text.replace(/
[\s\S]*?<\/pre>/gim, function (x) {
                         var hash = hashId();
-                        preExtractions[hash] = x;
+                        extractions[hash] = x;
                         return "{gfm-js-extract-pre-" + hash + "}";
                     }, 'm');
 
+                    // Extract code blocks
+                    text = text.replace(/```[\s\S]*```/gim, function (x) {
+                        var hash = hashId();
+                        extractions[hash] = x;
+                        return "{gfm-js-extract-code-" + hash + "}";
+                    }, 'm');
+
+
                     //prevent foo_bar and foo_bar_baz from ending up with an italic word in the middle
                     text = text.replace(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/gm, function (x) {
                         return x.replace(/_/gm, '\\_');
                     });
 
+                    text = text.replace(/\{gfm-js-extract-code-([0-9]+)\}/gm, function (x, y) {
+                        return extractions[y];
+                    });
+
                     // in very clear cases, let newlines become 
tags text = text.replace(/^[\w\<][^\n]*\n+/gm, function (x) { return x.match(/\n{2}/) ? x : x.trim() + " \n"; @@ -54,7 +66,7 @@ }); text = text.replace(/\{gfm-js-extract-pre-([0-9]+)\}/gm, function (x, y) { - return "\n\n" + preExtractions[y]; + return "\n\n" + extractions[y]; }); @@ -127,4 +139,4 @@ if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.github = github; } // Server-side export if (typeof module !== 'undefined') module.exports = github; -}()); \ No newline at end of file +}()); diff --git a/core/test/unit/client_showdown_int_spec.js b/core/test/unit/client_showdown_int_spec.js index ff95c524b5..88311df47a 100644 --- a/core/test/unit/client_showdown_int_spec.js +++ b/core/test/unit/client_showdown_int_spec.js @@ -279,6 +279,17 @@ describe("Showdown client side converter", function () { }); }); + it("should NOT escape underscore inside of code/pre blocks", function() { + var testPhrase = { + input: "```\n_____\n```", + output: /^
_____  \n<\/code><\/pre>$/
+        } ,
+        processedMarkup;
+
+        processedMarkup = converter.makeHtml(testPhrase.input);
+        processedMarkup.should.match(testPhrase.output);
+    });
+
     it("should NOT auto-link URLS inside of code/pre blocks", function () {
         var testPhrases = [
                 {
@@ -437,4 +448,4 @@ describe("Showdown client side converter", function () {
             processedMarkup.should.match(testPhrase.output);
         });
     });
-});
\ No newline at end of file
+});