FIX: fenced code blocks not hoisted correctly

also fixes unhoisting logic
This commit is contained in:
Sam
2015-07-31 17:53:20 +10:00
parent 6f9dc135ba
commit 568adc49c0
2 changed files with 19 additions and 4 deletions

View File

@@ -189,14 +189,15 @@ function hoistCodeBlocksAndSpans(text) {
// /!\ the order is important /!\
// <pre>...</pre> code blocks
text = text.replace(/(^\n*|\n\n)<pre>([\s\S]*?)<\/pre>/ig, function(_, before, content) {
text = text.replace(/(\s|^)<pre>([\s\S]*?)<\/pre>/ig, function(_, before, content) {
var hash = md5(content);
hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
return before + "<pre>" + hash + "</pre>";
});
// 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();