FIX: code block hoisting bug

This commit is contained in:
Régis Hanol 2015-03-12 11:17:00 +01:00
parent 12d59d13a7
commit 9061c26e52

View file

@ -181,6 +181,8 @@ function hoistCodeBlocksAndSpans(text) {
// replace all "\`" with a single character // replace all "\`" with a single character
text = hideBackslashEscapedCharacters(text); text = hideBackslashEscapedCharacters(text);
// /!\ the order is important /!\
// <pre>...</pre> code blocks // <pre>...</pre> code blocks
text = text.replace(/(^\n*|\n\n)<pre>([\s\S]*?)<\/pre>/ig, function(_, before, content) { text = text.replace(/(^\n*|\n\n)<pre>([\s\S]*?)<\/pre>/ig, function(_, before, content) {
var hash = md5(content); var hash = md5(content);
@ -188,6 +190,13 @@ function hoistCodeBlocksAndSpans(text) {
return before + "<pre>" + hash + "</pre>"; 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) {
var hash = md5(content);
hoisted[hash] = escape(showBackslashEscapedCharacters(content.trim()));
return before + "```" + language + "\n" + hash + "\n```";
});
// markdown code blocks // markdown code blocks
text = text.replace(/(^\n*|\n\n)((?:(?:[ ]{4}|\t).*\n*)+)/g, function(match, before, content, index) { text = text.replace(/(^\n*|\n\n)((?:(?:[ ]{4}|\t).*\n*)+)/g, function(match, before, content, index) {
// make sure we aren't in a list // make sure we aren't in a list
@ -206,13 +215,6 @@ function hoistCodeBlocksAndSpans(text) {
return before + " " + hash + "\n"; 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 `) // code spans (double & single `)
["``", "`"].forEach(function(delimiter) { ["``", "`"].forEach(function(delimiter) {
var regexp = new RegExp("(^|[^`])" + delimiter + "([^`\\n]+?)" + delimiter + "([^`]|$)", "g"); var regexp = new RegExp("(^|[^`])" + delimiter + "([^`\\n]+?)" + delimiter + "([^`]|$)", "g");