var _utils = require('./utils'); var escape = _utils.escape; var unescape = _utils.unescape; /** * Renderer */ var defaultOptions = { langPrefix: 'lang-', smartypants: false, headerPrefix: '', headerAutoId: true, xhtml: false, }; function Renderer(options) { this.options = options || defaultOptions; } Renderer.prototype.code = function(code, lang, escaped) { if (this.options.highlight) { var out = this.options.highlight(code, lang); if (out != null && out !== code) { escaped = true; code = out; } } if (!lang) { return '
'
+ (escaped ? code : escape(code, true))
+ '\n
';
}
return ''
+ (escaped ? code : escape(code, true))
+ '\n
\n';
};
Renderer.prototype.blockquote = function(quote) {
return '\n' + quote + '\n'; }; Renderer.prototype.html = function(html) { return html; }; Renderer.prototype._createId = function(str) { // replace " " and all punctuation characters to "-" str = str.toLowerCase().replace(/[\s\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\`\{\|\}\~\-]+/g, '-'); try { str = encodeURIComponent(str); } catch (e) { str = str.replace(/[^\w]+/g, '-'); } return str.replace(/-$/, ''); }; Renderer.prototype.heading = function(text, level, raw) { var id = /({#)(.+)(})/g.exec(raw); id = id? id[2] : null; if (!id && this.options.headerAutoId !== false) id = this._createId(raw) return '
' + text + '
\n'; }; Renderer.prototype.table = function(header, body) { return '' + text + '
';
};
Renderer.prototype.br = function() {
return this.options.xhtml ? '\n' + '' + refname + '. ' + text + ' ↩\n' + '\n'; } Renderer.prototype.link = function(href, title, text) { if (this.options.sanitize) { try { var prot = decodeURIComponent(unescape(href)) .replace(/[^\w:]/g, '') .toLowerCase(); } catch (e) { return ''; } if (prot.indexOf('javascript:') === 0) { return ''; } } var out = '' + text + ''; return out; }; Renderer.prototype.image = function(href, title, text) { var out = '