Skip to content

Instantly share code, notes, and snippets.

@dlimpid
Created December 5, 2014 13:21
Show Gist options
  • Save dlimpid/96c1e10e09e1bbba242a to your computer and use it in GitHub Desktop.
Save dlimpid/96c1e10e09e1bbba242a to your computer and use it in GitHub Desktop.
[Remarkable](https://github.com/jonschlinkert/remarkable/) plugin for MathJax
# 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