Commit f27d8ced authored by RawKStar77's avatar RawKStar77

Merge renderMath() into ctrl.$render add basic inline math support

Since renderMath() is doing much more than just rendering math, it is now a part of ctrl.$render(). This commit also adds basic support for inline math. However, this is not quite ready, using inline math inside of lists or block quotes does not yet work.
parent 2f4a5e97
......@@ -300,38 +300,56 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
$timeout -> inputEl.focus()
MathJaxFallback = false
renderMath = (textToCheck) ->
# Re-render the markdown when the view needs updating.
ctrl.$render = ->
if !scope.readonly and !scope.preview
inputEl.val (ctrl.$viewValue or '')
value = ctrl.$viewValue or ''
convert = $filter('converter')
re = /(?:\$\$)|(?:\\\(|\\\))/g
startMath = 0
endMath = 0
i = 0
parts = []
indexes = (match.index while match = re.exec(textToCheck))
indexes.push(textToCheck.length)
indexes = (match while match = re.exec(value))
indexes.push(value.length)
parts = for index in indexes
for match in indexes
if startMath > endMath
endMath = index + 2
endMath = match.index + 2
try
katex.renderToString($sanitize textToCheck.substring(startMath, index))
parts.push katex.renderToString($sanitize value.substring(startMath, match.index))
catch
loadMathJax()
MathJaxFallback = true
$sanitize textToCheck.substring(startMath, index)
parts.push $sanitize value.substring(startMath, match.index)
else
startMath = index + 2
$sanitize convert textToCheck.substring(endMath, index)
return parts.join('')
# Re-render the markdown when the view needs updating.
ctrl.$render = ->
if !scope.readonly and !scope.preview
inputEl.val (ctrl.$viewValue or '')
value = ctrl.$viewValue or ''
rendered = renderMath value
scope.rendered = $sce.trustAsHtml rendered
startMath = match.index + 2
# Inline math needs to fall inline, which can be tricky considering the markdown
# converter will take the part that comes before a peice of math and surround it
# with markup: <p>Here is some inline math: </p>\(2 + 2 = 4\)
# Here we look for various cases.
if match[0] == "\\("
# Text falls between two instances of inline math, we must remove the opening and
# closing <p> tags since this is meant to be one paragraph.
if i - 1 >= 0 and indexes[i - 1].toString() == "\\)"
markdown = $sanitize convert value.substring(endMath, match.index)
parts.push markdown.substring(3, markdown.length - 4)
# Text preceeds a case of inline math. We must remove the ending </p> tag
# so that the math is inline.
else
markdown = $sanitize convert value.substring(endMath, match.index)
parts.push markdown.substring(0, markdown.length - 4)
# Text follows a case of inline math, we must remove opening <p> tag.
else if i - 1 >= 0 and indexes[i - 1].toString() == "\\)"
markdown = $sanitize convert value.substring(endMath, match.index)
parts.push markdown.substring(3, markdown.length)
else # Block Math or no math.
parts.push $sanitize convert value.substring(endMath, match.index)
i++
scope.rendered = $sce.trustAsHtml parts.join('')
if MathJaxFallback
$timeout (-> MathJax?.Hub.Queue ['Typeset', MathJax.Hub, output]), 0, false
......
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