-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdiscourse-math.js.es6
233 lines (195 loc) · 6.46 KB
/
discourse-math.js.es6
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
// inspired by https://github.com/classeur/markdown-it-mathjax/blob/master/markdown-it-mathjax.js
//
//
//
function isSafeBoundary(character, delimiter) {
let characterCode = character.charCodeAt(0);
// 0-9
if (characterCode > 47 && characterCode < 58) {
return false;
}
// Need to distinguish $ from $$
if (delimiter.length == 1 && delimiter.charCodeAt(0) === characterCode) {
return false;
}
return true;
}
let inlineMath = (startDelimiter, endDelimiter) => (state, silent) => {
let pos = state.pos,
posMax = state.posMax;
if (
silent ||
!state.src.startsWith(startDelimiter, pos) ||
posMax < pos + startDelimiter.length + endDelimiter.length
) {
return false;
}
// too short
if (state.src.startsWith(endDelimiter, pos + startDelimiter.length)) {
return false;
}
if (pos > 0) {
let prev = state.src[pos - 1];
if (!isSafeBoundary(prev, startDelimiter)) {
return false;
}
}
let found;
if (endDelimiter.length === 1) {
// Faster iterations, comparing numbers instead of characters
// and respecting character escaping with `\`
let endDelimCode = endDelimiter.charCodeAt(0);
for (let i = pos + 1; i < posMax; i++) {
let code = state.src.charCodeAt(i);
if (code === endDelimCode && state.src.charCodeAt(i - 1) !== 92 /* \ */) {
found = i;
break;
}
}
} else {
for (let i = pos + 1; i <= posMax - endDelimiter.length; i++) {
// we do not respect escaping here because we need to allow for
// \(...\) TeX inline delimiters
if (state.src.startsWith(endDelimiter, i)) {
found = i;
break;
}
}
}
if (!found) {
return false;
}
if (found + endDelimiter.length <= posMax) {
let next = state.src[found + endDelimiter.length];
if (next && !isSafeBoundary(next, endDelimiter)) {
return false;
}
}
let data = state.src.slice(pos + startDelimiter.length, found);
let token = state.push("html_raw", "", 0);
const escaped = state.md.utils.escapeHtml(data);
let math_class = startDelimiter === endDelimiter === '%' ? "'asciimath'" : "'math'";
token.content = `<span class=${math_class}>${escaped}</span>`;
state.pos = found + endDelimiter.length;
return true;
}
function isBlockMarker(state, start, max, md, blockMarker) {
if (!state.src.startsWith(blockMarker, start)) {
return false;
}
// ensure we only have spaces and newlines after block math marker
for (let i = start + blockMarker.length; i < max; i++) {
if (!md.utils.isSpace(state.src.charCodeAt(i))) {
return false;
}
}
return true;
}
let blockMath = (startBlockMathMarker, endBlockMathMarker) => (state, startLine, endLine, silent) => {
let start = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
let startBlockMarker = startBlockMathMarker;
let endBlockMarker = endBlockMathMarker;
// Special processing for /\begin{([a-z]+)}/
if (startBlockMarker instanceof RegExp) {
let substr = state.src.substring(start, max);
let match = substr.match(startBlockMarker);
if(!match) {
return false;
}
let mathEnv = match[1];
startBlockMarker = `\\begin{${mathEnv}}`;
endBlockMarker = `\\end{${mathEnv}}`;
}
if (!isBlockMarker(state, start, max, state.md, startBlockMarker)) {
return false;
}
if (silent) {
return true;
}
let nextLine = startLine;
let closed = false;
for (;;) {
nextLine++;
// Unclosed blockmarker is considered math
if (nextLine >= endLine) {
break;
}
if (
isBlockMarker(
state,
state.bMarks[nextLine] + state.tShift[nextLine],
state.eMarks[nextLine],
state.md,
endBlockMarker
)
) {
closed = true;
break;
}
}
let token = state.push("html_raw", "", 0);
// Math environment blockmarkers '\begin{}' and '\end{}'
// needs to be passed to the TeX engine
let endContent = endBlockMarker.startsWith('\\end{') || !closed ?
state.eMarks[nextLine] : state.eMarks[nextLine - 1];
let startContent = startBlockMarker.startsWith('\\begin{') ?
state.bMarks[startLine] : state.bMarks[startLine + 1] + state.tShift[startLine + 1];
let content = state.src.slice(startContent, endContent);
const escaped = state.md.utils.escapeHtml(content);
token.content = `<div class='math'>\n${escaped}\n</div>\n`;
state.line = closed ? nextLine + 1 : nextLine;
return true;
}
export function setup(helper) {
if (!helper.markdownIt) {
return;
}
let enableAsciiMath, enableMathEnvs;
let inlineDelimiters, blockDelimiters;
let texRenderer;
helper.registerOptions((opts, siteSettings) => {
opts.features.math = siteSettings.discourse_math_enabled;
enableAsciiMath = siteSettings.discourse_math_enable_asciimath;
enableMathEnvs = siteSettings.discourse_math_process_tex_environments;
inlineDelimiters = siteSettings.discourse_math_inline_delimiters;
blockDelimiters = siteSettings.discourse_math_block_delimiters;
texRenderer = siteSettings.discourse_math_provider;
});
helper.registerPlugin(md => {
let mathjax = texRenderer === 'mathjax';
if (enableAsciiMath && mathjax) {
md.inline.ruler.after("escape", "asciimath", inlineMath('%', '%'));
}
if (enableMathEnvs && mathjax) {
md.block.ruler.after("code", "math",
blockMath(/\\begin\{([a-z]+)\}/, /\\end\{([a-z]+)\}/), {
alt: ["paragraph", "reference", "blockquote", "list"]
});
}
// Helper function for checking input
const isEmptyStr = elem => elem.trim() === '';
inlineDelimiters.split('|').forEach(d => {
let delims = d.split(',');
if (delims.length !== 2 || delims.some(isEmptyStr)) {
console.error('Invalid input in discourse_math_inline_delimiters!');
return;
}
let startDelim = delims[0].trim();
let endDelim = delims[1].trim();
md.inline.ruler.before("escape", "math", inlineMath(startDelim, endDelim));
});
blockDelimiters.split('|').forEach(d => {
let delims = d.split(',');
if (delims.length !== 2 || delims.some(isEmptyStr)) {
console.error('Invalid input in discourse_math_block_delimiters!');
return;
}
let startDelim = delims[0].trim();
let endDelim = delims[1].trim();
md.block.ruler.after("code", "math", blockMath(startDelim, endDelim), {
alt: ["paragraph", "reference", "blockquote", "list"]
});
});
});
}