|
| 1 | +;(function() { |
| 2 | + MathJax.Hub.Config({ |
| 3 | + showProcessingMessages: false, |
| 4 | + tex2jax: { |
| 5 | + inlineMath: [['$','$']], |
| 6 | + displayMath: [['$$', '$$']], |
| 7 | + ignoreClass: ".*", |
| 8 | + processClass: "mathjax" |
| 9 | + }, |
| 10 | + TeX: { |
| 11 | + equationNumbers: { |
| 12 | + autoNumber: "AMS" |
| 13 | + } |
| 14 | + } |
| 15 | + }); |
| 16 | + |
| 17 | + marked.setOptions({ |
| 18 | + renderer: new marked.Renderer(), |
| 19 | + gfm: true, |
| 20 | + tables: true, |
| 21 | + breaks: false, |
| 22 | + pedantic: false, |
| 23 | + sanitize: false, |
| 24 | + smartLists: true, |
| 25 | + smartypants: false, |
| 26 | + highlight: function (code) { |
| 27 | + return hljs.highlightAuto(code).value; |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + function EscapeTex(text) { |
| 32 | + var re = /(`+)(\s*)([\s\S]*?[^`])(\s*)(\1)(?!`)/g; |
| 33 | + var out = text.replace(re, function(m, p1, p2, p3, p4, p5, offset, string) { |
| 34 | + return p1 + p2 + p3.replace(/\$/g, '\\$') + p4 + p5; |
| 35 | + }); |
| 36 | + |
| 37 | + re = /^( {4}[^\n]+\n*)+/g; |
| 38 | + out = out.replace(re, function(m, p1, offset, string) { |
| 39 | + return p1.replace(/\$/g, '\\$'); |
| 40 | + }); |
| 41 | + |
| 42 | + re = /([^\\\$]|^)(\${1,2})(?!\$)(\s*)([\s\S]*?[^$])(\s*)(\2)(?!\2)/g; |
| 43 | + out = out.replace(re, function(m, p1, p2, p3, p4, p5, p6, offset, string) { |
| 44 | + return p1 + p2 + p3 + p4.replace(/(.)/g, '\\$1') + p5 + p6; |
| 45 | + }); |
| 46 | + |
| 47 | + return out; |
| 48 | + }, |
| 49 | + |
| 50 | + function ReEscapeTex(text) { |
| 51 | + var re = /([^\\\$]|^)(\${1,2})(?!\$)(\s*)([\s\S]*?[^$])(\s*)(\2)(?!\2)/g; |
| 52 | + var out = text.replace(re, function(m, p1, p2, p3, p4, p5, p6, offset, string) { |
| 53 | + return p1 + p2 + p3 + p4.replace(/\\(.)/g, '$1') + p5 + p6; |
| 54 | + }); |
| 55 | + |
| 56 | + return out; |
| 57 | + } |
| 58 | + |
| 59 | + function PreviewDone() { |
| 60 | + this.isRunning[index] = false; |
| 61 | + Preview.forceUpdate = false; |
| 62 | + this.preview[index].innerHTML = this.buffer[index].innerHTML; |
| 63 | + } |
| 64 | + |
| 65 | + function markjax(text, callback){ |
| 66 | + var src = text.replace(/</mg, '<').replace(/>/mg, '>'); |
| 67 | + |
| 68 | + var html = this.ReEscapeTex(marked(this.EscapeTex(src))); |
| 69 | + var code = $(html).find("code"); |
| 70 | + for (var i = 0; i < code.length; i++) { |
| 71 | + code[i].innerHTML = code[i].innerHTML.replace(/\\\$/g, '$'); |
| 72 | + } |
| 73 | + $(html).find("*").not("code").addClass("mathjax"); |
| 74 | + |
| 75 | + MathJax.Hub.Queue( |
| 76 | + ["Typeset", MathJax.Hub, html], |
| 77 | + [callback, html], |
| 78 | + ["resetEquationNumbers", MathJax.InputJax.TeX] |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + if (typeof module !== 'undefined' && typeof exports === 'object') { |
| 83 | + module.exports = markjax; |
| 84 | + } else if (typeof define === 'function' && define.amd) { |
| 85 | + define(function() { return markjax; }); |
| 86 | + } else { |
| 87 | + this.markjax = markjax; |
| 88 | + } |
| 89 | +}).call(function() { |
| 90 | + return this || (typeof window !== 'undefined' ? window : global); |
| 91 | +}()); |
0 commit comments