Skip to content

Commit 23f47bc

Browse files
Sanitize the output from the block level markdown renderers.
1 parent 171973f commit 23f47bc

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

libs/markdown.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@ var hljs = require('highlight.js');
33
var sanitizeHtml = require('sanitize-html');
44
var htmlWhitelistPost = require('./htmlWhitelistPost.json');
55
var renderer = new marked.Renderer();
6+
var blockRenderers = [
7+
'blockquote',
8+
'html',
9+
'list',
10+
'paragraph',
11+
'table'
12+
];
13+
14+
function sanitize (html) {
15+
return sanitizeHtml(html, htmlWhitelistPost);
16+
}
17+
18+
// Sanitize the output from the block level renderers
19+
blockRenderers.forEach(function (type) {
20+
renderer[type] = function () {
21+
return sanitize(marked.Renderer.prototype[type].apply(renderer, arguments));
22+
};
23+
});
624

725
// Automatically generate an anchor for each header
826
renderer.heading = function (text, level) {
@@ -12,18 +30,14 @@ renderer.heading = function (text, level) {
1230
var name = escapedText;
1331
var html = '<h' + level + '>';
1432
html += '<a name="' + name + '"></a>'
15-
html += text;
33+
html += sanitize(text);
1634
html += '<a href="#' + name + '" class="anchor">';
1735
html += '<i class="fa fa-link"></i>';
1836
html += '</a>';
1937
html += '</h' + level + '>';
2038
return html;
2139
};
2240

23-
renderer.html = renderer.paragraph = function (html) {
24-
return sanitizeHtml(html, htmlWhitelistPost);
25-
};
26-
2741
// Set the options to use for rendering markdown
2842
marked.setOptions({
2943
highlight: function (code, lang) {

0 commit comments

Comments
 (0)