Created
December 5, 2014 13:21
-
-
Save dlimpid/96c1e10e09e1bbba242a to your computer and use it in GitHub Desktop.
[Remarkable](https://github.com/jonschlinkert/remarkable/) plugin for MathJax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: | |
# | |
# ``` | |
# md = new Remarkable() | |
# md.use remarkableMath() | |
# parsedText = md.render text | |
# ``` | |
remarkableMath = -> | |
parser = (state, silent) -> | |
if state.src[state.pos] == '$' | |
# blabla$$ a < b $$blabla | |
# ^ ^ ^-- allClosePos | |
# | +-- matchClosePos | |
# +-- state.pos | |
# always can find `$` or `$$` | |
matchOpen = /^(\${1,2})/m.exec state.src.slice state.pos | |
mathPos = state.pos + matchOpen[0].length | |
matchCloseRe = new RegExp "(\\${#{matchOpen[0].length}})", 'm' | |
while matchClose = matchCloseRe.exec state.src.slice mathPos | |
matchClosePos = mathPos + matchClose.index | |
if state.src[matchClosePos - 1] != '\\' # hit! | |
allClosePos = matchClosePos + matchOpen[0].length | |
if !silent | |
state.push | |
type: 'math' | |
math: state.src.slice state.pos, allClosePos | |
level: state.level | |
state.pos = allClosePos | |
return true | |
else | |
mathPos += matchClose.index + 1 | |
# If we got here, we could not find a closing. | |
return false | |
else if state.src[state.pos] != '\\' | |
return false | |
else | |
return false | |
renderer = (tokens, id, options, env) -> | |
# actually do nothing and wait MathJax | |
tokens[id].math | |
self = (md) -> | |
md.inline.ruler.push 'math', parser | |
md.renderer.rules.math = renderer | |
null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment