import { defaults } from './defaults.js';
import {
cleanUrl,
escape
} from './helpers.js';
/**
* Renderer
*/
export class Renderer {
constructor(options) {
this.options = options || defaults;
}
code(code, infostring, escaped) {
const lang = (infostring || '').match(/\S*/)[0];
if (this.options.highlight) {
const out = this.options.highlight(code, lang);
if (out != null && out !== code) {
escaped = true;
code = out;
}
}
code = code.replace(/\n$/, '') + '\n';
if (!lang) {
return '
'
+ (escaped ? code : escape(code, true))
+ '
\n';
}
return ''
+ (escaped ? code : escape(code, true))
+ '
\n';
}
blockquote(quote) {
return '\n' + quote + '
\n';
}
html(html) {
return html;
}
heading(text, level, raw, slugger) {
if (this.options.headerIds) {
return '\n';
}
// ignore IDs
return '' + text + '\n';
}
hr() {
return this.options.xhtml ? '
\n' : '
\n';
}
list(body, ordered, start) {
const type = ordered ? 'ol' : 'ul',
startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
return '<' + type + startatt + '>\n' + body + '' + type + '>\n';
}
listitem(text) {
return '' + text + '\n';
}
checkbox(checked) {
return ' ';
}
paragraph(text) {
return '' + text + '
\n';
}
table(header, body) {
if (body) body = '' + body + '';
return '\n'
+ '\n'
+ header
+ '\n'
+ body
+ '
\n';
}
tablerow(content) {
return '\n' + content + '
\n';
}
tablecell(content, flags) {
const type = flags.header ? 'th' : 'td';
const tag = flags.align
? '<' + type + ' align="' + flags.align + '">'
: '<' + type + '>';
return tag + content + '' + type + '>\n';
}
// span level renderer
strong(text) {
return '' + text + '';
}
em(text) {
return '' + text + '';
}
codespan(text) {
return '' + text + '
';
}
br() {
return this.options.xhtml ? '
' : '
';
}
del(text) {
return '' + text + '';
}
link(href, title, text) {
href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
if (href === null) {
return text;
}
let out = '' + text + '';
return out;
}
image(href, title, text) {
href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
if (href === null) {
return text;
}
let out = '
' : '>';
return out;
}
text(text) {
return text;
}
}