forked from commenthol/markedpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markedpp.min.js
12 lines (12 loc) · 24.9 KB
/
markedpp.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*!
* markedpp
*
* A preprocessor for markdown files
*
* @copyright 2014 commenthol
* @licence MIT
*
* @note Code inspired by `marked` project
* @credits Christopher Jeffrey <https://github.com/chjj/marked>
*/
(function(e){"use strict";var t={gfm:true,include:true,toc:true,numberedheadings:true,ref:true,breaks:true,tags:true,level:3,minlevel:1,autonumber:true,autoid:true,githubid:false};function n(){return typeof process!=="undefined"&&typeof module!=="undefined"&&module.exports&&!process.browser}function i(e,t){e=e.source;t=t||"";return function n(i,r){if(!i)return new RegExp(e,t);r=r.source||r;r=r.replace(/(^|[^\[])\^/g,"$1");e=e.replace(i,r);return n}}function r(){}r.exec=r;function s(e){var t=1,n,i;for(;t<arguments.length;t++){n=arguments[t];for(i in n){if(Object.prototype.hasOwnProperty.call(n,i)){e[i]=n[i]}}}return e}function o(e,t){var n=parseInt(e,10);if(isNaN(n)){if(t===true)return;else return e}return n}function p(e,t){t=t||1;var n="";for(var i=0;i<t;i++){n+=e}return n}var u={normalizeArray:function(e,t){var n=0;for(var i=e.length-1;i>=0;i--){var r=e[i];if(r==="."){e.splice(i,1)}else if(r===".."){e.splice(i,1);n++}else if(n){e.splice(i,1);n--}}if(t){for(;n--;n){e.unshift("..")}}return e},resolve:function(){var e="",t=false;for(var n=arguments.length-1;n>=-1&&!t;n--){var i=n>=0?arguments[n]:"/";if(!i){continue}e=i+"/"+e;t=i.charAt(0)==="/"}e=this.normalizeArray(e.split("/").filter(function(e){return!!e}),!t).join("/");return(t?"/":"")+e||"."},splitPathRe:/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,splitPath:function(e){return this.splitPathRe.exec(e).slice(1)},dirname:function(e){var t=this.splitPath(e),n=t[0],i=t[1];if(!n&&!i){return"."}if(i){i=i.substr(0,i.length-1)}return n+i},join:function(){return Array.prototype.slice.call(arguments).join("/")}};var l={_eachLimit:function(e){return function(t,n,i){i=i||function(){};if(!t.length||e<=0){return i()}var r=0;var s=0;var o=0;(function p(){if(r>=t.length){return i()}while(o<e&&s<t.length){s+=1;o+=1;n(t[s-1],function(e){if(e){i(e);i=function(){}}else{r+=1;o-=1;if(r>=t.length){i()}else{p()}}})}})()}},eachLimit:function(e,t,n,i){var r=this._eachLimit(t);r.apply(null,[e,n,i])}};function a(e,t,n){if(typeof t==="function"){n=t;t=null}var i=t?t:{};var r=new XMLHttpRequest,s=i.method||"get",o=i.data||null,p;r.queryString=o;r.open(s,e,true);r.setRequestHeader("X-Requested-With","XMLHttpRequest");if(s.toLowerCase()=="post"){r.setRequestHeader("Content-Type","application/x-www-form-urlencoded")}for(p in i.headers){if(i.headers.hasOwnProperty(p)){r.setRequestHeader(p,i.headers[p])}}function u(){if(r.readyState===4){if(/^[20]/.test(r.status)){n(null,r.responseText)}else if(/^[45]/.test(r.status)){n(new Error(r.status))}}}r.onreadystatechange=u;try{r.send(o)}catch(l){console.log("Error: Resolving URL "+e);n(l)}}var h;if(n()){h=require("./ppinclude")(d,s)}else{h=function(e,t,n){var i=t.dirname||u.dirname(location.pathname),r={},o=s({},t);o.tags=false;if(!o.ppInclude){o.ppInclude={}}l.eachLimit(e,5,function(e,t){if(e.type==="ppinclude"&&typeof e.text==="string"&&!o.ppInclude[e.text]){var n=u.resolve(u.join(i,e.text));var s=location.protocol+"//"+location.host+n;a(s,function(i,s){o.ppInclude[e.text]=1;o.dirname=u.dirname(n);if(i){console.log("Error: "+i.message);return t()}var p=new d(o);var l="\n"+e.indent;s=e.indent+s.split("\n").join(l);if(s.substr(0-l.length)===l){s=s.substr(0,s.length-l.length+1)}h(p.lex(s),o,function(n,i){if(n){}r[e.text]=i;t()})})}else{t()}},function(){var i=[];e.forEach(function(e){if(e.type==="ppinclude"&&typeof e.text==="string"&&r[e.text]!==undefined){i.push({type:"ppinclude_start",text:e.text,indent:e.indent,lang:e.lang,tags:t.tags});r[e.text].forEach(function(e){i.push(s({},e))});i.push({type:"ppinclude_end",indent:e.indent,lang:e.lang,tags:t.tags})}else{i.push(e)}});n(null,i)})}}var c={newline:/^\n+/,code:/^( {4}[^\n]+\n*)+/,fences:r,hr:/^( *[-*_]){3,} *(?:\n+|$)/,heading:/^headingid *(#{1,6}) *([^\n]+?) *#* *(?:\n|$)/,lheading:/^headingid([^\n]+)\n *(=|-){2,} *(?:\n|$)/,headingid:/(?: *<a name="([^\"]+)" *(?:\/>|> *<\/a>|>) *\n)?/,blockquote:/^( *>[^\n]+(\n(?!def)[^\n]+)*)+(?:\n|$)/,list:/^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n?|\s*$)/,html:/^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,def:/^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n|$)/,table:r,paragraph:/^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,text:/^[^\n]+/,ppinclude:r,ppnumberedheadings:r,pptoc:r,ppref:r};c.bullet=/(?:[*+-]|\d+\.)/;c.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/;c.item=i(c.item,"gm")(/bull/g,c.bullet)();c.list=i(c.list)(/bull/g,c.bullet)("hr","\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))")("def","\\n+(?="+c.def.source+")")();c.blockquote=i(c.blockquote)("def",c.def)();c.heading=i(c.heading)("headingid",c.headingid)();c.lheading=i(c.lheading)("headingid",c.headingid)();c._tag="(?!(?:[A-Za-z][A-Za-z-]*)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b";c.html=i(c.html)("comment",/<!--[\s\S]*?-->/)("closed",/<(tag)[\s\S]+?<\/\1>/)("closing",/<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)(/tag/g,c._tag)();c.paragraph=i(c.paragraph)("hr",c.hr)("heading",c.heading)("lheading",c.lheading)("blockquote",c.blockquote)("tag","<"+c._tag)("def",c.def)();c.opts={};c.opts.gfm={fences:/^( *)(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\2 *(?:\n|$)/};var f={_ppopts_:/ ?(?:\(([^\)]+)\))?/,ppinclude:/^( *)(?:\!(?:include)_ppopts_|_ppinclude_|_ppincludeCompat_) *(?:\n|$)/,_ppinclude_:/<!-- *include_ppopts_ *-->(?:(?!<!-- *\/include * -->)[^])*<!-- *\/include * -->/,_ppincludeCompat_:/\!INCLUDE "([^"]+)"/,ppnumberedheadings:/^ *(?:\!(?:numberedheadings)_ppopts_|_ppnumberedheadings_) *(?:\n+|$)/,_ppnumberedheadings_:/<!-- \!*numberedheadings_ppopts_ *-->/,pptoc:/^(?:\!(?:toc)_ppopts_|_pptoc_|_pptocCompat1_|_pptocCompat2_|_pptocCompat3_) *(?:\n+|$)/,_pptoc_:/<!-- *\!toc_ppopts_ *-->(?:(?!<!-- *toc\! * -->)[^])*<!-- *toc\! * -->/,_pptocCompat1_:/\!TOC/,_pptocCompat2_:/<!-- *toc *-->(?:(?!<!-- *(?:\/toc|toc stop) * -->)[^])*<!-- *(?:\/toc|toc stop) * -->/,_pptocCompat3_:/<!-- *toc *-->/,ppref:/^(?:\!(?:ref)|_ppref_|_pprefCompat1_|_pprefCompat2_) *(?:\n|$)/,_ppref_:/<!-- *\!ref *-->(?:(?!<!-- *ref\! * -->)[^])*<!-- *ref\! * -->/,_pprefCompat1_:/\!REF/,_pprefCompat2_:/<!-- *ref *-->(?:(?!<!-- *\/ref * -->)[^])*<!-- \/ref * -->/};f.ppinclude=i(f.ppinclude)("_ppinclude_",f._ppinclude_)("_ppincludeCompat_",f._ppincludeCompat_)("_ppopts_",f._ppopts_)("_ppopts_",f._ppopts_)();f.ppnumberedheadings=i(f.ppnumberedheadings)("_ppnumberedheadings_",f._ppnumberedheadings_)("_ppopts_",f._ppopts_)("_ppopts_",f._ppopts_)();f.pptoc=i(f.pptoc)("_pptoc_",f._pptoc_)("_ppopts_",f._ppopts_)("_ppopts_",f._ppopts_)("_pptocCompat1_",f._pptocCompat1_)("_pptocCompat2_",f._pptocCompat2_)("_pptocCompat3_",f._pptocCompat3_)();f.ppref=i(f.ppref)("_ppref_",f._ppref_)("_pprefCompat1_",f._pprefCompat1_)("_pprefCompat2_",f._pprefCompat2_)();c.opts.include={ppinclude:f.ppinclude};c.opts.numberedheadings={ppnumberedheadings:f.ppnumberedheadings};c.opts.toc={pptoc:f.pptoc};c.opts.ref={ppref:f.ppref};function d(e){this.tokens=[];this.options=e||t;this.rules=c;if(this.options.gfm){s(this.rules,c.opts.gfm)}if(this.options.include){s(this.rules,c.opts.include)}if(this.options.numberedheadings){s(this.rules,c.opts.numberedheadings)}if(this.options.toc){s(this.rules,c.opts.toc)}if(this.options.ref){s(this.rules,c.opts.ref)}}d.prototype.lex=function(e){e=e.replace(/\r\n|\r/g,"\n").replace(/\t/g," ").replace(/\u00a0/g," ").replace(/\u2424/g,"\n");return this.token(e,true)};d.prototype.token=function(e,n){var i,r,s,u,l,a,h,c,f,g,_,k;while(e){if(i=this.rules.newline.exec(e)){e=e.substring(i[0].length);this.tokens.push({type:"space",text:i[0]})}if(i=this.rules.ppinclude.exec(e)){e=e.substring(i[0].length);_=i[2]||i[3]||i[4];k=d.splitOpts(_);_=_.replace(/ *(?:[a-z]+=[a-z0-9\-]+)/,"").replace(/\\ /g," ");this.tokens.push({type:"ppinclude",text:_,indent:k.indent?p(" ",k.indent):i[1],lang:k.lang});continue}if(i=this.rules.code.exec(e)){e=e.substring(i[0].length);this.tokens.push({type:"code",text:i[0]});continue}if(i=this.rules.fences.exec(e)){e=e.substring(i[0].length);this.tokens.push({type:"fences",indent:i[1],fences:i[2],lang:i[3],text:i[4]});continue}if(i=this.rules.heading.exec(e)){e=e.substring(i[0].length);_=new m(this.options);this.tokens.push({type:"heading",depth:i[2].length,text:i[3],raw:i[3],autoid:null,anchor:i[1],inline:_.lex(i[3])});continue}if(i=this.rules.lheading.exec(e)){e=e.substring(i[0].length);_=new m(this.options);this.tokens.push({type:"heading",depth:i[3]==="="?1:2,text:i[2],raw:i[2],autoid:null,anchor:i[1],inline:_.lex(i[2])});continue}if(i=this.rules.hr.exec(e)){e=e.substring(i[0].length);this.tokens.push({type:"hr",text:i[0]});continue}if(i=this.rules.blockquote.exec(e)){e=e.substring(i[0].length);this.tokens.push({type:"blockquote_start"});i=i[0].replace(/^ *> ?/gm,"");this.token(i,n);this.tokens.push({type:"blockquote_end"});continue}if(i=this.rules.list.exec(e)){e=e.substring(i[0].length);r=i[2];this.tokens.push({type:"list_start",ordered:r.length>1,start:o(r,true)});i=i[0].match(this.rules.item);s=false;u=i.length;a=0;for(;a<u;a++){h=i[a];r=this.rules.bullet.exec(h)[0];c=h.length;h=h.replace(/^ *([*+-]|\d+\.) +/,"");if(~h.indexOf("\n ")){c-=h.length;h=!this.options.pedantic?h.replace(new RegExp("^ {1,"+c+"}","gm"),""):h.replace(/^ {1,4}/gm,"")}if(this.options.smartLists&&a!==u-1){l=this.rules.bullet.exec(i[a+1])[0];if(r!==l&&!(r.length>1&&l.length>1)){e=i.slice(a+1).join("\n")+e;a=u-1}}f=s||/\n\n(?!\s*$)/.test(h);if(a!==u-1){s=h.charAt(h.length-1)==="\n";if(!f)f=s}this.tokens.push({type:f?"loose_item_start":"list_item_start",text:r});this.token(h,false,g);this.tokens.push({type:"list_item_end"})}this.tokens.push({type:"list_end"});continue}if(i=this.rules.ppnumberedheadings.exec(e)){e=e.substring(i[0].length);k=d.splitOpts(i[1]||i[2]);if(typeof k.omit==="string"){k.omit=[k.omit]}this.options.numberedHeadings=true;this.tokens.push({type:"ppnumberedheadings",level:d.range(k.level,t.minlevel,6,t.level),minlevel:d.range(k.minlevel,t.minlevel,6),skip:k.skip,start:k.start,omit:k.omit});continue}if(i=this.rules.pptoc.exec(e)){e=e.substring(i[0].length);k=d.splitOpts(i[1]||i[2]);if(typeof k.omit==="string"){k.omit=[k.omit]}this.tokens.push({type:"pptoc",level:d.range(k.level,t.minlevel,6,t.level),minlevel:d.range(k.minlevel,t.minlevel,6),numbered:k.numbered,omit:k.omit});continue}if(i=this.rules.ppref.exec(e)){e=e.substring(i[0].length);this.tokens.push({type:"ppref"});continue}if(i=this.rules.html.exec(e)){e=e.substring(i[0].length);this.tokens.push({type:"html",pre:i[1]==="pre"||i[1]==="script"||i[1]==="style",text:i[0]});continue}if(n&&(i=this.rules.def.exec(e))){e=e.substring(i[0].length);this.tokens.push({type:"def",ref:i[1],href:i[2],title:i[3]});continue}if(n&&(i=this.rules.paragraph.exec(e))){e=e.substring(i[1].length);_=new m(this.options);this.tokens.push({type:"paragraph",text:i[1],inline:_.lex(i[1])});continue}if(i=this.rules.text.exec(e)){e=e.substring(i[0].length);_=new m(this.options);this.tokens.push({type:"text",text:i[0],inline:_.lex(i[0])});continue}if(e){throw new Error("Infinite loop on byte: "+e.charCodeAt(0))}}return this.tokens};d.rules=c;d.lex=function(e,t,n){var i=new d(t);var r=i.lex(e);h(r,t,function(e,i){n(e,i,t)})};d.splitOpts=function(e){var t={},n,i="",r=/^(.+)$/,s=/^([a-z0-9]+)=(.*)$/,o=/^([a-z0-9]+)=(["'])(.*?)\2$/;(e||"").split(" ").forEach(function(e){if(/\\$|^["'].*[^"']$/.test(e)||n&&!n.test(e)){i+=e+" ";return}if(/\=(["']).*[^"']$/.test(e)){n=e.replace(/^.*\=(["']).*[^"']$/,"$1");n=new RegExp(n+"$");i+=e+" ";return}if(i){e=i+e;i="";n=undefined}if(o.test(e)){e.replace(o,function(e,n,i,r){t[n]=r.split(";");t[n]=t[n].map(function(e){if(/^\d+$/.test(e)){var t=parseInt(e,10);if(!isNaN(t)){e=t}}return e});if(t[n].length===1){t[n]=t[n][0]}})}else if(s.test(e)){e.replace(s,function(e,n,i){var r=parseInt(i,10);if(!isNaN(r)){i=r}t[n]=i})}else if(r.test(e)){e=e.replace(/\\ /g," ").replace(/^(["'])([^\1]+)\1$/,"$2");if(/^\!(.*)$/.test(e)){e=e.replace(/^\!(.*)$/,"$1");t[e]=false}else{t[e]=true}}});return t};d.range=function(e,t,n,i){if(e<t){return t}else if(e>n){return n}else{if(i&&e===i){return undefined}return e}};var g={escape:/^\\([\\`*{}\[\]()#+\-.!_>])/,autolink:/^<([^ >]+(@|:\/)[^ >]+)>/,url:r,tag:/^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,link:/^(!)?\[(inside)\]\(href\)/,reflink:/^(!)?\[(inside)\]\s*\[([^\]]*)\]/,nolink:/^(!)?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,strong:/^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,em:/^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,code:/^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,br:/^ {2,}\n(?!\s*$)/,del:r,text:/^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/,_inside:/(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/,_href:/\s*<?([\s\S]*?)>?(?:\s+(['"][\s\S]*?['"]))?\s*/};g.link=i(g.link)("inside",g._inside)("href",g._href)();g.reflink=i(g.reflink)("inside",g._inside)();g.opts={};g.opts.gfm={escape:i(g.escape)("])","~|])")(),url:/^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,del:/^~~(?=\S)([\s\S]*?\S)~~/,text:i(g.text)("]|","~]|")("|","|https?://|")()};g.opts.breaks={br:i(g.br)("{2,}","*")(),text:i(g.opts.gfm.text)("{2,}","*")()};function m(e){this.tokens=[];this.options=e||t;this.rules=g;if(this.options.gfm){s(this.rules,g.opts.gfm)}if(this.options.breaks){s(this.rules,g.opts.gfm,g.opts.breaks)}}m.rules=g;m.lex=function(e,t){var n=new m(t);return n.lex(e)};m.prototype.lex=function(e){var t;while(e){if(t=this.rules.escape.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:"escape",text:t[1]});continue}if(!this.inLink&&(t=this.rules.url.exec(e))){e=e.substring(t[0].length);this.tokens.push({type:"url",text:t[1]});continue}if(t=this.rules.tag.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:"tag",text:t[0]});continue}if(t=this.rules.link.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:t[1]?"image":"link",raw:t[0],text:t[2],href:t[3],title:t[4]});continue}if(t=this.rules.reflink.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:t[1]?"refimage":"reflink",raw:t[0],text:t[2],ref:t[3]});continue}if(t=this.rules.nolink.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:t[1]?"noimage":"nolink",raw:t[0],text:t[2]});continue}if(t=this.rules.strong.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:"strong",text:t[2]||t[1],"char":/^(..)/.exec(t[0])[0]});continue}if(t=this.rules.em.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:"em",text:t[2]||t[1],"char":/^(.)/.exec(t[0])[0]});continue}if(t=this.rules.code.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:"code",text:t[0]});continue}if(t=this.rules.br.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:"br",text:t[0]});continue}if(t=this.rules.del.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:"del",text:t[1]});continue}if(t=this.rules.text.exec(e)){e=e.substring(t[0].length);this.tokens.push({type:"text",text:t[0]});continue}if(e){throw new Error("Infinite loop on byte: "+e.charCodeAt(0))}}return this.tokens};function _(e){this._=[0,0,0,0,0,0,0];this.last=1;this._[1]=e?e-1:0}_.prototype.reset=function(e){for(var t=e+1;t<this._.length;t++){this._[t]=0}};_.prototype.val=function(e){var t,n=this._[1];for(t=2;t<=e;t++){n+="."+this._[t]}return n+"\\."};_.prototype.count=function(e){if(e<=6){if(this.last>e){this.reset(e)}this._[e]+=1;this.last=e;return this.val(e)}return};function k(e){this.options=e||{}}k.prototype.heading=function(e,t,n,i,r,s){var o="";if(s){o+='<a name="'+s+'"/>\n'}o+="########".substring(0,t);return o+" "+e+"\n"};k.prototype.paragraph=function(e){return e};k.prototype.newline=function(e){return e};k.prototype.blockquote=function(e){return e.replace(/\n$/,"").replace(/^/gm,"> ")+"\n"};k.prototype.list=function(e){return e.replace(/\n$/,"")};k.prototype.listitem=function(e,t){var n,i="";e+=" ";for(n=0;n<e.length;n++){i+=" "}return e+t.replace(/\n(?!\n|$)/gm,"\n"+i)+"\n"};k.prototype.fences=function(e,t,n,i){return this.fence(t,n,i)+e+"\n"+this.fence("",n,i)};k.prototype.reference=function(e,t,n){return"["+e+"]: "+t+(n?' "'+n+'"':"")+"\n"};k.prototype.include=function(e,t,n){return t+"!include ("+e+(n?" lang="+n:"")+")\n"};k.prototype.fence=function(e,t,n){n=n||"```";t=t||"";return t+n+(e?e:"")+"\n"};k.prototype.codeblock=function(e){return e};k.prototype.html=function(e){return e};k.prototype.hr=function(e){return e};k.prototype.escape=function(e){return"\\"+e};k.prototype.url=function(e){return e};k.prototype.tag=function(e){return e};k.prototype.link=function(e,t,n,i){return"["+t+"]("+n+(i?" "+i:"")+")"};k.prototype.reflink=function(e,t,n){return"["+t+"]["+n+"]"};k.prototype.nolink=function(e,t){return"["+t+"]"};k.prototype.image=function(e,t,n,i){return"!"+this.link(e,t,n,i)};k.prototype.refimage=function(e,t,n){return"!"+this.reflink(e,t,n)};k.prototype.noimage=function(e,t){return"!"+this.nolink(e,t)};k.prototype.strong=function(e,t){return t+e+t};k.prototype.em=function(e,t){return t+e+t};k.prototype.del=function(e){return"~~"+e+"~~"};k.prototype.text=function(e){return e};k.prototype.br=function(e){return e};k.prototype.code=function(e){return e};k.sortByTitle=function(e,t){var n=e.title.toLowerCase(),i=t.title.toLowerCase();if(n>i){return 1}else if(n<i){return-1}else{return 0}};k.prototype.references=function(e){var t=[];e.map(function(e){if(!e.title){e.title=e.ref}return e}).sort(k.sortByTitle).forEach(function(e){t.push("* ["+e.title+"]["+e.ref+"]")});if(this.options.tags){return"<!-- !ref -->\n\n"+t.join("\n")+"\n\n<!-- ref! -->\n"}else{return t.join("\n")+"\n"}};k.prototype.tableOfContents=function(e,n){var i=this,r,s=[],o=this.joinOpts({level:n.level,minlevel:n.minlevel,numbered:n.numbered,omit:n.omit}),p=new _,u=this.options.breaks?" <br>":"",l=n.level||t.level,a=n.minlevel||t.minlevel;s=e.filter(function(e){if(e.depth<=l&&e.depth>=a){return true}return false}).map(function(e){if(!i.options.numberedHeadings&&n.numbered){e.number=p.count(e.depth-a+1)}return e}).filter(function(e){if(n.omit){if(r){if(e.depth>r){return false}else{r=undefined}}return!n.omit.some(function(t){if(t===e.raw){r=e.depth;return true}return false})}return true}).map(function(e){if(n.numbered){if(i.options.numberedHeadings){return(e.number?e.number+" ":"")+"["+i.sanitizeHeadings(e.raw)+"]("+e.autoid+")"+u}else{return e.number+" ["+i.sanitizeHeadings(e.text)+"]("+e.autoid+")"+u}}else{var t="";for(var r=1;r<(e.depth-a+1||1);r++){t+=" "}return t+"* ["+i.sanitizeHeadings(e.text)+"]("+e.autoid+")"}});if(this.options.tags){return"<!-- !toc "+o+"-->\n\n"+s.join("\n")+"\n\n<!-- toc! -->\n\n"}else{return s.join("\n")+"\n\n"}};k.prototype.numberedHeadings=function(e,t,n,i,r){var s=this.joinOpts({level:e,minlevel:t,skip:n,start:i,omit:r});if(this.options.tags){return"<!-- !numberedheadings "+s+"-->\n\n"}return""};k.prototype.joinOpts=function(e){var t,n,i=[];for(t in e){n=e[t];if(n===true){i.push(t)}else if(n!==undefined){if(Array.isArray(n)){n='"'+n.join(";")+'"'}i.push(t+"="+n)}}if(i.length>0){return"("+i.join(" ")+") "}else{return""}};k.prototype.sanitizeHeadings=function(e){return e.replace(/\[([^\]]*)\]\s*(?:\[[^\]]*\]|\([^\)]*\))/g,"$1")};function x(e){this.tokens=[];this.token=null;this.count=-1;this.indent=[];this.options=e||t;this.options.renderer=this.options.renderer||new k;this.renderer=this.options.renderer;this.renderer.options=this.options}x.prototype.parse=function(e){this.tokens=e;this.updateAutoIdentifier();var t="";while(this.next()){t+=this.tok()}return t};x.prototype.next=function(){this.token=this.tokens[this.count+=1];return this.token};x.prototype.peek=function(){return this.tokens[this.count+1]||0};x.prototype.references=function(){var e,t=[],n={title:{},ref:{}};this.tokens.forEach(function(e){if(e.type==="def"&&!/^#/.test(e.href)){if(e.title){n.title[e.ref]=e.title}else{n.ref[e.ref]=e.ref}}});for(e in n.title){t.push({ref:e,title:n.title[e]})}for(e in n.ref){if(!n.title[e]){t.push({ref:e,title:e})}}return t};x.prototype.tableOfContents=function(){return this.tokens.filter(function(e){if(e.type==="heading"){return true}return false})};x.prototype.headingAutoId=function(e,t){t=t||{raw:false};var n=(!t.raw?e.text:e.raw||"").replace(/^#/,"");if(e.anchor){return"#"+e.anchor}if(this.options.githubid){n=n.replace(/ /g,"-").replace(/%([abcdef]|\d){2,2}/gi,"").replace(/[\/?:\[\]`.,()*"';{}+<>&]/g,"").toLowerCase()}else{n=n.replace(/[^\w]+/g,"-").toLowerCase()}return"#"+n};x.prototype.updateAutoIdentifier=function(){var e=this,t={};function n(t){t=t.replace(/(?:%20|\+)/g," ");t=e.headingAutoId({text:t});return t}this.tokens=this.tokens.map(function(n){var i,r;if(n.type==="heading"){i=e.headingAutoId(n);r=e.headingAutoId(n,{raw:true});t[r]=i;t[i]=i;n.autoid=i}return n});this.tokens=this.tokens.map(function(e){var i;if(e.inline){e.inline=e.inline.map(function(e){switch(e.type){case"link":case"image":{i=n(e.href);if(t[i]){e.href=t[i]}break}}return e})}else{switch(e.type){case"def":{if(e.href&&e.href.indexOf("#")===0){i=n(e.href);if(t[i]){e.href=t[i]}break}}}}return e})};x.prototype.numberedHeadings=function(e,n,i,r,s){var o=/^([0-9]+\\?\.)+ +/,p={},u=false,l=new _(r);i=i||0;(s||[]).forEach(function(e){p[e]=true});e=e||t.level;n=n||t.minlevel;this.tokens=this.tokens.map(function(t){if(t.type==="heading"){t.text=t.text.replace(o,"");t.raw=t.raw.replace(o,"");if(t.depth===n){if(i>0){i-=1;u=true}else if(i===0){u=false}}if(!u&&!p[t.raw]&&t.depth<=e&&t.depth>=n){t.number=l.count(t.depth-n+1);t.text=t.number+" "+t.text}}return t})};x.prototype.tok=function(e){var t,n,i,r,s,o=this;e=e||{};switch(this.token.type){case"space":{return this.renderer.newline(this.token.text)}case"code":{return this.renderer.codeblock(this.token.text)}case"hr":{return this.renderer.hr(this.token.text)}case"html":{return this.renderer.html(this.token.text)}case"paragraph":{n="";(this.token.inline||[]).forEach(function(e){n+=o.inlinetok(e)});return this.renderer.paragraph(n)}case"text":{n="";(this.token.inline||[]).forEach(function(e){n+=o.inlinetok(e)});return this.renderer.text(n)}case"heading":{return this.renderer.heading(this.token.text,this.token.depth,this.token.raw,this.token.number,this.token.autoid,this.token.anchor)}case"fences":{return this.renderer.fences(this.token.text,this.token.lang,this.token.indent,this.token.fences)}case"def":{return this.renderer.reference(this.token.ref,this.token.href,this.token.title)}case"blockquote_start":{n="";while(this.next().type!=="blockquote_end"){n+=this.tok()}return this.renderer.blockquote(n)}case"list_start":{n="";s=this.token.ordered;r=this.token.start;while(this.next().type!=="list_end"){if(this.options.autonumber&&s){t={start:r++}}n+=this.tok(t)}return this.renderer.list(n,s)}case"list_item_start":{n="";i=this.token.text;if(e.start){i=e.start+"."}while(this.next().type!=="list_item_end"){n+=this.tok()}return this.renderer.listitem(i,n)}case"loose_item_start":{n="";i=this.token.text;if(e.start){i=e.start+"."}while(this.next().type!=="list_item_end"){n+=this.tok()}return this.renderer.listitem(i,n)}case"ppnumberedheadings":{this.options.numberedHeadings=true;this.numberedHeadings(this.token.level,this.token.minlevel,this.token.skip,this.token.start,this.token.omit);this.updateAutoIdentifier();return this.renderer.numberedHeadings(this.token.level,this.token.minlevel,this.token.skip,this.token.start,this.token.omit)}case"ppref":{return this.renderer.references(this.references())}case"ppinclude_start":{n="";if(this.token.tags){var p=this.token.indent.replace(" "," ").length;n+="<!-- include ("+this.token.text.replace(/ /g,"\\ ")+(this.token.lang?" lang="+this.token.lang:"")+(p?" indent="+p.toString():"")+") -->\n"}if(typeof this.token.lang==="string"){n+=this.renderer.fence(this.token.lang)}return n}case"ppinclude_end":{n="";if(typeof this.token.lang==="string"){n+=this.renderer.fence()}if(this.token.tags){n+="<!-- /include -->\n"}return n}case"ppinclude":{return this.renderer.include(this.token.text,this.token.indent,this.token.lang)}case"pptoc":{return this.renderer.tableOfContents(this.tableOfContents(),this.token)}default:{return"<!-- "+JSON.stringify(this.token)+" -->\n"}}};x.prototype.inlinetok=function(e){switch(e.type){case"escape":{return this.renderer.escape(e.text)}case"url":{return this.renderer.url(e.text)}case"tag":{return this.renderer.tag(e.text)}case"link":{return this.renderer.link(e.raw,e.text,e.href,e.title)}case"reflink":{return this.renderer.reflink(e.raw,e.text,e.ref)}case"nolink":{return this.renderer.nolink(e.raw,e.text)}case"image":{return this.renderer.image(e.raw,e.text,e.href,e.title)}case"refimage":{return this.renderer.refimage(e.raw,e.text,e.ref)}case"noimage":{return this.renderer.noimage(e.raw,e.text)}case"strong":{return this.renderer.strong(e.text,e.char)}case"em":{return this.renderer.em(e.text,e.char)}case"text":{return this.renderer.text(e.text)}case"code":{return this.renderer.code(e.text)}case"br":{return this.renderer.br(e.text)}case"del":{return this.renderer.del(e.text)}default:{return"<!-- "+JSON.stringify(this.token)+" -->\n"}}};x.parse=function(e,t){var n=new x(t);return n.parse(e)};function b(e,n,i){if(typeof n==="function"){i=n;n=null}n=s({},t,n||{});d.lex(e,n,function(e,t){if(e){}var r=x.parse(t,n);i(null,r)})}b.defaults=t;b.Lexer=d;b.InlineLexer=m;b.Renderer=k;b.Parser=x;b.merge=s;b.setOptions=function(e){s(t,e);return b};if(n()){module.exports=b}else if(typeof define!=="undefined"&&define.amd){define([],function(){return b})}else if(typeof e.Window!=="undefined"&&!e[b]){e.markedpp=b}})(this);