/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* Marked.js library renderer with fixes:
* - no formatting for output HTML string - all newlines between tags are removed to create clean output,
* - changed long string concatenations to ES5 template strings,
* - changed code style.
*
* @param options
* @constructor
*/
function Renderer( options ) {
this.options = options || {};
}
Renderer.prototype.code = function( code, lang, escaped ) {
if ( this.options.highlight ) {
const out = this.options.highlight( code, lang );
if ( out !== null && out !== code ) {
escaped = true;
code = out;
}
}
if ( !lang ) {
return `
${ escaped ? code : escape( code, true ) }
`;
}
const cssClass = this.options.langPrefix + escape( lang, true );
return `${ escaped ? code : escape( code, true ) }
`;
};
Renderer.prototype.blockquote = function( quote ) {
return `${ quote }
`;
};
Renderer.prototype.html = function( html ) {
return html;
};
Renderer.prototype.heading = function( text, level, raw ) {
const id = this.options.headerPrefix + raw.toLowerCase().replace( /[^\w]+/g, '-' );
return `${ text }`;
};
Renderer.prototype.hr = function() {
return this.options.xhtml ? '
' : '
';
};
Renderer.prototype.list = function( body, ordered ) {
const type = ordered ? 'ol' : 'ul';
return `<${ type }>${ body }${ type }>`;
};
Renderer.prototype.listitem = function( text ) {
return `${ text }`;
};
Renderer.prototype.paragraph = function( text ) {
return `${ text }
`;
};
Renderer.prototype.table = function( header, body ) {
return ``;
};
Renderer.prototype.tablerow = function( content ) {
return '' + content + '
';
};
Renderer.prototype.tablecell = function( content, flags ) {
const type = flags.header ? 'th' : 'td';
const tag = flags.align ? `<${ type } align="${ flags.align }">` : `<${ type }>`;
return tag + content + `${ type }>`;
};
// span level renderer
Renderer.prototype.strong = function( text ) {
return `${ text }`;
};
Renderer.prototype.em = function( text ) {
return `${ text }`;
};
Renderer.prototype.codespan = function( text ) {
return `${ text.trim() }`;
};
Renderer.prototype.br = function() {
return this.options.xhtml ? '
' : '
';
};
Renderer.prototype.del = function( text ) {
return `${ text }`;
};
Renderer.prototype.link = function( href, title, text ) {
if ( this.options.sanitize ) {
let prot;
try {
prot = decodeURIComponent( unescape( href ) )
.replace( /[^\w:]/g, '' )
.toLowerCase();
} catch ( e ) {
return '';
}
if ( prot.indexOf( 'javascript:' ) === 0 || prot.indexOf( 'vbscript:' ) === 0 ) {
return '';
}
}
let out = '' + text + '';
return out;
};
Renderer.prototype.image = function( href, title, text ) {
let out = '
' : '>';
return out;
};
Renderer.prototype.text = function( text ) {
return text;
};
export default Renderer;
function escape( html, encode ) {
return html
.replace( !encode ? /&(?!#?\w+;)/g : /&/g, '&' )
.replace( //g, '>' )
.replace( /"/g, '"' )
.replace( /'/g, ''' );
}
function unescape( html ) {
// explicitly match decimal, hex, and named HTML entities
return html.replace( /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/g, function( _, n ) {
n = n.toLowerCase();
if ( n === 'colon' ) {
return ':';
}
if ( n.charAt( 0 ) === '#' ) {
return n.charAt( 1 ) === 'x' ?
String.fromCharCode( parseInt( n.substring( 2 ), 16 ) ) :
String.fromCharCode( +n.substring( 1 ) );
}
return '';
} );
}