Merge pull request #4 from cbreeden/master
Handle escapes properly, and have syntax agree with Pandoc's math-mode
This commit is contained in:
commit
b8d8e25a80
4 changed files with 207 additions and 144 deletions
11
README.md
11
README.md
|
@ -75,6 +75,17 @@ $$\begin{array}{c}
|
|||
\end{array}$$
|
||||
```
|
||||
|
||||
## Syntax
|
||||
|
||||
Math parsing in markdown is designed to agree with the conventions set by pandoc:
|
||||
|
||||
Anything between two $ characters will be treated as TeX math. The opening $ must
|
||||
have a non-space character immediately to its right, while the closing $ must
|
||||
have a non-space character immediately to its left, and must not be followed
|
||||
immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some
|
||||
reason you need to enclose text in literal $ characters, backslash-escape them and
|
||||
they won’t be treated as math delimiters.
|
||||
|
||||
## Math Syntax Support
|
||||
|
||||
KaTeX is based on TeX and LaTeX. Support for both is growing. Here's a list of
|
||||
|
|
110
index.js
110
index.js
|
@ -7,72 +7,96 @@ It differs in that it takes (a subset of) LaTeX as input and relies on KaTeX
|
|||
for rendering output.
|
||||
*/
|
||||
|
||||
/*jslint node: true */
|
||||
'use strict';
|
||||
|
||||
var katex = require('katex');
|
||||
|
||||
//return if we have valid delimiter, '$', is in the position.
|
||||
function isValidDelim(state) {
|
||||
var lastChar, secondLastChar, nextChar, pos = state.pos;
|
||||
// Test if potential opening or closing delimieter
|
||||
// Assumes that there is a "$" at state.src[pos]
|
||||
function isValidDelim(state, pos) {
|
||||
var prevChar, nextChar,
|
||||
max = state.posMax,
|
||||
can_open = true,
|
||||
can_close = true;
|
||||
|
||||
if(state.src[pos]!=='$'){
|
||||
return false; //the character $ must be in its position.
|
||||
prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1;
|
||||
nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1;
|
||||
|
||||
// Check non-whitespace conditions for opening and closing, and
|
||||
// check that closing delimeter isn't followed by a number
|
||||
if (prevChar === 0x20/* " " */ || prevChar === 0x09/* \t */ ||
|
||||
(nextChar >= 0x30/* "0" */ && nextChar <= 0x39/* "9" */)) {
|
||||
can_close = false;
|
||||
}
|
||||
if (nextChar === 0x20/* " " */ || nextChar === 0x09/* \t */) {
|
||||
can_open = false;
|
||||
}
|
||||
|
||||
secondLastChar= pos > 1 ? state.src[pos-2] : ' '
|
||||
lastChar = pos > 0 ? state.src[pos-1] : ' '
|
||||
nextChar = pos + 1 < state.src.length ? state.src[pos+1] : ' '
|
||||
|
||||
if(
|
||||
lastChar === '\\' //$ we found was escaped.
|
||||
|| (lastChar === '$' && secondLastChar !== '\\') // $ we found was after $ but not escaped.
|
||||
|| nextChar === '$' //$ we found was actually block delimiter $$.
|
||||
)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return {
|
||||
can_open: can_open,
|
||||
can_close: can_close
|
||||
};
|
||||
}
|
||||
|
||||
function math_inline(state, silent) {
|
||||
var start, found = false, token;
|
||||
if(silent) { return false; }
|
||||
var start, match, token, res, pos, esc_count;
|
||||
|
||||
start = state.pos;
|
||||
if(state.src[start] !== '$'){ return false; }
|
||||
if(!isValidDelim(state)){
|
||||
if (state.src[state.pos] !== "$") { return false; }
|
||||
|
||||
res = isValidDelim(state, state.pos);
|
||||
if (!res.can_open) {
|
||||
if (!silent) { state.pending += "$"; }
|
||||
state.pos += 1;
|
||||
state.pending += '$';
|
||||
return true;
|
||||
}
|
||||
state.pos+=1;
|
||||
|
||||
while(state.pos < state.posMax){
|
||||
if(isValidDelim(state)){
|
||||
found = true;
|
||||
break;
|
||||
// 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 = 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;
|
||||
}
|
||||
state.md.inline.skipToken(state);
|
||||
}
|
||||
if(!found){
|
||||
// Parser failed to find closing delimiter, so it is not a valid math
|
||||
|
||||
// No closing delimter found. Consume $ and continue.
|
||||
if (match === -1) {
|
||||
if (!silent) { state.pending += "$"; }
|
||||
state.pos = start;
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (start + 1 === state.pos) {
|
||||
// There is nothing between the delimiters -- don't match.
|
||||
// Check if we have empty content, ie: $$. Do not parse.
|
||||
if (match - start === 0) {
|
||||
if (!silent) { state.pending += "$$"; }
|
||||
state.pos = start + 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check for valid closing delimiter
|
||||
res = isValidDelim(state, match);
|
||||
if (!res.can_close) {
|
||||
if (!silent) { state.pending += "$"; }
|
||||
state.pos = start;
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
//found the closing delimiter and state.pos is pointing it
|
||||
if (!silent) {
|
||||
token = state.push('math_inline', 'math', 0);
|
||||
token.content = state.src.slice(start+1,state.pos).trim();
|
||||
token.markup = '$';
|
||||
token.markup = "$";
|
||||
token.content = state.src.slice(start, match);
|
||||
}
|
||||
|
||||
state.pos += 1;
|
||||
state.pos = match + 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -164,7 +188,7 @@ module.exports = function math_plugin(md, options) {
|
|||
return katexBlock(tokens[idx].content) + '\n';
|
||||
}
|
||||
|
||||
md.inline.ruler.before('escape', 'math_inline', math_inline);
|
||||
md.inline.ruler.after('escape', 'math_inline', math_inline);
|
||||
md.block.ruler.after('blockquote', 'math_block', math_block, {
|
||||
alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
|
||||
});
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
"author": "waylonflinn@gmail.com",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"katex": "^0.5.1"
|
||||
"katex": "^0.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"markdown-it": "^6.0.0",
|
||||
|
|
60
test/fixtures/default.txt
vendored
60
test/fixtures/default.txt
vendored
|
@ -35,20 +35,13 @@ aaa $$ bbb
|
|||
<p>aaa $$ bbb</p>
|
||||
.
|
||||
|
||||
Shouldn't render USD
|
||||
Should require a closing delimiter
|
||||
.
|
||||
aaa $5.99 bbb
|
||||
.
|
||||
<p>aaa $5.99 bbb</p>
|
||||
.
|
||||
|
||||
Shouldn't render trailing delimiter
|
||||
.
|
||||
aaa 5.99$ bbb
|
||||
.
|
||||
<p>aaa 5.99$ bbb</p>
|
||||
.
|
||||
|
||||
Paragraph break in inline math is not allowed
|
||||
.
|
||||
foo $1+1
|
||||
|
@ -59,14 +52,7 @@ foo $1+1
|
|||
<p>= 2$ bar</p>
|
||||
.
|
||||
|
||||
Neither is an end of document
|
||||
.
|
||||
foo $1+1 = 2
|
||||
.
|
||||
<p>foo $1+1 = 2</p>
|
||||
.
|
||||
|
||||
Inline math with apparent markup
|
||||
Inline math with apparent markup should not be processed
|
||||
.
|
||||
foo $1 *i* 1$ bar
|
||||
.
|
||||
|
@ -193,3 +179,45 @@ $$
|
|||
1
|
||||
$$</p>
|
||||
.
|
||||
|
||||
Numbers can not follow closing inline math
|
||||
.
|
||||
Thus, $20,000 and USD$30,000 won't parse as math.
|
||||
.
|
||||
<p>Thus, $20,000 and USD$30,000 won't parse as math.</p>
|
||||
.
|
||||
|
||||
Require non whitespace to right of opening inline math
|
||||
.
|
||||
For some Europeans, it is 2$ for a can of soda, not 1$.
|
||||
.
|
||||
<p>For some Europeans, it is 2$ for a can of soda, not 1$.</p>
|
||||
.
|
||||
|
||||
Require non whitespace to left of closing inline math.
|
||||
.
|
||||
I will give you $20 today, if you give me more $ tomorrow.
|
||||
.
|
||||
<p>I will give you $20 today, if you give me more $ tomorrow.</p>
|
||||
.
|
||||
|
||||
Inline blockmath is not (currently) registered.
|
||||
.
|
||||
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