Commit bd4a386d by Rocky Duan

highlighting while skipping code and mathjax (ugly and slow solution)

parent 39ae9343
...@@ -112,14 +112,16 @@ $ -> ...@@ -112,14 +112,16 @@ $ ->
if Markdown? if Markdown?
Markdown.getMathCompatibleConverter = -> Markdown.getMathCompatibleConverter = (postProcessor) ->
postProcessor ||= ((text) -> text)
converter = Markdown.getSanitizingConverter() converter = Markdown.getSanitizingConverter()
processor = new MathJaxProcessor() processor = new MathJaxProcessor()
converter.hooks.chain "preConversion", MathJaxProcessor.removeMathWrapper(processor) converter.hooks.chain "preConversion", MathJaxProcessor.removeMathWrapper(processor)
converter.hooks.chain "postConversion", MathJaxProcessor.replaceMathWrapper(processor) converter.hooks.chain "postConversion", (text) ->
postProcessor(MathJaxProcessor.replaceMathWrapper(processor)(text))
converter converter
Markdown.makeWmdEditor = (elem, appended_id, imageUploadUrl) -> Markdown.makeWmdEditor = (elem, appended_id, imageUploadUrl, postProcessor) ->
$elem = $(elem) $elem = $(elem)
if not $elem.length if not $elem.length
...@@ -136,7 +138,7 @@ $ -> ...@@ -136,7 +138,7 @@ $ ->
.append($("<div>").attr("id", "wmd-preview#{_append}").addClass("wmd-panel wmd-preview")) .append($("<div>").attr("id", "wmd-preview#{_append}").addClass("wmd-panel wmd-preview"))
$elem.append($wmdPanel) $elem.append($wmdPanel)
converter = Markdown.getMathCompatibleConverter() converter = Markdown.getMathCompatibleConverter(postProcessor)
ajaxFileUpload = (imageUploadUrl, input, startUploadHandler) -> ajaxFileUpload = (imageUploadUrl, input, startUploadHandler) ->
$("#loading").ajaxStart(-> $(this).show()).ajaxComplete(-> $(this).hide()) $("#loading").ajaxStart(-> $(this).show()).ajaxComplete(-> $(this).hide())
......
...@@ -283,8 +283,13 @@ initializeFollowThread = (thread) -> ...@@ -283,8 +283,13 @@ initializeFollowThread = (thread) ->
text.replace(/\&lt\;highlight\&gt\;/g, "<span class='search-highlight'>") text.replace(/\&lt\;highlight\&gt\;/g, "<span class='search-highlight'>")
.replace(/\&lt\;\/highlight\&gt\;/g, "</span>") .replace(/\&lt\;\/highlight\&gt\;/g, "</span>")
stripHighlight = (text, type) ->
text.replace(/\&(amp\;)?lt\;highlight\&(amp\;)?gt\;/g, "")
.replace(/\&(amp\;)?lt\;\/highlight\&(amp\;)?gt\;/g, "")
stripLatexHighlight = (text) -> stripLatexHighlight = (text) ->
text Discussion.processEachMathAndCode text, stripHighlight
markdownWithHighlight = (text) -> markdownWithHighlight = (text) ->
converter = Markdown.getMathCompatibleConverter() converter = Markdown.getMathCompatibleConverter()
...@@ -303,10 +308,7 @@ initializeFollowThread = (thread) -> ...@@ -303,10 +308,7 @@ initializeFollowThread = (thread) ->
$contentBody = $local(".content-body") $contentBody = $local(".content-body")
console.log "raw html:" $contentBody.html Discussion.postMathJaxProcessor markdownWithHighlight $contentBody.html()
console.log $contentBody.html()
$contentBody.html markdownWithHighlight $contentBody.html()
MathJax.Hub.Queue ["Typeset", MathJax.Hub, $contentBody.attr("id")] MathJax.Hub.Queue ["Typeset", MathJax.Hub, $contentBody.attr("id")]
id = $content.attr("_id") id = $content.attr("_id")
......
...@@ -95,12 +95,25 @@ wmdEditors = {} ...@@ -95,12 +95,25 @@ wmdEditors = {}
else else
success(response, textStatus, xhr) success(response, textStatus, xhr)
postMathJaxProcessor: (text) ->
RE_INLINEMATH = /^\$([^\$]*)\$/g
RE_DISPLAYMATH = /^\$\$([^\$]*)\$\$/g
Discussion.processEachMathAndCode text, (s, type) ->
if type == 'display'
s.replace RE_DISPLAYMATH, ($0, $1) ->
"\\[" + $1 + "\\]"
else if type == 'inline'
s.replace RE_INLINEMATH, ($0, $1) ->
"\\(" + $1 + "\\)"
else
s
makeWmdEditor: ($content, $local, cls_identifier) -> makeWmdEditor: ($content, $local, cls_identifier) ->
elem = $local(".#{cls_identifier}") elem = $local(".#{cls_identifier}")
id = $content.attr("_id") id = $content.attr("_id")
appended_id = "-#{cls_identifier}-#{id}" appended_id = "-#{cls_identifier}-#{id}"
imageUploadUrl = Discussion.urlFor('upload') imageUploadUrl = Discussion.urlFor('upload')
editor = Markdown.makeWmdEditor elem, appended_id, imageUploadUrl editor = Markdown.makeWmdEditor elem, appended_id, imageUploadUrl, Discussion.postMathJaxProcessor
wmdEditors["#{cls_identifier}-#{id}"] = editor wmdEditors["#{cls_identifier}-#{id}"] = editor
editor editor
...@@ -164,3 +177,54 @@ wmdEditors = {} ...@@ -164,3 +177,54 @@ wmdEditors = {}
unfollowLink() unfollowLink()
else else
followLink() followLink()
processEachMathAndCode: (text, processor) ->
codeArchive = []
RE_DISPLAYMATH = /^([^\$]*?)\$\$([^\$].+?)\$\$(.*)$/m
RE_INLINEMATH = /^([^\$]*?)\$([^\$]+?)\$(.*)$/m
ESCAPED_DOLLAR = '@@ESCAPED_D@@'
ESCAPED_BACKSLASH = '@@ESCAPED_B@@'
processedText = ""
$div = $("<div>").html(text)
$div.find("code").each (index, code) ->
codeArchive.push $(code).html()
$(code).html(codeArchive.length - 1)
text = $div.html()
text = text.replace /\\\$/g, ESCAPED_DOLLAR
while true
if RE_INLINEMATH.test(text)
text = text.replace RE_INLINEMATH, ($0, $1, $2, $3) ->
processedText += $1 + processor("$" + $2 + "$", 'inline')
$3
else if RE_DISPLAYMATH.test(text)
text = text.replace RE_DISPLAYMATH, ($0, $1, $2, $3) ->
processedText += $1 + processor("$$" + $2 + "$$", 'display')
$3
else
processedText += text
break
text = processedText
text = text.replace(new RegExp(ESCAPED_DOLLAR), '\\$')
text = text.replace /\\\\\\\\/g, ESCAPED_BACKSLASH
text = text.replace /\\begin\{([a-z]*\*?)\}([\s\S]*?)\\end\{\1\}/img, ($0, $1, $2) ->
processor("\\begin{#{$1}}" + $2 + "\\end{#{$1}}")
text = text.replace(new RegExp(ESCAPED_BACKSLASH), '\\\\\\\\')
$div = $("<div>").html(text)
cnt = 0
$div.find("code").each (index, code) ->
$(code).html(processor(codeArchive[cnt], 'code'))
cnt += 1
$div.html()
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