Commit 54262ca1 authored by RawKStar77's avatar RawKStar77

Rewrite renderMath() function

Rewrites the renderMath() function to sanatize, render math, and render markdown.

Big thanks to @tilgovi for help here.
parent ea449f43
...@@ -300,45 +300,32 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -300,45 +300,32 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
$timeout -> inputEl.focus() $timeout -> inputEl.focus()
renderMath = (textToCheck) -> renderMath = (textToCheck) ->
convert = $filter('converter')
re = /(?:\$\$)|(?:\\\(?\)?)/g re = /(?:\$\$)|(?:\\\(?\)?)/g
startMath = null
endMath = null startMath = 0
match = undefined endMath = 0
indexes = []
while match = re.exec(textToCheck) indexes = (match.index while match = re.exec(textToCheck))
indexes.push [ indexes.push(textToCheck.length)
match.index
] parts = for index in indexes
for index in indexes if startMath > endMath
if startMath == null endMath = index + 2
startMath = index[0] + 2 katex.renderToString($sanitize textToCheck.substring(startMath, index))
else else
endMath = index[0] startMath = index + 2
if startMath != null and endMath != null $sanitize convert textToCheck.substring(endMath, index)
math = katex.renderToString(textToCheck.substring(startMath, endMath))
textToCheck = ( return parts.join('')
textToCheck.substring(0, (startMath - 2)) + math +
textToCheck.substring((endMath + 2))
)
startMath = null
endMath = null
return renderMath(textToCheck)
return textToCheck
# Re-render the markdown when the view needs updating. # Re-render the markdown when the view needs updating.
ctrl.$render = -> ctrl.$render = ->
if !scope.readonly and !scope.preview if !scope.readonly and !scope.preview
inputEl.val (ctrl.$viewValue or '') inputEl.val (ctrl.$viewValue or '')
value = ctrl.$viewValue or '' value = ctrl.$viewValue or ''
markdown = $sanitize $filter('converter') value rendered = renderMath value
try scope.rendered = $sce.trustAsHtml rendered
rendered = renderMath markdown
scope.rendered = $sce.trustAsHtml rendered
catch
loadMathJax()
rendered = markdown
scope.rendered = $sce.trustAsHtml rendered
$timeout (-> MathJax?.Hub.Queue ['Typeset', MathJax.Hub, output]), 0, false
# React to the changes to the input # React to the changes to the input
inputEl.bind 'blur change keyup', -> inputEl.bind 'blur change keyup', ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment