diff --git a/app/assets/javascripts/discourse/dialects/github_code_dialect.js b/app/assets/javascripts/discourse/dialects/github_code_dialect.js
index ffa747e5843..515c4d0edfe 100644
--- a/app/assets/javascripts/discourse/dialects/github_code_dialect.js
+++ b/app/assets/javascripts/discourse/dialects/github_code_dialect.js
@@ -61,7 +61,7 @@ Discourse.Dialect.on("register", function(event) {
             next.unshift(MD.mk_block(n[2]));
           }
 
-          codeContents.push(n[1].trim());
+          codeContents.push(n[1].replace(/\s+$/, ""));
           break;
         } else {
           codeContents.push(b);
diff --git a/test/javascripts/components/markdown_test.js b/test/javascripts/components/markdown_test.js
index 877ecb6ddff..8bea0094cd2 100644
--- a/test/javascripts/components/markdown_test.js
+++ b/test/javascripts/components/markdown_test.js
@@ -217,6 +217,10 @@ test("Code Blocks", function() {
   cooked("    ```\n    hello\n    ```",
          "<pre><code>&#x60;&#x60;&#x60;\nhello\n&#x60;&#x60;&#x60;</code></pre>",
          "only detect ``` at the begining of lines");
+
+  cooked("```ruby\ndef self.parse(text)\n\n  text\nend\n```",
+         "<p><pre><code class=\"ruby\">def self.parse(text)\n\n  text\nend</code></pre></p>",
+         "it allows leading spaces on lines in a code block.");
 });
 
 test("SanitizeHTML", function() {