Merge pull request #1288 from zacgeis/codeformat-fix

Avoid filtering markdown code blocks
This commit is contained in:
Hannah Wolfe 2013-10-28 04:19:55 -07:00
commit c364ec472a
2 changed files with 28 additions and 5 deletions

View File

@ -19,7 +19,7 @@
// GFM newline and underscore modifications, happen BEFORE showdown
type : 'lang',
filter : function (text) {
var preExtractions = {},
var extractions = {},
imageMarkdownRegex = /^(?:\{(.*?)\})?!(?:\[([^\n\]]*)\])(?:\(([^\n\]]*)\))?$/gim,
hashID = 0;
@ -30,15 +30,27 @@
// Extract pre blocks
text = text.replace(/<pre>[\s\S]*?<\/pre>/gim, function (x) {
var hash = hashId();
preExtractions[hash] = x;
extractions[hash] = x;
return "{gfm-js-extract-pre-" + hash + "}";
}, 'm');
// Extract code blocks
text = text.replace(/```[\s\S]*```/gim, function (x) {
var hash = hashId();
extractions[hash] = x;
return "{gfm-js-extract-code-" + hash + "}";
}, 'm');
//prevent foo_bar and foo_bar_baz from ending up with an italic word in the middle
text = text.replace(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/gm, function (x) {
return x.replace(/_/gm, '\\_');
});
text = text.replace(/\{gfm-js-extract-code-([0-9]+)\}/gm, function (x, y) {
return extractions[y];
});
// in very clear cases, let newlines become <br /> tags
text = text.replace(/^[\w\<][^\n]*\n+/gm, function (x) {
return x.match(/\n{2}/) ? x : x.trim() + " \n";
@ -54,7 +66,7 @@
});
text = text.replace(/\{gfm-js-extract-pre-([0-9]+)\}/gm, function (x, y) {
return "\n\n" + preExtractions[y];
return "\n\n" + extractions[y];
});
@ -127,4 +139,4 @@
if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.github = github; }
// Server-side export
if (typeof module !== 'undefined') module.exports = github;
}());
}());

View File

@ -279,6 +279,17 @@ describe("Showdown client side converter", function () {
});
});
it("should NOT escape underscore inside of code/pre blocks", function() {
var testPhrase = {
input: "```\n_____\n```",
output: /^<pre><code>_____ \n<\/code><\/pre>$/
} ,
processedMarkup;
processedMarkup = converter.makeHtml(testPhrase.input);
processedMarkup.should.match(testPhrase.output);
});
it("should NOT auto-link URLS inside of code/pre blocks", function () {
var testPhrases = [
{
@ -437,4 +448,4 @@ describe("Showdown client side converter", function () {
processedMarkup.should.match(testPhrase.output);
});
});
});
});