| 1 |
/*----------------------------------------------------------------------------- |
| 2 |
| Copyright (c) Jupyter Development Team. |
| 3 |
| Distributed under the terms of the Modified BSD License. |
| 4 |
|----------------------------------------------------------------------------*/ |
| 5 |
// Some magic for deferring mathematical expressions to MathJax |
| 6 |
// by hiding them from the Markdown parser. |
| 7 |
// Some of the code here is adapted with permission from Davide Cervone |
| 8 |
// under the terms of the Apache2 license governing the MathJax project. |
| 9 |
// Other minor modifications are also due to StackExchange and are used with |
| 10 |
// permission. |
| 11 |
const inline = '$'; // the inline math delimiter |
| 12 |
// MATHSPLIT contains the pattern for math delimiters and special symbols |
| 13 |
// needed for searching for math in the text input. |
| 14 |
const MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[{}$]|[{}]|(?:\n\s*)+|@@\d+@@|\\\\(?:\(|\)|\[|\]))/i; |
| 15 |
/** |
| 16 |
* Break up the text into its component parts and search |
| 17 |
* through them for math delimiters, braces, linebreaks, etc. |
| 18 |
* Math delimiters must match and braces must balance. |
| 19 |
* Don't allow math to pass through a double linebreak |
| 20 |
* (which will be a paragraph). |
| 21 |
*/ |
| 22 |
export function removeMath(text) { |
| 23 |
const math = []; // stores math strings for later |
| 24 |
let start = null; |
| 25 |
let end = null; |
| 26 |
let last = null; |
| 27 |
let braces = 0; |
| 28 |
let deTilde; |
| 29 |
// Except for extreme edge cases, this should catch precisely those pieces of the markdown |
| 30 |
// source that will later be turned into code spans. While MathJax will not TeXify code spans, |
| 31 |
// we still have to consider them at this point; the following issue has happened several times: |
| 32 |
// |
| 33 |
// `$foo` and `$bar` are variables. --> <code>$foo ` and `$bar</code> are variables. |
| 34 |
const hasCodeSpans = /`/.test(text); |
| 35 |
if (hasCodeSpans) { |
| 36 |
text = text |
| 37 |
.replace(/~/g, '~T') |
| 38 |
.replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, (wholematch) => wholematch.replace(/\$/g, '~D')); |
| 39 |
deTilde = (text) => { |
| 40 |
return text.replace(/~([TD])/g, (wholematch, character) => character === 'T' ? '~' : inline); |
| 41 |
}; |
| 42 |
} |
| 43 |
else { |
| 44 |
deTilde = (text) => { |
| 45 |
return text; |
| 46 |
}; |
| 47 |
} |
| 48 |
let blocks = text.replace(/\r\n?/g, '\n').split(MATHSPLIT); |
| 49 |
for (let i = 1, m = blocks.length; i < m; i += 2) { |
| 50 |
const block = blocks[i]; |
| 51 |
if (block.charAt(0) === '@') { |
| 52 |
// |
| 53 |
// Things that look like our math markers will get |
| 54 |
// stored and then retrieved along with the math. |
| 55 |
// |
| 56 |
blocks[i] = '@@' + math.length + '@@'; |
| 57 |
math.push(block); |
| 58 |
} |
| 59 |
else if (start !== null) { |
| 60 |
// |
| 61 |
// If we are in math, look for the end delimiter, |
| 62 |
// but don't go past double line breaks, and |
| 63 |
// and balance braces within the math. |
| 64 |
// |
| 65 |
if (block === end) { |
| 66 |
if (braces) { |
| 67 |
last = i; |
| 68 |
} |
| 69 |
else { |
| 70 |
blocks = processMath(start, i, deTilde, math, blocks); |
| 71 |
start = null; |
| 72 |
end = null; |
| 73 |
last = null; |
| 74 |
} |
| 75 |
} |
| 76 |
else if (block.match(/\n.*\n/)) { |
| 77 |
if (last !== null) { |
| 78 |
i = last; |
| 79 |
blocks = processMath(start, i, deTilde, math, blocks); |
| 80 |
} |
| 81 |
start = null; |
| 82 |
end = null; |
| 83 |
last = null; |
| 84 |
braces = 0; |
| 85 |
} |
| 86 |
else if (block === '{') { |
| 87 |
braces++; |
| 88 |
} |
| 89 |
else if (block === '}' && braces) { |
| 90 |
braces--; |
| 91 |
} |
| 92 |
} |
| 93 |
else { |
| 94 |
// |
| 95 |
// Look for math start delimiters and when |
| 96 |
// found, set up the end delimiter. |
| 97 |
// |
| 98 |
if (block === inline || block === '$$') { |
| 99 |
start = i; |
| 100 |
end = block; |
| 101 |
braces = 0; |
| 102 |
} |
| 103 |
else if (block === '\\\\(' || block === '\\\\[') { |
| 104 |
start = i; |
| 105 |
end = block.slice(-1) === '(' ? '\\\\)' : '\\\\]'; |
| 106 |
braces = 0; |
| 107 |
} |
| 108 |
else if (block.substr(1, 5) === 'begin') { |
| 109 |
start = i; |
| 110 |
end = '\\end' + block.substr(6); |
| 111 |
braces = 0; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
if (start !== null && last !== null) { |
| 116 |
blocks = processMath(start, last, deTilde, math, blocks); |
| 117 |
start = null; |
| 118 |
end = null; |
| 119 |
last = null; |
| 120 |
} |
| 121 |
return { text: deTilde(blocks.join('')), math }; |
| 122 |
} |
| 123 |
/** |
| 124 |
* Put back the math strings that were saved, |
| 125 |
* and clear the math array (no need to keep it around). |
| 126 |
*/ |
| 127 |
export function replaceMath(text, math) { |
| 128 |
/** |
| 129 |
* Replace a math placeholder with its corresponding group. |
| 130 |
* The math delimiters "\\(", "\\[", "\\)" and "\\]" are replaced |
| 131 |
* removing one backslash in order to be interpreted correctly by MathJax. |
| 132 |
*/ |
| 133 |
const process = (match, n) => { |
| 134 |
let group = math[n]; |
| 135 |
if (group.substr(0, 3) === '\\\\(' && |
| 136 |
group.substr(group.length - 3) === '\\\\)') { |
| 137 |
group = '\\(' + group.substring(3, group.length - 3) + '\\)'; |
| 138 |
} |
| 139 |
else if (group.substr(0, 3) === '\\\\[' && |
| 140 |
group.substr(group.length - 3) === '\\\\]') { |
| 141 |
group = '\\[' + group.substring(3, group.length - 3) + '\\]'; |
| 142 |
} |
| 143 |
return group; |
| 144 |
}; |
| 145 |
// Replace all the math group placeholders in the text |
| 146 |
// with the saved strings. |
| 147 |
return text.replace(/@@(\d+)@@/g, process); |
| 148 |
} |
| 149 |
/** |
| 150 |
* Process math blocks. |
| 151 |
* |
| 152 |
* The math is in blocks i through j, so |
| 153 |
* collect it into one block and clear the others. |
| 154 |
* Replace &, <, and > by named entities. |
| 155 |
* For IE, put <br> at the ends of comments since IE removes \n. |
| 156 |
* Clear the current math positions and store the index of the |
| 157 |
* math, then push the math string onto the storage array. |
| 158 |
* The preProcess function is called on all blocks if it has been passed in |
| 159 |
*/ |
| 160 |
function processMath(i, j, preProcess, math, blocks) { |
| 161 |
let block = blocks |
| 162 |
.slice(i, j + 1) |
| 163 |
.join('') |
| 164 |
.replace(/&/g, '&') // use HTML entity for & |
| 165 |
.replace(/</g, '<') // use HTML entity for < |
| 166 |
.replace(/>/g, '>'); // use HTML entity for > |
| 167 |
if (navigator && navigator.appName === 'Microsoft Internet Explorer') { |
| 168 |
block = block.replace(/(%[^\n]*)\n/g, '$1<br/>\n'); |
| 169 |
} |
| 170 |
while (j > i) { |
| 171 |
blocks[j] = ''; |
| 172 |
j--; |
| 173 |
} |
| 174 |
blocks[i] = '@@' + math.length + '@@'; // replace the current block text with a unique tag to find later |
| 175 |
if (preProcess) { |
| 176 |
block = preProcess(block); |
| 177 |
} |
| 178 |
math.push(block); |
| 179 |
return blocks; |
| 180 |
} |
| 181 |
//# sourceMappingURL=latex.js.map |