converter.hooks.chain("postConversion", function(html) {
    var tag_whitelist = /^(<\/?(h1|h2|h3|h4|h5|h6|p|ul|ol|li|dl|dt|dd|pre|blockquote|code|q|table|thead|tbody|tfoot|tr|th|td|em|strong|del|ins|sup|sub|s)>|<td\s+.*>|<(br|hr)\s?\/?>)$/i; // <td\s+.*> is for <td> with alignment in md extra syntax

    // <a href="url..." optional title>|</a>
    var a_white = /^(<a\shref="((https?|ftp):\/\/|\/)[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)*[\]$]+"(\stitle="[^"<>]+")?(\sclass="[^"<>]+")?(\starget="_blank")?\s?>|<\/a>)$/i;

    // <img src="url..." optional width  optional height  optional alt  optional title
    var img_white = /^(<img\ssrc="(https?:\/\/|\/)[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)*[\]$]+"(\swidth="\d{1,3}")?(\sheight="\d{1,3}")?(\salt="[^"<>]*")?(\stitle="[^"<>]*")?(\sclass="[^"<>]*")?\s?\/?>)$/i;

    var str = html.replace(/<[^>]*>?/gim, function(tag){
        var s;

        if (!tag.match(tag_whitelist) && !tag.match(a_white) && !tag.match(img_white)) {
           return '';
        }

        // rm attributes start with 'on'
        s = tag.replace(/on\w+\s*=.*(?=\s+|>)/gi, '');

        // rm attributes of which the values start with 'javascript:'
        s = s.replace(/\w+\s*=.*javascript\:.*(?=\s+|>)/gi, '');

        return s;
    });

    return str;
});
