fixed mathjax in code

This commit is contained in:
Klemek
2019-07-18 16:41:25 +02:00
parent e14f9fc4af
commit 2e8ff1be92
2 changed files with 22 additions and 13 deletions
+16 -13
View File
@@ -64,7 +64,9 @@ module.exports = (config) => {
if (!config['modules']['mathjax']) if (!config['modules']['mathjax'])
return cb(data); return cb(data);
const doMJ = (match, format) => { const spl = data.split('```');
const doMJ = (match, format, i) => {
const eq = match[1].trim(); const eq = match[1].trim();
const output = config['mathjax']['output_format']; const output = config['mathjax']['output_format'];
const mjConf = { const mjConf = {
@@ -74,8 +76,8 @@ module.exports = (config) => {
}; };
mjConf[output] = true; mjConf[output] = true;
mjAPI.typeset(mjConf, (res) => { mjAPI.typeset(mjConf, (res) => {
data = data.slice(0, match.index) + res[output] + data.slice(match.index + match[0].length); spl[i] = spl[i].slice(0, match.index) + res[output] + spl[i].slice(match.index + match[0].length);
renderMathJax(data, (data2) => { renderMathJax(spl.join('```'), (data2) => {
cb(data2); cb(data2);
}); });
}); });
@@ -84,14 +86,15 @@ module.exports = (config) => {
const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m; const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m;
const inlineEqRegex = /\$([^$\n]*)\$/; const inlineEqRegex = /\$([^$\n]*)\$/;
let match; for (let i = 0; i < spl.length; i += 2) {
if ((match = eqRegex.exec(data))) { let match;
doMJ(match, 'TeX'); if ((match = eqRegex.exec(spl[i]))) {
} else if ((match = inlineEqRegex.exec(data))) { return doMJ(match, 'TeX', i);
doMJ(match, 'inline-TeX'); } else if ((match = inlineEqRegex.exec(spl[i]))) {
} else { return doMJ(match, 'inline-TeX', i);
cb(data); }
} }
cb(data);
}; };
return { return {
@@ -104,9 +107,9 @@ module.exports = (config) => {
if (err) if (err)
return cb(err); return cb(err);
renderPrism(data, (data) => { renderPlantUML(data, (data) => {
renderPlantUML(data, (data) => { renderMathJax(data, (data) => {
renderMathJax(data, (data) => { renderPrism(data, (data) => {
renderShowDown(data, (html) => { renderShowDown(data, (html) => {
cb(null, html); cb(null, html);
}); });
+6
View File
@@ -157,6 +157,12 @@ describe('Test MathJax', () => {
done(); done();
}); });
}); });
test('no eq in code', (done) => {
renderer.renderMathJax('this code is ```start $a$ end $$hello$$``` beautiful', (data) => {
expect(data).toBe('this code is ```start $a$ end $$hello$$``` beautiful');
done();
});
});
test('multiple eq', (done) => { test('multiple eq', (done) => {
renderer.renderMathJax('$$\n\nA\n\n$$\nstart $a$ end\n$$\n\nA\n\n$$', (data) => { renderer.renderMathJax('$$\n\nA\n\n$$\nstart $a$ end\n$$\n\nA\n\n$$', (data) => {
expect(data).toBe('' + expect(data).toBe('' +