Added proper math-mode escaping
This commit is contained in:
parent
97cea23133
commit
fd464f82da
2 changed files with 39 additions and 20 deletions
45
index.js
45
index.js
|
@ -40,20 +40,33 @@ function isValidDelim(state, pos) {
|
|||
}
|
||||
|
||||
function math_inline(state, silent) {
|
||||
var start, match, token, res,
|
||||
pos = state.pos;
|
||||
var start, match, token, res, pos, esc_count;
|
||||
|
||||
if (state.src[state.pos] !== "$") { return false; }
|
||||
|
||||
res = isValidDelim(state, pos);
|
||||
res = isValidDelim(state, state.pos);
|
||||
if (!res.can_open) {
|
||||
if (!silent) { state.pending += "$"; }
|
||||
state.pos += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
// First check for and bypass all properly escaped delimieters
|
||||
// This loop will assume that the first leading backtick can not
|
||||
// be the first character in state.src, which is known since
|
||||
// we have found an opening delimieter already.
|
||||
start = state.pos + 1;
|
||||
match = state.src.indexOf("$", start);
|
||||
match = start;
|
||||
while ( (match = state.src.indexOf("$", match)) !== -1) {
|
||||
// Found potential $, look for escapes, pos will point to
|
||||
// first non escape when complete
|
||||
pos = match - 1;
|
||||
while (state.src[pos] === "\\") { pos -= 1; }
|
||||
|
||||
// Even number of escapes, potential closing delimiter found
|
||||
if ( ((match - pos) % 2) == 1 ) { break; }
|
||||
match += 1;
|
||||
}
|
||||
|
||||
// No closing delimter found. Consume $ and continue.
|
||||
if (match === -1) {
|
||||
|
@ -62,22 +75,6 @@ function math_inline(state, silent) {
|
|||
return true;
|
||||
}
|
||||
|
||||
res = isValidDelim(state, match);
|
||||
|
||||
// We only will look at the very next delimeter while searching
|
||||
// for closing delimeters. As a consequnce, we will never send
|
||||
// KaTeX a $ inside of math mode, even if escaped. The other alternative
|
||||
// would otherwise require escaping commonly used things such as
|
||||
// \int, \sum, etc... Perhaps there is a way to find a better solution.
|
||||
// Such as counting the number of \\\\\\\$ and if odd escape $ and remove
|
||||
// and leading \ or otherwise leave as is.
|
||||
|
||||
if (!res.can_close) {
|
||||
if (!silent) { state.pending += "$"; }
|
||||
state.pos = start;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if we have empty content, ie: $$. Do not parse.
|
||||
if (match - start === 0) {
|
||||
if (!silent) { state.pending += "$$"; }
|
||||
|
@ -85,6 +82,14 @@ function math_inline(state, silent) {
|
|||
return true;
|
||||
}
|
||||
|
||||
// Check for valid closing delimiter
|
||||
res = isValidDelim(state, match);
|
||||
if (!res.can_close) {
|
||||
if (!silent) { state.pending += "$"; }
|
||||
state.pos = start;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!silent) {
|
||||
token = state.push('math_inline', 'math', 0);
|
||||
token.markup = "$";
|
||||
|
|
14
test/fixtures/default.txt
vendored
14
test/fixtures/default.txt
vendored
|
@ -207,3 +207,17 @@ It's well know that $$1 + 1 = 3$$ for sufficiently large 1.
|
|||
.
|
||||
<p>It's well know that $$1 + 1 = 3$$ for sufficiently large 1.</p>
|
||||
.
|
||||
|
||||
Escaped delimiters in math mode
|
||||
.
|
||||
Money adds: $\$X + \$Y = \$Z$.
|
||||
.
|
||||
<p>Money adds: <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">$</mi><mi>X</mi><mo>+</mo><mi mathvariant="normal">$</mi><mi>Y</mi><mo>=</mo><mi mathvariant="normal">$</mi><mi>Z</mi></mrow><annotation encoding="application/x-tex">\$X + \$Y = \$Z</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:0.83333em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">$</span><span class="mord mathit" style="margin-right:0.07847em;">X</span><span class="mbin">+</span><span class="mord mathrm">$</span><span class="mord mathit" style="margin-right:0.22222em;">Y</span><span class="mrel">=</span><span class="mord mathrm">$</span><span class="mord mathit" style="margin-right:0.07153em;">Z</span></span></span></span>.</p>
|
||||
.
|
||||
|
||||
Multiple escaped delimiters in math module
|
||||
.
|
||||
Weird-o: $\displaystyle{\begin{pmatrix} \$ & 1\\\$ \end{pmatrix}}$.
|
||||
.
|
||||
<p>Weird-o: <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow><mo fence="true">(</mo><mtable><mtr><mtd><mrow><mi mathvariant="normal">$</mi></mrow></mtd><mtd><mrow><mn>1</mn></mrow></mtd></mtr><mtr><mtd><mrow><mi mathvariant="normal">$</mi></mrow></mtd></mtr></mtable><mo fence="true">)</mo></mrow></mrow></mstyle></mrow><annotation encoding="application/x-tex">\displaystyle{\begin{pmatrix} \$ & 1\\\$ \end{pmatrix}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.45em;"></span><span class="strut bottom" style="height:2.40003em;vertical-align:-0.95003em;"></span><span class="base textstyle uncramped"><span class="reset-textstyle displaystyle textstyle uncramped"><span class="mord displaystyle textstyle uncramped"><span class="minner displaystyle textstyle uncramped"><span class="style-wrap reset-textstyle textstyle uncramped" style="top:0em;"><span class="delimsizing size3">(</span></span><span class="mord"><span class="mtable"><span class="col-align-c"><span class="vlist"><span style="top:-0.6099999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span><span class="mord displaystyle textstyle uncramped"><span class="mord mathrm">$</span></span></span><span style="top:0.5900000000000003em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span><span class="mord displaystyle textstyle uncramped"><span class="mord mathrm">$</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span></span></span></span><span class="arraycolsep" style="width:0.5em;"></span><span class="arraycolsep" style="width:0.5em;"></span><span class="col-align-c"><span class="vlist"><span style="top:-0.6099999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span><span class="mord displaystyle textstyle uncramped"><span class="mord mathrm">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span></span></span></span></span></span><span class="style-wrap reset-textstyle textstyle uncramped" style="top:0em;"><span class="delimsizing size3">)</span></span></span></span></span></span></span></span>.</p>
|
||||
.
|
||||
|
|
Reference in a new issue