diff --git a/notebook/static/notebook/js/mathjaxutils.js b/notebook/static/notebook/js/mathjaxutils.js index 4f6b1d5aa9..bd82eea733 100644 --- a/notebook/static/notebook/js/mathjaxutils.js +++ b/notebook/static/notebook/js/mathjaxutils.js @@ -55,11 +55,9 @@ define([ // Other minor modifications are also due to StackExchange and are used with // permission. - var inline = "$"; // the inline math delimiter - // MATHSPLIT contains the pattern for math delimiters and special symbols // needed for searching for math in the text input. - var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i; + var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[{}$]|[{}]|(?:\n\s*)+|@@\d+@@|\\\\(?:\(|\)|\[|\]))/i; // The math is in blocks i through j, so // collect it into one block and clear the others. @@ -173,11 +171,16 @@ define([ // Look for math start delimiters and when // found, set up the end delimiter. // - if (block === inline || block === "$$") { + if (block === "$" || block === "$$") { start = i; end = block; braces = 0; } + else if (block === "\\\\\(" || block === "\\\\\[") { + start = i; + end = block.slice(-1) === "(" ? "\\\\\)" : "\\\\\]"; + braces = 0; + } else if (block.substr(1, 5) === "begin") { start = i; end = "\\end" + block.substr(6); @@ -199,9 +202,27 @@ define([ // and clear the math array (no need to keep it around). // var replace_math = function (text, math) { - text = text.replace(/@@(\d+)@@/g, function (match, n) { - return math[n]; - }); + // + // Replaces a math placeholder with its corresponding group. + // The math delimiters "\\(", "\\[", "\\)" and "\\]" are replaced + // removing one backslash in order to be interpreted correctly by MathJax. + // + var math_group_process = function (match, n) { + var math_group = math[n]; + + if (math_group.substr(0, 3) === "\\\\\(" && math_group.substr(math_group.length - 3) === "\\\\\)") { + math_group = "\\\(" + math_group.substring(3, math_group.length - 3) + "\\\)"; + } else if (math_group.substr(0, 3) === "\\\\\[" && math_group.substr(math_group.length - 3) === "\\\\\]") { + math_group = "\\\[" + math_group.substring(3, math_group.length - 3) + "\\\]"; + } + + return math_group; + }; + + // Replace all the math group placeholders in the text + // with the saved strings. + text = text.replace(/@@(\d+)@@/g, math_group_process); + return text; }; diff --git a/notebook/tests/notebook/markdown.js b/notebook/tests/notebook/markdown.js index 344cfed3bc..3147476305 100644 --- a/notebook/tests/notebook/markdown.js +++ b/notebook/tests/notebook/markdown.js @@ -102,4 +102,55 @@ casper.notebook_test(function () { codeblock = '```aaaa\nx = 1\n```' result = '
x = 1\n
' md_render_test(codeblock, result, 'Markdown code block unknown language'); + + function mathjax_render_test(input_string, result, message){ + casper.thenEvaluate(function (text){ + window._test_result = null; + require(['notebook/js/mathjaxutils'],function(mathjaxutils){ + window._test_result = mathjaxutils.remove_math(text); + }); + }, {text: input_string}); + casper.waitFor(function() { + return casper.evaluate(function(){ + return window._test_result!==null; + }); + }); + casper.then(function(){ + var return_val = casper.evaluate(function(){ + var blah = window._test_result; + delete window._test_result; + return blah; + }); + this.test.assertEquals(return_val[0], result[0], message+" markdown"); + this.test.assertEquals(return_val[1].length, result[1].length, message+" math instance count"); + for(var i=0; i