Skip to content
Snippets Groups Projects
Commit 4349e448 authored by cbreeden's avatar cbreeden Committed by Christopher Breeden
Browse files

refactored inline processing for pandoc syntax

parent a5a4427d
No related branches found
No related tags found
No related merge requests found
......@@ -7,72 +7,91 @@ 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.
}
secondLastChar= pos > 1 ? state.src[pos-2] : ' '
lastChar = pos > 0 ? state.src[pos-1] : ' '
nextChar = pos + 1 < state.src.length ? state.src[pos+1] : ' '
prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1;
nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -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;
// 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;
}
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 = state.pos;
if (state.src[state.pos] !== "$") { return false; }
start = state.pos;
if(state.src[start] !== '$'){ return false; }
if(!isValidDelim(state)){
res = isValidDelim(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;
}
state.md.inline.skipToken(state);
}
if(!found){
// Parser failed to find closing delimiter, so it is not a valid math
start = state.pos + 1;
match = state.src.indexOf("$", start);
// 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.
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 false;
return true;
}
//found the closing delimiter and state.pos is pointing it
// Check if we have empty content, ie: $$. Do not parse.
if (match - start === 0) {
if (!silent) { state.pending += "$$"; }
state.pos = start + 1;
return true;
}
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 +183,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' ]
});
......
0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'run', 'lint' ]
2 info using npm@2.14.20
3 info using node@v4.4.0
4 verbose stack Error: missing script: lint
4 verbose stack at run (/usr/lib/node_modules/npm/lib/run-script.js:142:19)
4 verbose stack at /usr/lib/node_modules/npm/lib/run-script.js:58:5
4 verbose stack at /usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:345:5
4 verbose stack at checkBinReferences_ (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:309:45)
4 verbose stack at final (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:343:3)
4 verbose stack at then (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:113:5)
4 verbose stack at /usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:300:12
4 verbose stack at /usr/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
4 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)
5 verbose cwd /home/breeden/github/markdown-it-katex
6 error Linux 3.16.0-4-amd64
7 error argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "lint"
8 error node v4.4.0
9 error npm v2.14.20
10 error missing script: lint
11 error If you need help, you may report this error at:
11 error <https://github.com/npm/npm/issues>
12 verbose exit [ 1, true ]
......@@ -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
.
......@@ -201,16 +187,16 @@ 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 left of opening inline math
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 right of closing inline math.
Require non whitespace to left of closing inline math.
.
I will give you 20$, if you give me 15$ tomorrow.
I will give you $20 today, if you give me more $ tomorrow.
.
<p>I will give you 20$, if you give me 15$ tomorrow.</p>
<p>I will give you $20 today, if you give me more $ tomorrow.</p>
.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment