FIX: hoist code blocks content before doing any kind of processing

This commit is contained in:
Régis Hanol
2015-03-07 02:16:27 +01:00
parent 7a508b201a
commit f95c86ac72
3 changed files with 57 additions and 22 deletions
@@ -10,7 +10,7 @@ var acceptableCodeClasses =
"perl", "php", "profile", "python", "r", "rib", "rsl", "ruby", "rust", "scala", "smalltalk", "sql",
"tex", "text", "vala", "vbscript", "vhdl"];
var textCodeClasses = ["text", "pre"];
var textCodeClasses = ["text", "pre", "plain"];
function flattenBlocks(blocks) {
var result = "";
@@ -39,6 +39,17 @@ Discourse.Dialect.replaceBlock({
}
});
Discourse.Dialect.replaceBlock({
start: /(<pre[^\>]*\>)([\s\S]*)/igm,
stop: /<\/pre>/igm,
rawContents: true,
skipIfTradtionalLinebreaks: true,
emitter: function(blockContents) {
return ['p', ['pre', flattenBlocks(blockContents)]];
}
});
// Ensure that content in a code block is fully escaped. This way it's not white listed
// and we can use HTML and Javascript examples.
Discourse.Dialect.on('parseNode', function (event) {
@@ -51,7 +62,6 @@ Discourse.Dialect.on('parseNode', function (event) {
if (path && path[path.length-1] && path[path.length-1][0] && path[path.length-1][0] === "pre") {
regexp = / +$/g;
} else {
regexp = /^ +| +$/g;
}
@@ -59,17 +69,6 @@ Discourse.Dialect.on('parseNode', function (event) {
}
});
Discourse.Dialect.replaceBlock({
start: /(<pre[^\>]*\>)([\s\S]*)/igm,
stop: /<\/pre>/igm,
rawContents: true,
skipIfTradtionalLinebreaks: true,
emitter: function(blockContents) {
return ['p', ['pre', flattenBlocks(blockContents)]];
}
});
// Whitelist the language classes
var regexpSource = "^lang-(" + acceptableCodeClasses.join('|') + ")$";
Discourse.Markdown.whiteListTag('code', 'class', new RegExp(regexpSource, "i"));