mathjax-parser
Version:
Find & Replace the mathjax delimiters in a HTML string
1 lines • 3.02 kB
JavaScript
var MathjaxParser=function(){function a(){var a=this;this.parse=function(b,c){a.config=c||{inlineMath:[["$","$"],["\\(","\\)"]],displayMath:[["$$","$$"],["\\[","\\]"]],inlineMathReplacement:["XXX","XXX"],displayMathReplacement:["YYY","YYY"]};var d=document.createElement("body");return d.innerHTML=b,a.processNodeList(d.childNodes,a.buildDelimiterArray(c)),{outputHtml:d.innerHTML}},this.processNodeList=function(b,c){var d=a.findAdjacentTextOrBrNodes(b);d.forEach(function(d){a.iterateMath(c,d,b)});for(var e=0;e<b.length;e++){var f=b[e];3!==f.nodeType&&a.processNodeList(f.childNodes,c)}},this.isMatchingIndex=function(a,b,c){return a.substr(b,c.length)===c},this.replaceStartAndEndOfMatchedSet=function(b,c){a.replaceDelimiters(c,b.end),a.replaceDelimiters(c,b.start)},this.cleanOccurrences=function(a){a.length>0&&(a[a.length-1].end||a.pop())},this.replaceDelimiters=function(b,c){var d=c.isStart?c.delimiterGroup.group[0].length:c.delimiterGroup.group[1].length,e=b[c.nodeNumber].nodeValue;b[c.nodeNumber].nodeValue=e.substr(0,c.index)+a.config[c.delimiterGroup.type+"MathReplacement"][c.isStart?0:1]+e.substr(c.index+d,e.length-1)},this.findAdjacentTextOrBrNodes=function(b){for(var c=[],d=0;d<b.length;d++){var e=b[d];a.isTextOrBrNode(e)?c.push(!0):c.push(!1)}for(var f=[],d=0;d<c.length;d++){var g=c[d];g&&(0===f.length||f[f.length-1].end!==d?f.push({start:d,end:d+1}):f[f.length-1].end===d&&++f[f.length-1].end)}return f},this.isTextOrBrNode=function(a){return 3===a.nodeType||"BR"===a.nodeName}}return a.prototype.buildDelimiterArray=function(a){var b=[],c=function(a,b,c,d){b.splice(a,0,{group:c,type:d})},d=function(a,b){for(var c=0,d=0;d<a.length&&!(b.indexOf(a[d].group[0])>-1);d++)++c;return c};return a.inlineMath.forEach(function(a){var e=d(b,a[0]);c(e,b,a,"inline")}),a.displayMath.forEach(function(a){var e=d(b,a[0]);c(e,b,a,"display")}),b},a.prototype.iterateMath=function(a,b,c){for(var d={matchedDelimiterSets:[]},e=b.start;e<b.end;e++){var f=c[e];if(3===f.nodeType){var g=f.textContent;this.processIndices(g,d,a,e)}}this.cleanOccurrences(d.matchedDelimiterSets),this.replaceMatches(d.matchedDelimiterSets,c)},a.prototype.replaceMatches=function(a,b){var c=this;a=a.reverse(),a.forEach(function(a){c.replaceStartAndEndOfMatchedSet(a,b)})},a.prototype.processIndices=function(b,c,d,e){for(var f=this,g=0,h=function(){if(0===c.matchedDelimiterSets.length||c.matchedDelimiterSets[c.matchedDelimiterSets.length-1].end){var h=!1;d.some(function(d){if(f.isMatchingIndex(b,g,d.group[0]))return c.lastMatchedGroup=d,a.pushStart(c.matchedDelimiterSets,e,g,d),h=!0,!0}),h?g+=c.lastMatchedGroup.group[0].length:++g}else i.isMatchingIndex(b,g,c.lastMatchedGroup.group[1])?(a.pushEnd(c.matchedDelimiterSets,e,g,c.lastMatchedGroup),g+=c.lastMatchedGroup.group[1].length):++g},i=this;g<b.length;)h()},a.pushStart=function(a,b,c,d){a.push({start:{nodeNumber:b,index:c,delimiterGroup:d,isStart:!0},end:void 0})},a.pushEnd=function(a,b,c,d){a[a.length-1].end={nodeNumber:b,index:c,delimiterGroup:d,isStart:!1}},a}();