/
index.js
338 lines (279 loc) · 7.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* marked-tex-renderer
* A plug-in style renderer to produce TeX output for marked
* https://github.com/sampathsris/marked-tex-renderer
*/
/*jshint node: true */
'use strict';
var NEWLINE = '\r\n';
function Renderer() {
}
Renderer.prototype.failOnUnsupported = function() {
if (!this.options.hasOwnProperty('failOnUnsupported')) {
return true;
}
return this.options.failOnUnsupported;
};
Renderer.prototype.code = function (code, lang, escaped) {
return [
'\\begin{verbatim}',
code,
'\\end{verbatim}'
].join(NEWLINE) + NEWLINE;
};
Renderer.prototype.blockquote = function (quote) {
return [
'\\begin{quote}',
quote,
'\\end{quote}'
].join(NEWLINE) + NEWLINE;
};
Renderer.prototype.html = function (html) {
return html;
};
Renderer.prototype.heading = function (text, level, raw) {
var command = '';
switch (level) {
case 1:
command = '\\chapter';
break;
case 2:
command = '\\section';
break;
case 3:
command = '\\subsection';
break;
case 4:
command = '\\subsubsection';
break;
case 5:
command = '\\paragraph';
break;
case 6:
command = '\\subparagraph';
break;
}
if (command !== '' && text.indexOf('\\{-\\}') !== -1) {
command += '*';
text = text.replace(' \\{-\\}', '').replace('\\{-\\}', '');
}
return NEWLINE + command + '{' + text + '}' + NEWLINE;
};
Renderer.prototype.hr = function () {
return '\\hrulefill' + NEWLINE;
};
Renderer.prototype.list = function (body, ordered) {
if (ordered) {
return [
NEWLINE,
'\\begin{enumerate}',
body,
'\\end{enumerate}',
NEWLINE
].join(NEWLINE);
} else {
return [
NEWLINE,
'\\begin{itemize}',
body,
'\\end{itemize}',
NEWLINE
].join(NEWLINE);
}
};
Renderer.prototype.listitem = function (text) {
return '\\item ' + text + NEWLINE;
};
Renderer.prototype.paragraph = function (text) {
return '\\par ' + text;
};
Renderer.prototype.tablecell = function (content, flags) {
// treat the cell as an element of a JSON array, and add a comma
// to separate it from the subsequent cells
return JSON.stringify({ content: content, flags: flags}) + ',';
};
Renderer.prototype.tablerow = function (content) {
// remove trailing comma from the list of cells
var row = content.substr(0, content.length - 1);
// and return it as a JSON array. add a comma to separate the
// row from the subsequent rows
return '[' + row + '],';
};
Renderer.prototype.table = function (header, body) {
var headerArr = [],
bodyArr = [],
hasHeader = false,
firstRow,
tex,
tableSpec;
// remove the trailing comma from header row
if (header) {
header = header.substr(0, header.length - 1);
headerArr = JSON.parse(header);
if (headerArr.length !== 0) {
hasHeader = true;
}
}
// remove the trailing comma from body row(s)
if (body) {
body = body.substr(0, body.length - 1);
bodyArr = JSON.parse('[' + body + ']');
}
if (headerArr.length !== 0) {
firstRow = headerArr;
} else {
firstRow = bodyArr[0];
}
tex = '\\begin{tabular}';
// create table spec
tableSpec = '{|';
for (var i = 0; i < firstRow.length; i++) {
var alignFlag = firstRow[i].flags.align || 'none';
var align = 'l|';
switch (alignFlag) {
case 'right':
align = 'r|';
break;
case 'center':
align = 'c|';
break;
}
tableSpec += align;
}
tableSpec += '}';
tex += tableSpec + NEWLINE;
// create table body
tex += '\\hline' + NEWLINE;
if (hasHeader) {
tex += createTableRow(headerArr);
tex += '\\hline' + NEWLINE;
}
for (var j = 0; j < bodyArr.length; j++) {
tex += createTableRow(bodyArr[j]);
}
tex += '\\hline' + NEWLINE;
tex += '\\end{tabular}' + NEWLINE;
return tex;
};
Renderer.prototype.strong = function (text) {
return '\\textbf{' + text + '}';
};
Renderer.prototype.em = function (text) {
return '\\emph{' + text + '}';
};
Renderer.prototype.codespan = function (text) {
return '\\texttt{' + this.text(text) + '}';
};
Renderer.prototype.br = function () {
return '\\\\';
};
Renderer.prototype.del = function (text) {
if (this.options.delRenderer) {
return this.options.delRenderer(text);
} else if (this.failOnUnsupported()) {
throw new Error(
'Client should provide a function to render deleted texts. ' +
'Use options.delRenderer = function (text)');
} else {
// deleted text is meant to be deleted. return ''.
return '';
}
};
Renderer.prototype.link = function (href, title, text) {
if (this.options.linkRenderer) {
return this.options.linkRenderer(href, title, text);
} else if (this.failOnUnsupported()) {
throw new Error(
'Client should provide a function to render hyperlinks. ' +
'Use options.linkRenderer = function (href, title, text)');
} else {
// omit hyperlink and just return the text.
return text;
}
};
Renderer.prototype.image = function (href, title, text) {
if (this.options.imageRenderer) {
return this.options.imageRenderer(href, title, text);
} else if (this.failOnUnsupported()) {
throw new Error(
'Client should provide a function to render images. ' +
'Use options.imageRenderer = function (href, title, text)');
} else {
// some text without an image would be weird. return ''.
return '';
}
};
Renderer.prototype.text = function (text) {
return texEscape(htmlUnescape(text));
};
/*
* Implementation for unsupported features of plain TeX
*/
Renderer.delImpl = function (text) {
// requires \usepackage{ulem}
// use \normalem to retain normal emphasis
return '\\sout{' + text + '}';
};
Renderer.linkImpl = function (href, title, text) {
// requires \usepackage{hyperref}
return '\\href{' + href + '}{' + text + '}';
};
Renderer.imageImpl = function (herf, title, text) {
// requires \usepackage{graphicx}
return [
NEWLINE,
'\\begin{figure}[h!]',
'\\caption{' + text + '}',
'\\centering',
'\\includegraphics{' + herf + '}',
'\\end{figure}'
].join(NEWLINE) + NEWLINE;
};
/*
* Helpers
*/
function createTableRow(rowArr) {
var tex = '';
for (var c = 0; c < rowArr.length; c++) {
tex += rowArr[c].content;
if (c < rowArr.length - 1) {
tex += ' & ';
} else {
tex += ' \\\\' + NEWLINE;
}
}
return tex;
}
function htmlUnescape(html) {
return html.replace(/&([#\w]+);/g, function(_, n) {
n = n.toLowerCase();
if (n === 'colon') return ':';
if (n === 'amp') return '&';
if (n.charAt(0) === '#') {
var charCode = 0;
if (n.charAt(1) === 'x') {
charCode = parseInt(n.substring(2), 16);
} else {
charCode = +n.substring(1);
}
return String.fromCharCode(charCode);
}
return '';
});
}
function texEscape(text) {
// some characters have special meaning in TeX
// \ & % $ # _ { } ~ ^
return text
.replace(/\\/g, '\\textbackslash')
.replace(/\&/g, '\\&')
.replace(/%/g, '\\%')
.replace(/\$/g, '\\$')
.replace(/#/g, '\\#')
.replace(/\_/g, '\\_')
.replace(/\{/g, '\\{')
.replace(/\}/g, '\\}')
.replace(/~/g, '\\textasciitilde')
.replace(/\^/g, '\\textasciicircum');
}
module.exports = Renderer;