Commit fd0ba1dd authored by RawKStar77's avatar RawKStar77

Add MathJax fallback

If KaTex math rendering throws an error, load MathJax from the CDN and render
the math with MathJax.
parent 5b82eb97
...@@ -251,6 +251,7 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -251,6 +251,7 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
input.style.height = output.style.height input.style.height = output.style.height
$timeout -> inputEl.focus() $timeout -> inputEl.focus()
scope.MathJaxFallback = false
scope.renderMath = (textToCheck) -> scope.renderMath = (textToCheck) ->
# Parses text for math as denoted by '$$' # Parses text for math as denoted by '$$'
i = 0 i = 0
...@@ -267,10 +268,29 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -267,10 +268,29 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
try try
math = katex.renderToString(textToCheck.substring(startMath, endMath)) math = katex.renderToString(textToCheck.substring(startMath, endMath))
catch error catch error
# Show error on the frontend so users have some insight of which Tex Commands # KaTex does not have full math support. In the case that we come across math we
# caused the error. KaTex does not yet support all Tex Commands. # can't render, we load MathJax and render the Math with MathJax.
math = error math = textToCheck.substring(startMath, endMath)
textToCheck = textToCheck.substring(0, (startMath - 2)) + math + textToCheck.substring((endMath + 2)) scope.MathJaxFallback = true
try
if !MathJax?
$.ajax {
url:"//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
dataType: 'script'
cache: true
complete: ->
# MathJax configuration overides.
MathJax.Hub.Config({
showMathMenu: false
})
}
return textToCheck
catch error
console.log error
textToCheck = (
textToCheck.substring(0, (startMath - 2)) + math +
textToCheck.substring((endMath + 2))
)
startMath = null startMath = null
endMath = null endMath = null
scope.renderMath(textToCheck) scope.renderMath(textToCheck)
...@@ -284,6 +304,8 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -284,6 +304,8 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
markdown = $sanitize $filter('converter') value markdown = $sanitize $filter('converter') value
rendered = scope.renderMath markdown rendered = scope.renderMath markdown
scope.rendered = $sce.trustAsHtml rendered scope.rendered = $sce.trustAsHtml rendered
if scope.MathJaxFallback
MathJax?.Hub.Queue(["Typeset",MathJax.Hub])
# 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