UNPKG

3.46 kBJavaScriptView Raw
1const { defaults } = require('./defaults.js');
2const {
3 cleanUrl,
4 escape
5} = require('./helpers.js');
6
7/**
8 * Renderer
9 */
10module.exports = 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 blockquote(quote) {
42 return '<blockquote>\n' + quote + '</blockquote>\n';
43 }
44
45 html(html) {
46 return html;
47 }
48
49 heading(text, level, raw, slugger) {
50 if (this.options.headerIds) {
51 return '<h'
52 + level
53 + ' id="'
54 + this.options.headerPrefix
55 + slugger.slug(raw)
56 + '">'
57 + text
58 + '</h'
59 + level
60 + '>\n';
61 }
62 // ignore IDs
63 return '<h' + level + '>' + text + '</h' + level + '>\n';
64 }
65
66 hr() {
67 return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
68 }
69
70 list(body, ordered, start) {
71 const type = ordered ? 'ol' : 'ul',
72 startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
73 return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
74 }
75
76 listitem(text) {
77 return '<li>' + text + '</li>\n';
78 }
79
80 checkbox(checked) {
81 return '<input '
82 + (checked ? 'checked="" ' : '')
83 + 'disabled="" type="checkbox"'
84 + (this.options.xhtml ? ' /' : '')
85 + '> ';
86 }
87
88 paragraph(text) {
89 return '<p>' + text + '</p>\n';
90 }
91
92 table(header, body) {
93 if (body) body = '<tbody>' + body + '</tbody>';
94
95 return '<table>\n'
96 + '<thead>\n'
97 + header
98 + '</thead>\n'
99 + body
100 + '</table>\n';
101 }
102
103 tablerow(content) {
104 return '<tr>\n' + content + '</tr>\n';
105 }
106
107 tablecell(content, flags) {
108 const type = flags.header ? 'th' : 'td';
109 const tag = flags.align
110 ? '<' + type + ' align="' + flags.align + '">'
111 : '<' + type + '>';
112 return tag + content + '</' + type + '>\n';
113 }
114
115 // span level renderer
116 strong(text) {
117 return '<strong>' + text + '</strong>';
118 }
119
120 em(text) {
121 return '<em>' + text + '</em>';
122 }
123
124 codespan(text) {
125 return '<code>' + text + '</code>';
126 }
127
128 br() {
129 return this.options.xhtml ? '<br/>' : '<br>';
130 }
131
132 del(text) {
133 return '<del>' + text + '</del>';
134 }
135
136 link(href, title, text) {
137 href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
138 if (href === null) {
139 return text;
140 }
141 let out = '<a href="' + escape(href) + '"';
142 if (title) {
143 out += ' title="' + title + '"';
144 }
145 out += '>' + text + '</a>';
146 return out;
147 }
148
149 image(href, title, text) {
150 href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
151 if (href === null) {
152 return text;
153 }
154
155 let out = '<img src="' + href + '" alt="' + text + '"';
156 if (title) {
157 out += ' title="' + title + '"';
158 }
159 out += this.options.xhtml ? '/>' : '>';
160 return out;
161 }
162
163 text(text) {
164 return text;
165 }
166};