diff --git a/src/renderer.js b/src/renderer.js index 6094f08..289e7b9 100644 --- a/src/renderer.js +++ b/src/renderer.js @@ -64,7 +64,9 @@ module.exports = (config) => { if (!config['modules']['mathjax']) return cb(data); - const doMJ = (match, format) => { + const spl = data.split('```'); + + const doMJ = (match, format, i) => { const eq = match[1].trim(); const output = config['mathjax']['output_format']; const mjConf = { @@ -74,8 +76,8 @@ module.exports = (config) => { }; mjConf[output] = true; mjAPI.typeset(mjConf, (res) => { - data = data.slice(0, match.index) + res[output] + data.slice(match.index + match[0].length); - renderMathJax(data, (data2) => { + spl[i] = spl[i].slice(0, match.index) + res[output] + spl[i].slice(match.index + match[0].length); + renderMathJax(spl.join('```'), (data2) => { cb(data2); }); }); @@ -84,14 +86,15 @@ module.exports = (config) => { const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m; const inlineEqRegex = /\$([^$\n]*)\$/; - let match; - if ((match = eqRegex.exec(data))) { - doMJ(match, 'TeX'); - } else if ((match = inlineEqRegex.exec(data))) { - doMJ(match, 'inline-TeX'); - } else { - cb(data); + for (let i = 0; i < spl.length; i += 2) { + let match; + if ((match = eqRegex.exec(spl[i]))) { + return doMJ(match, 'TeX', i); + } else if ((match = inlineEqRegex.exec(spl[i]))) { + return doMJ(match, 'inline-TeX', i); + } } + cb(data); }; return { @@ -104,9 +107,9 @@ module.exports = (config) => { if (err) return cb(err); - renderPrism(data, (data) => { - renderPlantUML(data, (data) => { - renderMathJax(data, (data) => { + renderPlantUML(data, (data) => { + renderMathJax(data, (data) => { + renderPrism(data, (data) => { renderShowDown(data, (html) => { cb(null, html); }); diff --git a/test/renderer.test.js b/test/renderer.test.js index d2cc29d..aa09068 100644 --- a/test/renderer.test.js +++ b/test/renderer.test.js @@ -157,6 +157,12 @@ describe('Test MathJax', () => { 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) => { renderer.renderMathJax('$$\n\nA\n\n$$\nstart $a$ end\n$$\n\nA\n\n$$', (data) => { expect(data).toBe('' +