|
@@ -11,18 +11,18 @@ import type { _Parser } from './Parser.ts';
|
|
|
/**
|
|
|
* Renderer
|
|
|
*/
|
|
|
-export class _Renderer {
|
|
|
- options: MarkedOptions;
|
|
|
- parser!: _Parser; // set by the parser
|
|
|
- constructor(options?: MarkedOptions) {
|
|
|
+export class _Renderer<ParserOutput = string, RendererOutput = string> {
|
|
|
+ options: MarkedOptions<ParserOutput, RendererOutput>;
|
|
|
+ parser!: _Parser<ParserOutput, RendererOutput>; // set by the parser
|
|
|
+ constructor(options?: MarkedOptions<ParserOutput, RendererOutput>) {
|
|
|
this.options = options || _defaults;
|
|
|
}
|
|
|
|
|
|
- space(token: Tokens.Space): string {
|
|
|
- return '';
|
|
|
+ space(token: Tokens.Space): RendererOutput {
|
|
|
+ return '' as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- code({ text, lang, escaped }: Tokens.Code): string {
|
|
|
+ code({ text, lang, escaped }: Tokens.Code): RendererOutput {
|
|
|
const langString = (lang || '').match(other.notSpaceStart)?.[0];
|
|
|
|
|
|
const code = text.replace(other.endingNewline, '') + '\n';
|
|
@@ -30,34 +30,34 @@ export class _Renderer {
|
|
|
if (!langString) {
|
|
|
return '<pre><code>'
|
|
|
+ (escaped ? code : escape(code, true))
|
|
|
- + '</code></pre>\n';
|
|
|
+ + '</code></pre>\n' as RendererOutput;
|
|
|
}
|
|
|
|
|
|
return '<pre><code class="language-'
|
|
|
+ escape(langString)
|
|
|
+ '">'
|
|
|
+ (escaped ? code : escape(code, true))
|
|
|
- + '</code></pre>\n';
|
|
|
+ + '</code></pre>\n' as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- blockquote({ tokens }: Tokens.Blockquote): string {
|
|
|
+ blockquote({ tokens }: Tokens.Blockquote): RendererOutput {
|
|
|
const body = this.parser.parse(tokens);
|
|
|
- return `<blockquote>\n${body}</blockquote>\n`;
|
|
|
+ return `<blockquote>\n${body}</blockquote>\n` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- html({ text }: Tokens.HTML | Tokens.Tag) : string {
|
|
|
- return text;
|
|
|
+ html({ text }: Tokens.HTML | Tokens.Tag): RendererOutput {
|
|
|
+ return text as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- heading({ tokens, depth }: Tokens.Heading): string {
|
|
|
- return `<h${depth}>${this.parser.parseInline(tokens)}</h${depth}>\n`;
|
|
|
+ heading({ tokens, depth }: Tokens.Heading): RendererOutput {
|
|
|
+ return `<h${depth}>${this.parser.parseInline(tokens)}</h${depth}>\n` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- hr(token: Tokens.Hr): string {
|
|
|
- return '<hr>\n';
|
|
|
+ hr(token: Tokens.Hr): RendererOutput {
|
|
|
+ return '<hr>\n' as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- list(token: Tokens.List): string {
|
|
|
+ list(token: Tokens.List): RendererOutput {
|
|
|
const ordered = token.ordered;
|
|
|
const start = token.start;
|
|
|
|
|
@@ -69,10 +69,10 @@ export class _Renderer {
|
|
|
|
|
|
const type = ordered ? 'ol' : 'ul';
|
|
|
const startAttr = (ordered && start !== 1) ? (' start="' + start + '"') : '';
|
|
|
- return '<' + type + startAttr + '>\n' + body + '</' + type + '>\n';
|
|
|
+ return '<' + type + startAttr + '>\n' + body + '</' + type + '>\n' as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- listitem(item: Tokens.ListItem): string {
|
|
|
+ listitem(item: Tokens.ListItem): RendererOutput {
|
|
|
let itemBody = '';
|
|
|
if (item.task) {
|
|
|
const checkbox = this.checkbox({ checked: !!item.checked });
|
|
@@ -98,20 +98,20 @@ export class _Renderer {
|
|
|
|
|
|
itemBody += this.parser.parse(item.tokens, !!item.loose);
|
|
|
|
|
|
- return `<li>${itemBody}</li>\n`;
|
|
|
+ return `<li>${itemBody}</li>\n` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- checkbox({ checked }: Tokens.Checkbox): string {
|
|
|
+ checkbox({ checked }: Tokens.Checkbox): RendererOutput {
|
|
|
return '<input '
|
|
|
+ (checked ? 'checked="" ' : '')
|
|
|
- + 'disabled="" type="checkbox">';
|
|
|
+ + 'disabled="" type="checkbox">' as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- paragraph({ tokens }: Tokens.Paragraph): string {
|
|
|
- return `<p>${this.parser.parseInline(tokens)}</p>\n`;
|
|
|
+ paragraph({ tokens }: Tokens.Paragraph): RendererOutput {
|
|
|
+ return `<p>${this.parser.parseInline(tokens)}</p>\n` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- table(token: Tokens.Table): string {
|
|
|
+ table(token: Tokens.Table): RendererOutput {
|
|
|
let header = '';
|
|
|
|
|
|
// header
|
|
@@ -119,7 +119,7 @@ export class _Renderer {
|
|
|
for (let j = 0; j < token.header.length; j++) {
|
|
|
cell += this.tablecell(token.header[j]);
|
|
|
}
|
|
|
- header += this.tablerow({ text: cell });
|
|
|
+ header += this.tablerow({ text: cell as ParserOutput });
|
|
|
|
|
|
let body = '';
|
|
|
for (let j = 0; j < token.rows.length; j++) {
|
|
@@ -130,7 +130,7 @@ export class _Renderer {
|
|
|
cell += this.tablecell(row[k]);
|
|
|
}
|
|
|
|
|
|
- body += this.tablerow({ text: cell });
|
|
|
+ body += this.tablerow({ text: cell as ParserOutput });
|
|
|
}
|
|
|
if (body) body = `<tbody>${body}</tbody>`;
|
|
|
|
|
@@ -139,50 +139,50 @@ export class _Renderer {
|
|
|
+ header
|
|
|
+ '</thead>\n'
|
|
|
+ body
|
|
|
- + '</table>\n';
|
|
|
+ + '</table>\n' as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- tablerow({ text }: Tokens.TableRow): string {
|
|
|
- return `<tr>\n${text}</tr>\n`;
|
|
|
+ tablerow({ text }: Tokens.TableRow<ParserOutput>): RendererOutput {
|
|
|
+ return `<tr>\n${text}</tr>\n` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- tablecell(token: Tokens.TableCell): string {
|
|
|
+ tablecell(token: Tokens.TableCell): RendererOutput {
|
|
|
const content = this.parser.parseInline(token.tokens);
|
|
|
const type = token.header ? 'th' : 'td';
|
|
|
const tag = token.align
|
|
|
? `<${type} align="${token.align}">`
|
|
|
: `<${type}>`;
|
|
|
- return tag + content + `</${type}>\n`;
|
|
|
+ return tag + content + `</${type}>\n` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* span level renderer
|
|
|
*/
|
|
|
- strong({ tokens }: Tokens.Strong): string {
|
|
|
- return `<strong>${this.parser.parseInline(tokens)}</strong>`;
|
|
|
+ strong({ tokens }: Tokens.Strong): RendererOutput {
|
|
|
+ return `<strong>${this.parser.parseInline(tokens)}</strong>` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- em({ tokens }: Tokens.Em): string {
|
|
|
- return `<em>${this.parser.parseInline(tokens)}</em>`;
|
|
|
+ em({ tokens }: Tokens.Em): RendererOutput {
|
|
|
+ return `<em>${this.parser.parseInline(tokens)}</em>` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- codespan({ text }: Tokens.Codespan): string {
|
|
|
- return `<code>${escape(text, true)}</code>`;
|
|
|
+ codespan({ text }: Tokens.Codespan): RendererOutput {
|
|
|
+ return `<code>${escape(text, true)}</code>` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- br(token: Tokens.Br): string {
|
|
|
- return '<br>';
|
|
|
+ br(token: Tokens.Br): RendererOutput {
|
|
|
+ return '<br>' as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- del({ tokens }: Tokens.Del): string {
|
|
|
- return `<del>${this.parser.parseInline(tokens)}</del>`;
|
|
|
+ del({ tokens }: Tokens.Del): RendererOutput {
|
|
|
+ return `<del>${this.parser.parseInline(tokens)}</del>` as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- link({ href, title, tokens }: Tokens.Link): string {
|
|
|
- const text = this.parser.parseInline(tokens);
|
|
|
+ link({ href, title, tokens }: Tokens.Link): RendererOutput {
|
|
|
+ const text = this.parser.parseInline(tokens) as string;
|
|
|
const cleanHref = cleanUrl(href);
|
|
|
if (cleanHref === null) {
|
|
|
- return text;
|
|
|
+ return text as RendererOutput;
|
|
|
}
|
|
|
href = cleanHref;
|
|
|
let out = '<a href="' + href + '"';
|
|
@@ -190,16 +190,16 @@ export class _Renderer {
|
|
|
out += ' title="' + (escape(title)) + '"';
|
|
|
}
|
|
|
out += '>' + text + '</a>';
|
|
|
- return out;
|
|
|
+ return out as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- image({ href, title, text, tokens }: Tokens.Image): string {
|
|
|
+ image({ href, title, text, tokens }: Tokens.Image): RendererOutput {
|
|
|
if (tokens) {
|
|
|
- text = this.parser.parseInline(tokens, this.parser.textRenderer);
|
|
|
+ text = this.parser.parseInline(tokens, this.parser.textRenderer) as string;
|
|
|
}
|
|
|
const cleanHref = cleanUrl(href);
|
|
|
if (cleanHref === null) {
|
|
|
- return escape(text);
|
|
|
+ return escape(text) as RendererOutput;
|
|
|
}
|
|
|
href = cleanHref;
|
|
|
|
|
@@ -208,12 +208,12 @@ export class _Renderer {
|
|
|
out += ` title="${escape(title)}"`;
|
|
|
}
|
|
|
out += '>';
|
|
|
- return out;
|
|
|
+ return out as RendererOutput;
|
|
|
}
|
|
|
|
|
|
- text(token: Tokens.Text | Tokens.Escape) : string {
|
|
|
+ text(token: Tokens.Text | Tokens.Escape): RendererOutput {
|
|
|
return 'tokens' in token && token.tokens
|
|
|
- ? this.parser.parseInline(token.tokens)
|
|
|
- : ('escaped' in token && token.escaped ? token.text : escape(token.text));
|
|
|
+ ? this.parser.parseInline(token.tokens) as unknown as RendererOutput
|
|
|
+ : ('escaped' in token && token.escaped ? token.text as RendererOutput : escape(token.text) as RendererOutput);
|
|
|
}
|
|
|
}
|