Files
leanote/public/mdeditor/editor/Markdown.Extra-min.js
lealife 6987a38820 v1.0
只读模式
group, 分享
评论更多问题
博客标签总是存在一个
2015-06-15 18:01:48 +08:00

1 line
16 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(function(){var inlineTags=new RegExp(["^(<\\/?(a|abbr|acronym|applet|area|b|basefont|","bdo|big|button|cite|code|del|dfn|em|figcaption|","font|i|iframe|img|input|ins|kbd|label|map|","mark|meter|object|param|progress|q|ruby|rp|rt|s|","samp|script|select|small|span|strike|strong|","sub|sup|textarea|time|tt|u|var|wbr)[^>]*>|","<(br)\\s?\\/?>)$"].join(""),"i");if(!Array.indexOf){Array.prototype.indexOf=function(obj){for(var i=0;i<this.length;i++){if(this[i]==obj){return i}}return-1}}function trim(str){return str.replace(/^\s+|\s+$/g,"")}function rtrim(str){return str.replace(/\s+$/g,"")}function outdent(text){return text.replace(new RegExp("^(\\t|[ ]{1,4})","gm"),"")}function contains(str,substr){return str.indexOf(substr)!=-1}function sanitizeHtml(html,whitelist){return html.replace(/<[^>]*>?/gi,function(tag){return tag.match(whitelist)?tag:""})}function union(x,y){var obj={};for(var i=0;i<x.length;i++)obj[x[i]]=x[i];for(i=0;i<y.length;i++)obj[y[i]]=y[i];var res=[];for(var k in obj){if(obj.hasOwnProperty(k))res.push(obj[k])}return res}function addAnchors(text){if(text.charAt(0)!="")text=""+text;if(text.charAt(text.length-1)!="")text=text+"";return text}function removeAnchors(text){if(text.charAt(0)=="")text=text.substr(1);if(text.charAt(text.length-1)=="")text=text.substr(0,text.length-1);return text}function convertSpans(text,extra){return sanitizeHtml(convertAll(text,extra),inlineTags)}function convertAll(text,extra){var result=extra.blockGamutHookCallback(text);result=unescapeSpecialChars(result);result=result.replace(/~D/g,"$$").replace(/~T/g,"~");result=extra.previousPostConversion(result);return result}function processEscapesStep1(text){return text.replace(/\\\|/g,"~I").replace(/\\:/g,"~i")}function processEscapesStep2(text){return text.replace(/~I/g,"|").replace(/~i/g,":")}function unescapeSpecialChars(text){text=text.replace(/~E(\d+)E/g,function(wholeMatch,m1){var charCodeToReplace=parseInt(m1);return String.fromCharCode(charCodeToReplace)});return text}function slugify(text){return text.toLowerCase().replace(/\s+/g,"-").replace(/[^\w\-]+/g,"").replace(/\-\-+/g,"-").replace(/^-+/,"").replace(/-+$/,"")}Markdown.Extra=function(){this.converter=null;this.hashBlocks=[];this.footnotes={};this.usedFootnotes=[];this.attributeBlocks=false;this.googleCodePrettify=false;this.highlightJs=false;this.tableClass="";this.tabWidth=4};Markdown.Extra.init=function(converter,options){options=options||{};options.extensions=["fenced_code_gfm","tables","def_list","attr_list","footnotes","smartypants","strikethrough","newlines"];var extra=new Markdown.Extra;var postNormalizationTransformations=[];var preBlockGamutTransformations=[];var postSpanGamutTransformations=[];var postConversionTransformations=["unHashExtraBlocks"];options=options||{};options.extensions=options.extensions||["all"];if(contains(options.extensions,"all")){options.extensions=["tables","fenced_code_gfm","def_list","attr_list","footnotes","smartypants","strikethrough","newlines"]}preBlockGamutTransformations.push("wrapHeaders");if(contains(options.extensions,"attr_list")){postNormalizationTransformations.push("hashFcbAttributeBlocks");preBlockGamutTransformations.push("hashHeaderAttributeBlocks");postConversionTransformations.push("applyAttributeBlocks");extra.attributeBlocks=true}if(contains(options.extensions,"fenced_code_gfm")){preBlockGamutTransformations.push("fencedCodeBlocks");postNormalizationTransformations.push("fencedCodeBlocks")}if(contains(options.extensions,"tables")){preBlockGamutTransformations.push("tables")}if(contains(options.extensions,"def_list")){preBlockGamutTransformations.push("definitionLists")}if(contains(options.extensions,"footnotes")){postNormalizationTransformations.push("stripFootnoteDefinitions");preBlockGamutTransformations.push("doFootnotes");postConversionTransformations.push("printFootnotes")}if(contains(options.extensions,"smartypants")){postConversionTransformations.push("runSmartyPants")}if(contains(options.extensions,"strikethrough")){postSpanGamutTransformations.push("strikethrough")}if(contains(options.extensions,"newlines")){postSpanGamutTransformations.push("newlines")}converter.hooks.chain("postNormalization",function(text){return extra.doTransform(postNormalizationTransformations,text)+"\n"});converter.hooks.chain("preBlockGamut",function(text,blockGamutHookCallback){extra.blockGamutHookCallback=blockGamutHookCallback;text=processEscapesStep1(text);text=extra.doTransform(preBlockGamutTransformations,text)+"\n";text=processEscapesStep2(text);return text});converter.hooks.chain("postSpanGamut",function(text){return extra.doTransform(postSpanGamutTransformations,text)});extra.previousPostConversion=converter.hooks.postConversion;converter.hooks.chain("postConversion",function(text){text=extra.doTransform(postConversionTransformations,text);extra.hashBlocks=[];extra.footnotes={};extra.usedFootnotes=[];return text});if("highlighter"in options){extra.googleCodePrettify=options.highlighter==="prettify";extra.highlightJs=options.highlighter==="highlight"}if("table_class"in options){extra.tableClass=options.table_class}extra.converter=converter;return extra};Markdown.Extra.prototype.doTransform=function(transformations,text){for(var i=0;i<transformations.length;i++)text=this[transformations[i]](text);return text};Markdown.Extra.prototype.hashExtraBlock=function(block){return"\n<p>~X"+(this.hashBlocks.push(block)-1)+"X</p>\n"};Markdown.Extra.prototype.hashExtraInline=function(block){return"~X"+(this.hashBlocks.push(block)-1)+"X"};Markdown.Extra.prototype.unHashExtraBlocks=function(text){var self=this;function recursiveUnHash(){var hasHash=false;text=text.replace(/(?:<p>)?~X(\d+)X(?:<\/p>)?/g,function(wholeMatch,m1){hasHash=true;var key=parseInt(m1,10);return self.hashBlocks[key]});if(hasHash===true){recursiveUnHash()}}recursiveUnHash();return text};Markdown.Extra.prototype.wrapHeaders=function(text){function wrap(text){return"\n"+text+"\n"}text=text.replace(/^.+[ \t]*\n=+[ \t]*\n+/gm,wrap);text=text.replace(/^.+[ \t]*\n-+[ \t]*\n+/gm,wrap);text=text.replace(/^\#{1,6}[ \t]*.+?[ \t]*\#*\n+/gm,wrap);return text};var attrBlock="\\{[ \\t]*((?:[#.][-_:a-zA-Z0-9]+[ \\t]*)+)\\}";var hdrAttributesA=new RegExp("^(#{1,6}.*#{0,6})[ \\t]+"+attrBlock+"[ \\t]*(?:\\n|0x03)","gm");var hdrAttributesB=new RegExp("^(.*)[ \\t]+"+attrBlock+"[ \\t]*\\n"+"(?=[\\-|=]+\\s*(?:\\n|0x03))","gm");var fcbAttributes=new RegExp("^(```[^`\\n]*)[ \\t]+"+attrBlock+"[ \\t]*\\n"+"(?=([\\s\\S]*?)\\n```[ \\t]*(\\n|0x03))","gm");Markdown.Extra.prototype.hashHeaderAttributeBlocks=function(text){var self=this;function attributeCallback(wholeMatch,pre,attr){return"<p>~XX"+(self.hashBlocks.push(attr)-1)+"XX</p>\n"+pre+"\n"}text=text.replace(hdrAttributesA,attributeCallback);text=text.replace(hdrAttributesB,attributeCallback);return text};Markdown.Extra.prototype.hashFcbAttributeBlocks=function(text){var self=this;function attributeCallback(wholeMatch,pre,attr){return"<p>~XX"+(self.hashBlocks.push(attr)-1)+"XX</p>\n"+pre+"\n"}return text.replace(fcbAttributes,attributeCallback)};Markdown.Extra.prototype.applyAttributeBlocks=function(text){var self=this;var blockRe=new RegExp("<p>~XX(\\d+)XX</p>[\\s]*"+'(?:<(h[1-6]|pre)(?: +class="(\\S+)")?(>[\\s\\S]*?</\\2>))',"gm");text=text.replace(blockRe,function(wholeMatch,k,tag,cls,rest){if(!tag)return"";var key=parseInt(k,10);var attributes=self.hashBlocks[key];var id=attributes.match(/#[^\s#.]+/g)||[];var idStr=id[0]?' id="'+id[0].substr(1,id[0].length-1)+'"':"";var classes=attributes.match(/\.[^\s#.]+/g)||[];for(var i=0;i<classes.length;i++)classes[i]=classes[i].substr(1,classes[i].length-1);var classStr="";if(cls)classes=union(classes,[cls]);if(classes.length>0)classStr=' class="'+classes.join(" ")+'"';return"<"+tag+idStr+classStr+rest});return text};Markdown.Extra.prototype.tables=function(text){var self=this;var leadingPipe=new RegExp(["^","[ ]{0,3}","[|]","(.+)\\n","[ ]{0,3}","[|]([ ]*[-:]+[-| :]*)\\n","(","(?:[ ]*[|].*\\n?)*",")","(?:\\n|$)"].join(""),"gm");var noLeadingPipe=new RegExp(["^","[ ]{0,3}","(\\S.*[|].*)\\n","[ ]{0,3}","([-:]+[ ]*[|][-| :]*)\\n","(","(?:.*[|].*\\n?)*",")","(?:\\n|$)"].join(""),"gm");text=text.replace(leadingPipe,doTable);text=text.replace(noLeadingPipe,doTable);function doTable(match,header,separator,body,offset,string){header=header.replace(/^ *[|]/m,"");separator=separator.replace(/^ *[|]/m,"");body=body.replace(/^ *[|]/gm,"");header=header.replace(/[|] *$/m,"");separator=separator.replace(/[|] *$/m,"");body=body.replace(/[|] *$/gm,"");alignspecs=separator.split(/ *[|] */);align=[];for(var i=0;i<alignspecs.length;i++){var spec=alignspecs[i];if(spec.match(/^ *-+: *$/m))align[i]=' align="right"';else if(spec.match(/^ *:-+: *$/m))align[i]=' align="center"';else if(spec.match(/^ *:-+ *$/m))align[i]=' align="left"';else align[i]=""}var headers=header.split(/ *[|] */);var colCount=headers.length;var cls=self.tableClass?' class="'+self.tableClass+'"':"";var html=["<table",cls,">\n","<thead>\n","<tr>\n"].join("");for(i=0;i<colCount;i++){var headerHtml=convertSpans(trim(headers[i]),self);html+=[" <th",align[i],">",headerHtml,"</th>\n"].join("")}html+="</tr>\n</thead>\n";var rows=body.split("\n");for(i=0;i<rows.length;i++){if(rows[i].match(/^\s*$/))continue;var rowCells=rows[i].split(/ *[|] */);var lenDiff=colCount-rowCells.length;for(var j=0;j<lenDiff;j++)rowCells.push("");html+="<tr>\n";for(j=0;j<colCount;j++){var colHtml=convertSpans(trim(rowCells[j]),self);html+=[" <td",align[j],">",colHtml,"</td>\n"].join("")}html+="</tr>\n"}html+="</table>\n";return self.hashExtraBlock(html)}return text};Markdown.Extra.prototype.stripFootnoteDefinitions=function(text){var self=this;text=text.replace(/\n[ ]{0,3}\[\^(.+?)\]\:[ \t]*\n?([\s\S]*?)\n{1,2}((?=\n[ ]{0,3}\S)|$)/g,function(wholeMatch,m1,m2){m1=slugify(m1);m2+="\n";m2=m2.replace(/^[ ]{0,3}/g,"");self.footnotes[m1]=m2;return"\n"});return text};Markdown.Extra.prototype.doFootnotes=function(text){var self=this;if(self.isConvertingFootnote===true){return text}var footnoteCounter=0;text=text.replace(/\[\^(.+?)\]/g,function(wholeMatch,m1){var id=slugify(m1);var footnote=self.footnotes[id];if(footnote===undefined){return wholeMatch}footnoteCounter++;self.usedFootnotes.push(id);var html='<a href="#fn:'+id+'" id="fnref:'+id+'" title="See footnote" class="footnote">'+footnoteCounter+"</a>";return self.hashExtraInline(html)});return text};Markdown.Extra.prototype.printFootnotes=function(text){var self=this;if(self.usedFootnotes.length===0){return text}text+='\n\n<div class="footnotes">\n<hr>\n<ol>\n\n';for(var i=0;i<self.usedFootnotes.length;i++){var id=self.usedFootnotes[i];var footnote=self.footnotes[id];self.isConvertingFootnote=true;var formattedfootnote=convertSpans(footnote,self);delete self.isConvertingFootnote;text+='<li id="fn:'+id+'">'+formattedfootnote+' <a href="#fnref:'+id+'" title="Return to article" class="reversefootnote">&#8617;</a></li>\n\n'}text+="</ol>\n</div>";return text};Markdown.Extra.prototype.fencedCodeBlocks=function(text){function encodeCode(code){code=code.replace(/&/g,"&amp;");code=code.replace(/</g,"&lt;");code=code.replace(/>/g,"&gt;");code=code.replace(/~D/g,"$$");code=code.replace(/~T/g,"~");return code}var self=this;text=text.replace(/(?:^|\n)```([^`\n]*)\n([\s\S]*?)\n```[ \t]*(?=\n)/g,function(match,m1,m2){var language=trim(m1),codeblock=m2;var preclass=self.googleCodePrettify?' class="prettyprint"':"";var codeclass="";if(language){if(self.googleCodePrettify||self.highlightJs){codeclass=' class="language-'+language+'"'}else{codeclass=' class="'+language+'"'}}var html=["<pre",preclass,"><code",codeclass,">",encodeCode(codeblock),"</code></pre>"].join("");return self.hashExtraBlock(html)});return text};Markdown.Extra.prototype.educatePants=function(text){var self=this;var result="";var blockOffset=0;text.replace(/(?:<!--[\s\S]*?-->)|(<)([a-zA-Z1-6]+)([^\n]*?>)([\s\S]*?)(<\/\2>)/g,function(wholeMatch,m1,m2,m3,m4,m5,offset){var token=text.substring(blockOffset,offset);result+=self.applyPants(token);self.smartyPantsLastChar=result.substring(result.length-1);blockOffset=offset+wholeMatch.length;if(!m1){result+=wholeMatch;return}if(!/code|kbd|pre|script|noscript|iframe|math|ins|del|pre/i.test(m2)){m4=self.educatePants(m4)}else{self.smartyPantsLastChar=m4.substring(m4.length-1)}result+=m1+m2+m3+m4+m5});var lastToken=text.substring(blockOffset);result+=self.applyPants(lastToken);self.smartyPantsLastChar=result.substring(result.length-1);return result};function revertPants(wholeMatch,m1){var blockText=m1;blockText=blockText.replace(/&\#8220;/g,'"');blockText=blockText.replace(/&\#8221;/g,'"');blockText=blockText.replace(/&\#8216;/g,"'");blockText=blockText.replace(/&\#8217;/g,"'");blockText=blockText.replace(/&\#8212;/g,"---");blockText=blockText.replace(/&\#8211;/g,"--");blockText=blockText.replace(/&\#8230;/g,"...");return blockText}Markdown.Extra.prototype.applyPants=function(text){text=text.replace(/---/g,"&#8212;").replace(/--/g,"&#8211;");text=text.replace(/\.\.\./g,"&#8230;").replace(/\.\s\.\s\./g,"&#8230;");text=text.replace(/``/g,"&#8220;").replace(/''/g,"&#8221;");if(/^'$/.test(text)){if(/\S/.test(this.smartyPantsLastChar)){return"&#8217;"}return"&#8216;"}if(/^"$/.test(text)){if(/\S/.test(this.smartyPantsLastChar)){return"&#8221;"}return"&#8220;"}text=text.replace(/^'(?=[!"#\$\%'()*+,\-.\/:;<=>?\@\[\\]\^_`{|}~]\B)/,"&#8217;");text=text.replace(/^"(?=[!"#\$\%'()*+,\-.\/:;<=>?\@\[\\]\^_`{|}~]\B)/,"&#8221;");text=text.replace(/"'(?=\w)/g,"&#8220;&#8216;");text=text.replace(/'"(?=\w)/g,"&#8216;&#8220;");text=text.replace(/'(?=\d{2}s)/g,"&#8217;");text=text.replace(/(\s|&nbsp;|--|&[mn]dash;|&\#8211;|&\#8212;|&\#x201[34];)'(?=\w)/g,"$1&#8216;");text=text.replace(/([^\s\[\{\(\-])'/g,"$1&#8217;");text=text.replace(/'(?=\s|s\b)/g,"&#8217;");text=text.replace(/'/g,"&#8216;");text=text.replace(/(\s|&nbsp;|--|&[mn]dash;|&\#8211;|&\#8212;|&\#x201[34];)"(?=\w)/g,"$1&#8220;");text=text.replace(/([^\s\[\{\(\-])"/g,"$1&#8221;");text=text.replace(/"(?=\s)/g,"&#8221;");text=text.replace(/"/gi,"&#8220;");return text};Markdown.Extra.prototype.runSmartyPants=function(text){this.smartyPantsLastChar="";text=this.educatePants(text);text=text.replace(/(<([a-zA-Z1-6]+)\b([^\n>]*?)(\/)?>)/g,revertPants);return text};Markdown.Extra.prototype.definitionLists=function(text){var wholeList=new RegExp(["(\\x02\\n?|\\n\\n)","(?:","(","(","[ ]{0,3}","((?:[ \\t]*\\S.*\\n)+)","\\n?","[ ]{0,3}:[ ]+",")","([\\s\\S]+?)","(","(?=\\0x03)","|","(?=","\\n{2,}","(?=\\S)","(?!","[ ]{0,3}","(?:\\S.*\\n)+?","\\n?","[ ]{0,3}:[ ]+",")","(?!","[ ]{0,3}:[ ]+",")",")",")",")",")"].join(""),"gm");var self=this;text=addAnchors(text);text=text.replace(wholeList,function(match,pre,list){var result=trim(self.processDefListItems(list));result="<dl>\n"+result+"\n</dl>";return pre+self.hashExtraBlock(result)+"\n\n"});return removeAnchors(text)};Markdown.Extra.prototype.processDefListItems=function(listStr){var self=this;var dt=new RegExp(["(\\x02\\n?|\\n\\n+)","(","[ ]{0,3}","(?![:][ ]|[ ])","(?:\\S.*\\n)+?",")","(?=\\n?[ ]{0,3}:[ ])"].join(""),"gm");var dd=new RegExp(["\\n(\\n+)?","(","[ ]{0,3}","[:][ ]+",")","([\\s\\S]+?)","(?=\\n*","(?:","\\n[ ]{0,3}[:][ ]|","<dt>|\\x03",")",")"].join(""),"gm");listStr=addAnchors(listStr);listStr=listStr.replace(/\n{2,}(?=\\x03)/,"\n");listStr=listStr.replace(dt,function(match,pre,termsStr){var terms=trim(termsStr).split("\n");var text="";for(var i=0;i<terms.length;i++){var term=terms[i];term=convertSpans(trim(term),self);text+="\n<dt>"+term+"</dt>"}return text+"\n"});listStr=listStr.replace(dd,function(match,leadingLine,markerSpace,def){if(leadingLine||def.match(/\n{2,}/)){def=Array(markerSpace.length+1).join(" ")+def;def=outdent(def)+"\n\n";def="\n"+convertAll(def,self)+"\n"}else{def=rtrim(def);def=convertSpans(outdent(def),self)}return"\n<dd>"+def+"</dd>\n"});return removeAnchors(listStr)};Markdown.Extra.prototype.strikethrough=function(text){return text.replace(/([\W_]|^)~T~T(?=\S)([^\r]*?\S[\*_]*)~T~T([\W_]|$)/g,"$1<del>$2</del>$3")};Markdown.Extra.prototype.newlines=function(text){return text.replace(/(<(?:br|\/li)>)?\n/g,function(wholeMatch,previousTag){return previousTag?wholeMatch:" <br>\n"})}})();