Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabled global HTML escaping, while always escaping within code blocks. #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1434,10 +1434,14 @@ function escapeHTML( text ) {
.replace( /'/g, "'" );
}

function render_tree( jsonml ) {
function render_tree( jsonml, options ) {
options = options || {};
// escape HTML in strings?
options.escape = options.escape || false;

// basic case
if ( typeof jsonml === "string" ) {
return escapeHTML( jsonml );
return options.escape ? escapeHTML( jsonml ) : jsonml;
}

var tag = jsonml.shift(),
Expand All @@ -1449,7 +1453,7 @@ function render_tree( jsonml ) {
}

while ( jsonml.length ) {
content.push( render_tree( jsonml.shift() ) );
content.push( render_tree( jsonml.shift(), {escape: tag == 'code'} ) );
}

var tag_attrs = "";
Expand Down
13 changes: 13 additions & 0 deletions test/interface.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,16 @@ test("arguments untouched", function(t) {

t.end();
});

test("code escaped", function(t){
var input = "Here is an <i>example</i> of HTML:\n\n <p>Paragraph</p>",
tree = markdown.parse( input ),
output = markdown.toHTML( tree ),
expected = "<p>Here is an <i>example</i> of HTML:</p>\n\n<pre><code>&lt;p&gt;Paragraph&lt;/p&gt;</code></pre>";

// Escaping is done at the toHTML stage, so we test our escaping here,
// and not in features.
t.equivalent( expected, output, "HTML inside code blocks is escaped" );

t.end();
});