From 9061c26e52e178448a15e9939c83aa8db043b1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 12 Mar 2015 11:17:00 +0100 Subject: [PATCH] FIX: code block hoisting bug --- .../javascripts/discourse/dialects/dialect.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/discourse/dialects/dialect.js b/app/assets/javascripts/discourse/dialects/dialect.js index 34ab5b5f0..d5d785e2a 100644 --- a/app/assets/javascripts/discourse/dialects/dialect.js +++ b/app/assets/javascripts/discourse/dialects/dialect.js @@ -181,6 +181,8 @@ function hoistCodeBlocksAndSpans(text) { // replace all "\`" with a single character text = hideBackslashEscapedCharacters(text); + // /!\ the order is important /!\ + //
...
code blocks text = text.replace(/(^\n*|\n\n)
([\s\S]*?)<\/pre>/ig, function(_, before, content) {
     var hash = md5(content);
@@ -188,6 +190,13 @@ function hoistCodeBlocksAndSpans(text) {
     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())); + return before + "```" + language + "\n" + hash + "\n```"; + }); + // markdown code blocks text = text.replace(/(^\n*|\n\n)((?:(?:[ ]{4}|\t).*\n*)+)/g, function(match, before, content, index) { // make sure we aren't in a list @@ -206,13 +215,6 @@ function hoistCodeBlocksAndSpans(text) { return before + " " + hash + "\n"; }); - // 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())); - return before + "```" + language + "\n" + hash + "\n```"; - }); - // code spans (double & single `) ["``", "`"].forEach(function(delimiter) { var regexp = new RegExp("(^|[^`])" + delimiter + "([^`\\n]+?)" + delimiter + "([^`]|$)", "g");