-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmdjs.min.js
2 lines (2 loc) · 10 KB
/
mdjs.min.js
1
2
(function(){function a(a,b){!String.prototype[a]&&(String.prototype[a]=b);}a('trim',function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,'');}),a('startsWith',function(a,b){return b=b||0,this.substr(b,a.length)===a;}),a('endsWith',function(a,b){var c=this.toString();(typeof b!=='number'||!isFinite(b)||Math.floor(b)!==b||b>c.length)&&(b=c.length),b-=a.length;var d=c.lastIndexOf(a,b);return d!==-1&&d===b;});})();
(function(){function a(a){return a<0?'':a<1024?v.slice(0,a):u(a);}function b(a){try{if(a!==decodeURIComponent(a))return a;}catch(a){}return encodeURI(a);}function c(a){var b=t.exec(a);if(!b)return a;var c='',d='',e=0,f=0,g=a.length;for(e=b.index;e<g;e++){switch(a.charCodeAt(e)){case 34:d='"';break;case 38:d='&';break;case 39:d=''';break;case 60:d='<';break;case 62:d='>';break;default:continue;}f!==e&&(c+=a.substring(f,e)),f=e+1,c+=d;}return f===e?c:c+a.substring(f,e);}function d(a){var b={},c=[];this.set=function(d,e,f){f&&(e.id=c.push(e),e.url=a.func.footNoteName(e.id)),b[d.toLowerCase()]=e;},this.get=function(a){return b[a.toLowerCase()];},this.getFootNotes=function(){return c;};}function e(){var a=[],b=[],c=0;this.topLevel=function(){return c?a[c-1]:-1;},this.topType=function(){return c?b[c-1]:-1;},this.push=function(d,e){a.push(d),b.push(e),c++;},this.pop=function(){a.pop(),b.pop(),c=a.length;};}function f(){this.tag={hr:'<hr />',br:'<br />',p:['<p>','</p>'],quote:['<blockquote>','</blockquote>'],del:['<del>','</del>'],strong:['<strong>','</strong>'],em:['<em>','</em>'],inlineCode:['<code>','</code>'],codeBlock:['<pre><code data-lang="$language">','</code></pre>'],list:['<ul>','</ul>'],orderList:['<ol>','</ol>'],listItem:['<li>','</li>'],toc:['<div class="md_toc">','</div>'],tocList:['<ol>','</ol>'],tocItem:['<a href="#$uri"><li>','</li></a>'],footNote:['<div class="md_foot"><ol>','</ol></div>']},this.func={heading:function(a,b,c){return"<h"+a+" id=\""+b+"\" name=\""+b+"\">"+c+"</h"+a+">";},link:function(a,d,e){return"<a title=\""+c(d)+"\" href=\""+b(a)+"\">"+e+"</a>";},email:function(a){return"<a href=\"mailto:"+a+"\">"+a+"</a>";},image:function(a,d,e){return"<img alt=\""+c(e)+"\" title=\""+c(d)+"\" src=\""+b(a)+"\" />";},table:function(a,b){return"<table class=\"md_table\"><thead>"+a+"</thead><tbody>"+b+"</tbody></table>";},tableRow:function(a,b,c){for(var d='<tr>',e=a?'<th style="text-align: {0}">':'<td style="text-align: {0}">',f=a?'</th>':'</td>',g=['left','center','right'],h=0;h<b.length;h++)d+=e.replace('{0}',g[c[h]])+b[h]+f;return d+'</tr>';},footNoteLink:function(a,d,e){return"<sup><a title=\""+c(d)+"\" href=\"#"+b(a)+"\">"+e+"</a></sup>";},footNote:function(a,b){return"<li name=\""+a+"\" id=\""+a+"\">"+b+"</li>";},footNoteName:function(a){return"markdown_foot_"+a;}};var a=[];this.addRefLinkProvider=function(b){return a.push(b);},this._resolveRefLink=function(b){for(var c,d=0;d<a.length;d++)if(c=a[d](b))return c.url?c:{url:c};};}function g(b){function g(a){a=a.trim();var b={url:a,title:''},c=a.match(/(.+?)\s+(.+)/);if(!c)return b;b.url=c[1];var d=c[2],e=d[0],f=d[d.length-1];return d.length>=2&&((e=='\''||e=='"')&&e==f||e=='('&&f==')')?(b.title=d.slice(1,-1),b):(b.url=a,b);}function h(a){var b=a.charCodeAt(0);if(b!=61&&b!=45&&b!=42&&b!=95)return!1;var c=0,d=0;for(var e=0;e<a.length;e++,c++)if(d=a.charCodeAt(e),d!=32&&d!=9&&d!=b)return!1;return c>=3;}function t(a){for(var b=0,c=0;c<a.length;c++)if(a[c]==' ')b++;else if(a[c]=='\t')b+=4;else break;return b;}function u(a){var b=0;for(b=0;b<a.length&&!(a[b]!='#');b++);return b;}function v(a){return a.endsWith(' ');}function w(a){return a.replace(o,'_').replace(/^_/,'').replace(/_$/,'');}function x(a){return K.get(a)||H._resolveRefLink(a);}function y(b,d,e){for(var f='',g=b.length,l=0,m='',o='',p=0,q=-1,r=[],x=[],G=[],H=!0,K=!1,L='',M={},N=0,O=0;O<g;O++){if(m=b[O],o=m.trim(),l){if(o=='```'){l=0,f+=I.codeBlock[1];continue;}f+=(l++>1?'\n':'')+c(m);continue;}p=t(m);var P=A(o);if(P!=0){f+=B(p,P,o);continue;}if(f+=C(),o.length==0){!H&&(f+=(K?I.br:'')+I.p[1],H=!0);continue;}if(p<4){if(o.startsWith('```')){var Q=o.slice(3).trim();f+=I.codeBlock[0].replace(s,Q),l=1;continue;}if(N=u(o),N>0){for(var R=o.length-1;R>N&&o[R]=='#';R--);var S=o.slice(N,R+1),T=S=F(S,0);G.push(N),r.push(T=T.trim().replace(n,'')),x.push(T=w(T)),f+=J.heading(N,T,S);continue;}if(o[0]=='>'&&o.length>1){for(var U=[],V=O;V<g&&(L=b[V].trim(),L.length!=0);V++){if(L[0]=='>')L=L.slice(1)+(v(b[V])?' ':'');else if(d)break;else L=b[V];U.push(L);}f+=I.quote[0]+y(U,!0,e)+I.quote[1],O=V-1;continue;}if(h(o)){f+=I.hr;continue;}if(o=='[TOC]'){q=f.length;continue;}var ca=E(o);if(ca!==!1&&O<g-1){var da=D(b[O+1].trim(),ca.length);if(da!==!1){for(var W=J.tableRow(!0,ca,da),X='',Y=O+2;Y<g&&(ca=E(b[Y].trim()))!=!1;Y++)X+=J.tableRow(!1,ca,da);O=Y-1,f+=J.table(W,X);continue;}}}else if(O==0||b[O-1].trim().length==0){f+=I.codeBlock[0].replace(s,'');for(var Z,$='',_=O,Y=O;Y<g;Y++){if(b[Y].trim().length==0){$+='\n';continue;}if((Z=t(b[Y]))<4)break;f+=$+(Y==O?'':'\n')+a(Z-2)+c(b[Y].trim()),$='',_=Y;}f+=I.codeBlock[1],O=_;continue;}if(O+1<g){var aa=b[O+1].trim();if(h(aa)){var ba=3;aa[0]=='='?ba=1:aa[0]=='-'&&(ba=2);var T=S=F(o,0);G.push(ba),r.push(T=T.trim().replace(n,'')),x.push(T=w(T)),f+=J.heading(ba,T,S),O++;continue;}}M={},L=F(m,0,M),H&&M.onlyOneImg?f+=L:(H&&(L=I.p[0]+L,K=!1),(e.alwaysNewline&&!H||K)&&(f+=I.br),K=v(m),f+=L,H=!1);}return!H&&(f+=(K?I.br:'')+I.p[1],H=!0),q!=-1&&(f=f.slice(0,q)+z(r,x,G)+f.slice(q)),f;}function z(a,b,c){for(var d,e,f=I.toc[0],g=[],h=0;h<a.length;h++)e=I.tocItem[0].replace('$uri',b[h])+a[h]+I.tocItem[1],g.length==0||c[h]>d?(f+=I.tocList[0]+e,g.push(d=c[h])):c[h]==d?f+=e:(f+=I.tocList[1],g.pop(),d=g[g.length-1],h--);for(;g.length;)f+=I.tocList[1],g.pop();return f+I.toc[1];}function A(a){return h(a)?0:a.search(m)==-1?a.search(l)==-1?0:2:1;}function B(a,b,c){var d=L.topLevel(),e=I.listItem[0]+F(c,c.indexOf(' '))+I.listItem[1],f='';if(a>d)return L.push(a,b),(b==1?I.orderList:I.list)[0]+e;if(a==d)return e;for(;a<d;)f+=L.topType()==1?I.orderList[1]:I.list[1],L.pop(),d=L.topLevel();return d==-1?(L.push(a,b),f+(b==1?I.orderList:I.list)[0]+e):f+e;}function C(){for(var a='';L.topLevel()!=-1;)a+=L.topType()==1?I.orderList[1]:I.list[1],L.pop();return a;}function D(a,b){var c=E(a,!0),d=[],e=0,f=0;if(c===!1)return!1;for(var g=c.length;e<g;e++,f=0){if(c[e].length<=1){d[e]=0;continue;}c[e][c[e].length-1]==':'&&(f=c[e][0]==':'?1:2),d[e]=f;}for(;e<b;e++)d[e]=0;return d;}function E(a,b){for(var c=[],d=a.length,e='',f=a[0]=='|'?1:0;f<d;f++){switch(a[f]){case'\\':if(b)return!1;e+='\\',a[f+1]=='|'&&(e+='|',f++);continue;case'|':if(e=e.trim(),b&&e.length==0)return!1;c.push(e),e='';continue;}if(!b||a[f]==':'||a[f]=='-'||a[f]==' '||a[f]=='\t')e+=a[f];else return!1;}return(c.length!=0||a[0]=='|')&&(e=e.trim(),e.length!=0&&c.push(e),c);}function F(a,b,d){for(var e,f,h,l,m,n,o,s=a.length,t=[],u='',v=-1,w=-1,y=-1,z=-1,A=-1,B='*',C='*',D='l',E=0,G=!1,H=b||0;H<s;H++)switch(a[H]){case'\\':k.indexOf(a[H+1])>=0&&(v=t.length,w=++H),u+=a[H];break;case'`':m=a[H+1]=='`'?'``':'`',n=m.length,(e=a.indexOf(m,H+n))==-1?u+=m:(u+=I.inlineCode[0]+c(a.slice(H+n,e))+I.inlineCode[1],H=e),H+=n-1;break;case'~':a[H+1]=='~'?(A>=0?u==''?t[A]+='~~':(t[A]+=I.del[0],u+=I.del[1],A=-1):(A=t.push(u)-1,u=''),H++):u+='~';break;case'*':case'_':if((a[H+1]==' '||a[H+1]=='\t')&&(a[H-1]==' '||a[H-1]=='\t')){u+=a[H];break;}if(a[H+1]==a[H]){if(y>=0){if(B!=a[H]){u+=a[H++]+a[H];break;}t[y]+=I.strong[0],u+=I.strong[1],y=-1;}else a[H+2]==a[H]&&a[H+3]==a[H]&&(u+=a[H++]+a[H++]),y=t.push(u)-1,u='',B=a[H];H++;}else if(z>=0){if(C!=a[H]){u+=a[H];break;}t[z]+=I.em[0],u+=I.em[1],z=-1;}else z=t.push(u)-1,u='',C=a[H];break;case'<':if(a.slice(H+1,H+4)=='!--'){u+='<!--',H+=3;break;}for(G=!0,e=H+1;e<s&&a[e]!='>';e++)(a[e]==' '||a[e]=='\t')&&(G=!1);if(e>=s){u+='<';break;}if(m=a.slice(H+1,e),G){if(m.match(p)){u+=J.link(m,'',m),H=e;break;}if(m.match(q)){u+=J.email(m),H=e;break;}}u+='<';break;case'!':a[H+1]!='['&&(u+='!');break;case'[':{D=a[H-1]=='!'&&(v!=t.length||w!=H-1)?'i':a[H+1]=='^'?'f':'l';for(var L=0,M=0,N=H+1;N<s;N++)switch(a[N]){case'!':if(a[N+1]!='[')break;D=='l'?(L=1,N++):N=s;break;case'`':m=a[N+1]=='`'?'``':'`',n=m.length,(e=a.indexOf(m,N+n))==-1?N+=n-1:N=e+n-1;break;case'[':N=s;break;case']':if(f=a.slice(H+1,N),D=='f'){o=K.get(f),o&&(u+=J.footNoteLink(o.url,o.title,o.id),M=1,H=N,N=s);break;}var R=void 0;if(m=a[N+1],m=='(')R=')';else if(m=='['||m==' '&&a[N+2]=='[')R=']';else{o=x(f),o&&(u+=J.link(o.url,o.title,F(f,0)),M=1,H=N),N=s;break;}if(n=m==' '?N+3:N+2,(e=a.indexOf(R,n))!=-1){if(L){L=0;break;}var O=a.slice(n,e).trim();if(R!=']')o=g(O);else if(O.length==0&&(O=f),o=x(O),!o){N=s;break;}h=o.url,l=o.title||'',D=='i'?(t.push(u),t.push(J.image(h,l,f)),u='',E++):u+=J.link(h,l,F(f,0)),M=1,H=e;}N=s;}!M&&N>=s&&(D==='f'?(u+='[^',H++):D==='i'?u+='![':u+='[');break;}default:u+=a[H];}if(t.push(u),A!=-1&&(t[A]+='~~'),y!=-1&&(t[y]+=B+B),z!=-1&&(t[z]+=C),E==1){var P=0;for(var Q in t)P+=t[Q].trim()?1:0;P==1&&d&&(d.onlyOneImg=!0);}return t.join('');}function G(){var a=K.getFootNotes();if(a.length==0)return'';var b=I.footNote[0];return a.forEach(function(a){return b+=J.footNote(a.url,F(a.content,0));}),b+I.footNote[1];}var H=new f();b&&(H=b);var I=H.tag,J=H.func,K=new d(H),L=new e();this.render=H,this.md2html=function(a,b){b=b||{},a=typeof a=='string'?a:String(a),K=new d(H),L=new e();for(var c=a.split(/\r?\n/),f=c.length,h=[],j='',l=0;l<f;l++){j=c[l];var m=j.trim().match(r);if(!m){h.push(j);continue;}var n,o=!1,p='';if(o=m[1]=='^'){p=m[3];for(var q=l+1;q<f&&(j=c[q].trim(),!!j)&&!j.match(r);q++,l++)p+='\n'+c[q];n={title:m[2],content:p};}else n=g(m[3].trim());K.set(m[1]+m[2],n,o);}return y(h,!1,b)+G();};}var h="Mdjs",i=function(a,b){return typeof a.prototype[b]==='function';},k="#`*[]()-_{}+.!|\\",l=/^[\*\-\+] +\S*/g,m=/^\d+\. +\S*/g,n=/<\/?[^<>]+>/g,o=/\W+/g,p=/^\w+:\/{2,3}\S+$/g,q=/^\S+@\S+\.\S+$/g,r=/^\[([\^]?)(.+)\]\:\s+(.+)$/,s=/\$language/g,t=/[<>"'&]/,u=i(String,'repeat')?function(a){return' '.repeat(a);}:function(a){return new Array(a).join(' ');},v=u(1024),w=new g();g.md2html=function(a,b){return w.md2html(a,b);},g.escapedHTML=c,g.MdjsRenderer=f,g.Mdjs=g,typeof module=='object'&&typeof global=='object'&&(module.exports=global[h]=g),typeof window!='undefined'&&(window[h]=g);})();