Commit be049d67 authored by Aron Carroll's avatar Aron Carroll

Merge pull request #1558 from hypothesis/math-support

Improve math support for matrices, inline content and list item and block quotes containing math.
parents 72ca80be ac4952c5
...@@ -53,31 +53,32 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -53,31 +53,32 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
# Focus the input # Focus the input
input.focus() input.focus()
applyInlineMarkup = (markup, innertext)-> applyInlineMarkup = (markupL, innertext, markupR) ->
markupR or= markupL
text = userSelection() text = userSelection()
if text.selection == "" if text.selection == ""
newtext = text.before + markup + innertext + markup + text.after newtext = text.before + markupL + innertext + markupR + text.after
start = (text.before + markup).length start = (text.before + markupL).length
end = (text.before + innertext + markup).length end = (text.before + innertext + markupR).length
insertMarkup(newtext, start, end) insertMarkup(newtext, start, end)
else else
# Check to see if markup has already been applied before to the selection. # Check to see if markup has already been applied before to the selection.
slice1 = text.before.slice(text.before.length - markup.length) slice1 = text.before.slice(text.before.length - markupL.length)
slice2 = text.after.slice(0, markup.length) slice2 = text.after.slice(0, markupR.length)
if slice1 == markup and slice2 == markup if (slice1 == markupL and slice2 == markupR)
# Remove markup # Remove markup
newtext = ( newtext = (
text.before.slice(0, (text.before.length - markup.length)) + text.before.slice(0, (text.before.length - markupL.length)) +
text.selection + text.after.slice(markup.length) text.selection + text.after.slice(markupR.length)
) )
start = text.before.length - markup.length start = text.before.length - markupL.length
end = (text.before + text.selection).length - markup.length end = (text.before + text.selection).length - markupR.length
insertMarkup(newtext, start, end) insertMarkup(newtext, start, end)
else else
# Apply markup # Apply markup
newtext = text.before + markup + text.selection + markup + text.after newtext = text.before + markupL + text.selection + markupR + text.after
start = (text.before + markup).length start = (text.before + markupL).length
end = (text.before + text.selection + markup).length end = (text.before + text.selection + markupR).length
insertMarkup(newtext, start, end) insertMarkup(newtext, start, end)
scope.insertBold = -> scope.insertBold = ->
...@@ -87,7 +88,16 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -87,7 +88,16 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
applyInlineMarkup("*", "Italic") applyInlineMarkup("*", "Italic")
scope.insertMath = -> scope.insertMath = ->
applyInlineMarkup("$$", "LaTex") text = userSelection()
index = text.before.length
if (
index == 0 or
input.value[index - 1] == '\n' or
(input.value[index - 1] == '$' and input.value[index - 2] == '$')
)
applyInlineMarkup('$$', 'LaTeX or MathML')
else
applyInlineMarkup('\\(', 'LaTeX or MathML', '\\)')
scope.insertLink = -> scope.insertLink = ->
text = userSelection() text = userSelection()
...@@ -264,27 +274,54 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -264,27 +274,54 @@ 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()
renderMath = (textToCheck) -> mathJaxFallback = false
# Parses text for math as denoted by '$$' renderMathAndMarkdown = (textToCheck) ->
i = 0 convert = $filter('converter')
re = /\$\$/g
startMath = 0
endMath = 0
indexes = (match.index while match = re.exec(textToCheck))
indexes.push(textToCheck.length)
parts = for index in indexes
if startMath > endMath
endMath = index + 2
try
katex.renderToString($sanitize textToCheck.substring(startMath, index))
catch
loadMathJax()
mathJaxFallback = true
$sanitize textToCheck.substring(startMath, index)
else
startMath = index + 2
$sanitize convert renderInlineMath textToCheck.substring(endMath, index)
return parts.join('')
renderInlineMath = (textToCheck) ->
re = /\\?\\\(|\\?\\\)/g
startMath = null startMath = null
endMath = null endMath = null
for char in textToCheck match = undefined
if char == "$" and textToCheck[i + 1] == "$" indexes = []
while match = re.exec(textToCheck)
indexes.push match.index
for index in indexes
if startMath == null if startMath == null
startMath = i + 2 startMath = index + 2
else else
endMath = i endMath = index
i++
if startMath != null and endMath != null if startMath != null and endMath != null
math = katex.renderToString(textToCheck.substring(startMath, endMath)) math = katex.renderToString(textToCheck.substring(startMath, endMath))
textToCheck = ( textToCheck = (
textToCheck.substring(0, (startMath - 2)) + math + textToCheck.substring(0, (startMath - 2)) + math +
textToCheck.substring((endMath + 2)) textToCheck.substring(endMath + 2)
) )
startMath = null startMath = null
endMath = null endMath = null
return renderMath(textToCheck) return renderInlineMath(textToCheck)
return textToCheck return textToCheck
# Re-render the markdown when the view needs updating. # Re-render the markdown when the view needs updating.
...@@ -292,14 +329,9 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -292,14 +329,9 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
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 = renderMathAndMarkdown value
try
rendered = renderMath markdown
scope.rendered = $sce.trustAsHtml rendered
catch
loadMathJax()
rendered = markdown
scope.rendered = $sce.trustAsHtml rendered scope.rendered = $sce.trustAsHtml rendered
if mathJaxFallback
$timeout (-> MathJax?.Hub.Queue ['Typeset', MathJax.Hub, output]), 0, false $timeout (-> MathJax?.Hub.Queue ['Typeset', MathJax.Hub, output]), 0, false
# React to the changes to the input # React to the changes to the input
......
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