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 ...@@ -300,38 +300,56 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
$timeout -> inputEl.focus() $timeout -> inputEl.focus()
MathJaxFallback = false 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') convert = $filter('converter')
re = /(?:\$\$)|(?:\\\(|\\\))/g re = /(?:\$\$)|(?:\\\(|\\\))/g
startMath = 0 startMath = 0
endMath = 0 endMath = 0
i = 0
parts = []
indexes = (match.index while match = re.exec(textToCheck)) indexes = (match while match = re.exec(value))
indexes.push(textToCheck.length) indexes.push(value.length)
parts = for index in indexes for match in indexes
if startMath > endMath if startMath > endMath
endMath = index + 2 endMath = match.index + 2
try try
katex.renderToString($sanitize textToCheck.substring(startMath, index)) parts.push katex.renderToString($sanitize value.substring(startMath, match.index))
catch catch
loadMathJax() loadMathJax()
MathJaxFallback = true MathJaxFallback = true
$sanitize textToCheck.substring(startMath, index) parts.push $sanitize value.substring(startMath, match.index)
else else
startMath = index + 2 startMath = match.index + 2
$sanitize convert textToCheck.substring(endMath, index) # 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
return parts.join('') # with markup: <p>Here is some inline math: </p>\(2 + 2 = 4\)
# Here we look for various cases.
# Re-render the markdown when the view needs updating. if match[0] == "\\("
ctrl.$render = -> # Text falls between two instances of inline math, we must remove the opening and
if !scope.readonly and !scope.preview # closing <p> tags since this is meant to be one paragraph.
inputEl.val (ctrl.$viewValue or '') if i - 1 >= 0 and indexes[i - 1].toString() == "\\)"
value = ctrl.$viewValue or '' markdown = $sanitize convert value.substring(endMath, match.index)
rendered = renderMath value parts.push markdown.substring(3, markdown.length - 4)
scope.rendered = $sce.trustAsHtml rendered # 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 if MathJaxFallback
$timeout (-> MathJax?.Hub.Queue ['Typeset', MathJax.Hub, output]), 0, false $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