UNPKG

3.94 kBJavaScriptView Raw
1import { defaults } from './defaults.js';
2import {
3 cleanUrl,
4 escape
5} from './helpers.js';
6
7/**
8 * Renderer
9 */
10export class Renderer {
11 constructor(options) {
12 this.options = options || defaults;
13 }
14
15 code(code, infostring, escaped) {
16 const lang = (infostring || '').match(/\S*/)[0];
17 if (this.options.highlight) {
18 const out = this.options.highlight(code, lang);
19 if (out != null && out !== code) {
20 escaped = true;
21 code = out;
22 }
23 }
24
25 code = code.replace(/\n$/, '') + '\n';
26
27 if (!lang) {
28 return '<pre><code>'
29 + (escaped ? code : escape(code, true))
30 + '</code></pre>\n';
31 }
32
33 return '<pre><code class="'
34 + this.options.langPrefix
35 + escape(lang, true)
36 + '">'
37 + (escaped ? code : escape(code, true))
38 + '</code></pre>\n';
39 }
40
41 /**
42 * @param {string} quote
43 */
44 blockquote(quote) {
45 return `<blockquote>\n${quote}</blockquote>\n`;
46 }
47
48 html(html) {
49 return html;
50 }
51
52 /**
53 * @param {string} text
54 * @param {string} level
55 * @param {string} raw
56 * @param {any} slugger
57 */
58 heading(text, level, raw, slugger) {
59 if (this.options.headerIds) {
60 const id = this.options.headerPrefix + slugger.slug(raw);
61 return `<h${level} id="${id}">${text}</h${level}>\n`;
62 }
63
64 // ignore IDs
65 return `<h${level}>${text}</h${level}>\n`;
66 }
67
68 hr() {
69 return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
70 }
71
72 list(body, ordered, start) {
73 const type = ordered ? 'ol' : 'ul',
74 startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
75 return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
76 }
77
78 /**
79 * @param {string} text
80 */
81 listitem(text) {
82 return `<li>${text}</li>\n`;
83 }
84
85 checkbox(checked) {
86 return '<input '
87 + (checked ? 'checked="" ' : '')
88 + 'disabled="" type="checkbox"'
89 + (this.options.xhtml ? ' /' : '')
90 + '> ';
91 }
92
93 /**
94 * @param {string} text
95 */
96 paragraph(text) {
97 return `<p>${text}</p>\n`;
98 }
99
100 /**
101 * @param {string} header
102 * @param {string} body
103 */
104 table(header, body) {
105 if (body) body = `<tbody>${body}</tbody>`;
106
107 return '<table>\n'
108 + '<thead>\n'
109 + header
110 + '</thead>\n'
111 + body
112 + '</table>\n';
113 }
114
115 /**
116 * @param {string} content
117 */
118 tablerow(content) {
119 return `<tr>\n${content}</tr>\n`;
120 }
121
122 tablecell(content, flags) {
123 const type = flags.header ? 'th' : 'td';
124 const tag = flags.align
125 ? `<${type} align="${flags.align}">`
126 : `<${type}>`;
127 return tag + content + `</${type}>\n`;
128 }
129
130 /**
131 * span level renderer
132 * @param {string} text
133 */
134 strong(text) {
135 return `<strong>${text}</strong>`;
136 }
137
138 /**
139 * @param {string} text
140 */
141 em(text) {
142 return `<em>${text}</em>`;
143 }
144
145 /**
146 * @param {string} text
147 */
148 codespan(text) {
149 return `<code>${text}</code>`;
150 }
151
152 br() {
153 return this.options.xhtml ? '<br/>' : '<br>';
154 }
155
156 /**
157 * @param {string} text
158 */
159 del(text) {
160 return `<del>${text}</del>`;
161 }
162
163 /**
164 * @param {string} href
165 * @param {string} title
166 * @param {string} text
167 */
168 link(href, title, text) {
169 href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
170 if (href === null) {
171 return text;
172 }
173 let out = '<a href="' + escape(href) + '"';
174 if (title) {
175 out += ' title="' + title + '"';
176 }
177 out += '>' + text + '</a>';
178 return out;
179 }
180
181 /**
182 * @param {string} href
183 * @param {string} title
184 * @param {string} text
185 */
186 image(href, title, text) {
187 href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
188 if (href === null) {
189 return text;
190 }
191
192 let out = `<img src="${href}" alt="${text}"`;
193 if (title) {
194 out += ` title="${title}"`;
195 }
196 out += this.options.xhtml ? '/>' : '>';
197 return out;
198 }
199
200 text(text) {
201 return text;
202 }
203}