-
Notifications
You must be signed in to change notification settings - Fork 8
/
fenced_code.js
207 lines (180 loc) · 6.23 KB
/
fenced_code.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
// prettier-disable
/* eslint-disable */
// prettier-ignore
var forEach = require('lodash.foreach');
var contains = require('lodash.contains');
var map = require('lodash.map');
var reject = require('lodash.reject');
var util = require('./util');
var fenced_code = (function () {
var katex = require('katex');
var exports = {};
// Parsing routine that can be dropped in to message parsing
// and formats code blocks
//
// This supports arbitrarily nested code blocks as well as
// auto-completing code blocks missing a trailing close.
// See backend fenced_code.py:71 for associated regexp
var fencestr = "^(~{3,}|`{3,})" + // Opening Fence
"[ ]*" + // Spaces
"(" +
"\\{?\\.?" +
"([a-zA-Z0-9_+-./#]*)" + // Language
"\\}?" +
"[ ]*" + // Spaces
")$";
var fence_re = new RegExp(fencestr);
// Default stashing function does nothing
var stash_func = function (text) {
return text;
};
var escape_func = function (text) {
return text;
};
function wrap_code(code) {
// Trim trailing \n until there's just one left
// This mirrors how pygments handles code input
code += '\n';
while (code.length > 2 && code.substr(code.length - 2) === '\n\n') {
code = code.substring(0, code.length - 1);
}
return '<div class="codehilite"><pre><span></span>' + escape_func(code) + '</pre></div>\n';
}
function wrap_quote(text) {
var paragraphs = text.split('\n\n');
var quoted_paragraphs = [];
// Prefix each quoted paragraph with > at the
// beginning of each line
forEach(paragraphs, function (paragraph) {
var lines = paragraph.split('\n');
quoted_paragraphs.push(map(
reject(lines, function (line) { return line === ''; }),
function (line) { return '> ' + line; }).join('\n'));
});
return quoted_paragraphs.join('\n\n');
}
function wrap_tex(tex) {
try {
return katex.renderToString(tex, {
displayMode: true,
});
} catch (ex) {
return '<span class="tex-error">' + escape_func(tex) + '</span>';
}
}
exports.set_stash_func = function (stash_handler) {
stash_func = stash_handler;
};
exports.set_escape_func = function (escape) {
escape_func = escape;
};
exports.process_fenced_code = function (content) {
var input = content.split('\n');
var output = [];
var handler_stack = [];
var consume_line;
function handler_for_fence(output_lines, fence, lang) {
// lang is ignored except for 'quote', as we
// don't do syntax highlighting yet
return (function () {
var lines = [];
if (lang === 'quote') {
return {
handle_line: function (line) {
if (line === fence) {
this.done();
} else {
consume_line(lines, line);
}
},
done: function () {
var text = wrap_quote(lines.join('\n'));
output_lines.push('');
output_lines.push(text);
output_lines.push('');
handler_stack.pop();
},
};
}
if (lang === 'math' || lang === 'tex' || lang === 'latex') {
return {
handle_line: function (line) {
if (line === fence) {
this.done();
} else {
consume_line(lines, line);
}
},
done: function () {
var text = wrap_tex(lines.join('\n'));
var placeholder = stash_func(text, true);
output_lines.push('');
output_lines.push(placeholder);
output_lines.push('');
handler_stack.pop();
},
};
}
return {
handle_line: function (line) {
if (line === fence) {
this.done();
} else {
lines.push(util.rtrim(line));
}
},
done: function () {
var text = wrap_code(lines.join('\n'));
// insert safe HTML that is passed through the parsing
var placeholder = stash_func(text, true);
output_lines.push('');
output_lines.push(placeholder);
output_lines.push('');
handler_stack.pop();
},
};
}());
}
function default_hander() {
return {
handle_line: function (line) {
consume_line(output, line);
},
done: function () {
handler_stack.pop();
},
};
}
consume_line = function consume_line(output_lines, line) {
var match = fence_re.exec(line);
if (match) {
var fence = match[1];
var lang = match[3];
var handler = handler_for_fence(output_lines, fence, lang);
handler_stack.push(handler);
} else {
output_lines.push(line);
}
};
var current_handler = default_hander();
handler_stack.push(current_handler);
forEach(input, function (line) {
var handler = handler_stack[handler_stack.length - 1];
handler.handle_line(line);
});
// Clean up all trailing blocks by letting them
// insert closing fences
while (handler_stack.length !== 0) {
var handler = handler_stack[handler_stack.length - 1];
handler.done();
}
if (output.length > 2 && output[output.length - 2] !== '') {
output.push('');
}
return output.join('\n');
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = fenced_code;
}