/**
* @license
*
* Copyright (c) 2011-2014, Christopher Jeffrey. (MIT Licensed)
* https://github.com/chjj/marked
*
* Copyright (c) 2018, Костя Третяк. (MIT Licensed)
* https://github.com/ts-stack/markdown
*/
import type { Align, MarkedOptions } from "./interfaces.ts";
import { Marked } from "./marked.ts";
export class Renderer {
protected options: MarkedOptions;
constructor(options?: MarkedOptions) {
this.options = options || Marked.options;
}
code(code: string, lang?: string, escaped?: boolean): string {
if (this.options.highlight) {
const out = this.options.highlight(code, lang);
if (out != null && out !== code) {
escaped = true;
code = out;
}
}
if (!this.options.escape) throw ReferenceError;
if (!lang) {
return (
"\n
" +
(escaped ? code : this.options.escape(code, true)) +
"\n
\n"
);
}
return (
'\n' +
(escaped ? code : this.options.escape(code, true)) +
"\n
\n"
);
}
blockquote(quote: string): string {
return "\n" + quote + "
\n";
}
html(html: string): string {
return html;
}
heading(text: string, level: number, raw: string): string {
const id: string =
this.options.headerPrefix + raw.toLowerCase().replace(/[^\w]+/g, "-");
return `${text}\n`;
}
hr(): string {
return this.options.xhtml ? "
\n" : "
\n";
}
list(body: string, ordered?: boolean): string {
const type = ordered ? "ol" : "ul";
return `\n<${type}>\n${body}${type}>\n`;
}
listitem(text: string): string {
return "" + text + "\n";
}
paragraph(text: string): string {
return "" + text + "
\n";
}
table(header: string, body: string): string {
return `
`;
}
tablerow(content: string): string {
return "\n" + content + "
\n";
}
tablecell(
content: string,
flags: { header?: boolean; align?: Align }
): string {
const type = flags.header ? "th" : "td";
const tag = flags.align
? "<" + type + ' style="text-align:' + flags.align + '">'
: "<" + type + ">";
return tag + content + "" + type + ">\n";
}
// *** Inline level renderer methods. ***
strong(text: string): string {
return "" + text + "";
}
em(text: string): string {
return "" + text + "";
}
codespan(text: string): string {
return "" + text + "
";
}
br(): string {
return this.options.xhtml ? "
" : "
";
}
del(text: string): string {
return "" + text + "";
}
link(href: string, title: string, text: string): string {
if (this.options.sanitize) {
let prot: string;
if (!this.options.unescape) throw ReferenceError;
try {
prot = decodeURIComponent(this.options.unescape(href))
.replace(/[^\w:]/g, "")
.toLowerCase();
} catch (e) {
return text;
}
if (
prot.indexOf("javascript:") === 0 ||
prot.indexOf("vbscript:") === 0 ||
prot.indexOf("data:") === 0
) {
return text;
}
}
let out = '" + text + "";
return out;
}
image(href: string, title: string, text: string): string {
let out = '
" : ">";
return out;
}
text(text: string): string {
return text;
}
}