mmark
Version:
Modern markdown parser.
3 lines (2 loc) • 16.4 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.mmark=t()}(this,function(){"use strict";function e(e){for(var t,n,r=arguments,s=1;s<arguments.length;s++){t=r[s];for(n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}function t(){}function n(e,t){return e.replace(t?/&/g:/&(?!#?\w+;)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function r(e){return e.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/g,function(e,t){return t=t.toLowerCase(),"colon"===t?":":"#"===t.charAt(0)?"x"===t.charAt(1)?String.fromCharCode(parseInt(t.substring(2),16)):String.fromCharCode(Number(t.substring(1))):""})}function s(e,t){return e=e.source,t=t||"",function n(r,s){return r?(s=s.source||s,s=s.replace(/(^|[^\[])\^/g,"$1"),e=e.replace(r,s),n):new RegExp(e,t)}}function i(e){return e.replace(/<(?:.|\n)*?>/gm,"").replace(/[!\"#$%&'\(\)\*\+,\/:;<=>\?\@\[\\\]\^`\{\|\}~]/g,"").replace(/(\s|\.)/g,"-").replace(/-{2,}/g,"-").toLowerCase()}function l(t,n,r){if(r||"function"==typeof n){r||(r=n,n=null),n=e({},a,n||{});var s,i,l=n.highlight,o=0;try{s=g.lex(t,n)}catch(e){return r(e)}i=s.length;var h=function(e){if(e)return n.highlight=l,r(e);var t;try{t=u.parse(s,n)}catch(t){e=t}return n.highlight=l,e?r(e):r(null,t)};if(!l||l.length<3)return h();if(delete n.highlight,!i)return h();for(;o<s.length;o++)!function(e){"code"!==e.type?--i||h():l(e.text,e.lang,function(t,n){return t?h(t):null===n||n===e.text?--i||h():(e.text=n,e.escaped=!0,void(--i||h()))})}(s[o])}else try{return n&&(n=e({},a,n)),u.parse(g.lex(t,n),n)}catch(e){if(e.message+="\nPlease report this to https://github.com/egoist/mmark.",(n||a).silent)return"<p>An error occured:</p><pre>"+escape(String(e.message),!0)+"</pre>";throw e}}t.exec=t;var o=function(e){this.options=e||{},this._headings=[]};o.prototype.code=function(e,t,r){if(this.options.highlight){var s=this.options.highlight(e,t);null!==s&&s!==e&&(r=!0,e=s)}return t?'<pre><code class="'+this.options.langPrefix+n(t,!0)+'">'+(r?e:n(e,!0))+"\n</code></pre>\n":"<pre><code>"+(r?e:n(e,!0))+"\n</code></pre>"},o.prototype.blockquote=function(e){return"<blockquote>\n"+e+"</blockquote>\n"},o.prototype.html=function(e){return e},o.prototype.heading=function(e,t,n){var r=i(n),s=this._headings.filter(function(e){return e===n}).length;return s>0&&(r+="-"+s),this._headings.push(n),"<h"+t+' id="'+this.options.headerPrefix+r+'">'+e+"</h"+t+">\n"},o.prototype.hr=function(){return this.options.xhtml?"<hr/>\n":"<hr>\n"},o.prototype.list=function(e,t,n){var r=t?"ol":"ul";return"<"+r+(n?' class="task-list"':"")+">\n"+e+"</"+r+">\n"},o.prototype.listitem=function(e,t){return void 0===t?"<li>"+e+"</li>\n":'<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox"'+(t?" checked":"")+"> "+e+"</li>\n"},o.prototype.paragraph=function(e){return"<p>"+e+"</p>\n"},o.prototype.table=function(e,t){return"<table>\n<thead>\n"+e+"</thead>\n<tbody>\n"+t+"</tbody>\n</table>\n"},o.prototype.tablerow=function(e){return"<tr>\n"+e+"</tr>\n"},o.prototype.tablecell=function(e,t){var n=t.header?"th":"td";return(t.align?"<"+n+' style="text-align:'+t.align+'">':"<"+n+">")+e+"</"+n+">\n"},o.prototype.strong=function(e){return"<strong>"+e+"</strong>"},o.prototype.em=function(e){return"<em>"+e+"</em>"},o.prototype.codespan=function(e){return"<code>"+e+"</code>"},o.prototype.br=function(){return this.options.xhtml?"<br/>":"<br>"},o.prototype.del=function(e){return"<del>"+e+"</del>"},o.prototype.link=function(e,t,n){if(this.options.sanitize){var s;try{s=decodeURIComponent(r(e)).replace(/[^\w:]/g,"").toLowerCase()}catch(e){return""}if(0===s.indexOf("javascript:")||0===s.indexOf("vbscript:")||0===s.indexOf("data:"))return""}var i='<a href="'+e+'"';return t&&(i+=' title="'+t+'"'),i+=">"+n+"</a>"},o.prototype.image=function(e,t,n){var r='<img src="'+e+'" alt="'+n+'"';return t&&(r+=' title="'+t+'"'),r+=this.options.xhtml?"/>":">"},o.prototype.text=function(e){return e};var a={gfm:!0,tables:!0,taskLists:!0,breaks:!1,pedantic:!1,sanitize:!1,sanitizer:null,mangle:!0,smartLists:!1,silent:!1,highlight:null,langPrefix:"lang-",smartypants:!1,headerPrefix:"",renderer:new o,xhtml:!1},h={escape:/^\\([\\`*{}[\]()#+\-.!_>])/,autolink:/^<([^ >]+(@|:\/)[^ >]+)>/,url:t,tag:/^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,link:/^!?\[(inside)\]\(href\)/,reflink:/^!?\[(inside)\]\s*\[([^\]]*)\]/,nolink:/^!?\[((?:\[[^\]]*\]|[^[\]])*)\]/,strong:/^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,em:/^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,code:/^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,br:/^ {2,}\n(?!\s*$)/,del:t,text:/^[\s\S]+?(?=[\\<![_*`]| {2,}\n|$)/};h._inside=/(?:\[[^\]]*\]|[^[\]]|\](?=[^[]*\]))*/,h._href=/\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/,h.link=s(h.link)("inside",h._inside)("href",h._href)(),h.reflink=s(h.reflink)("inside",h._inside)(),h.normal=e({},h),h.pedantic=e({},h.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/}),h.gfm=e({},h.normal,{escape:s(h.escape)("])","~|])")(),url:/^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,del:/^~~(?=\S)([\s\S]*?\S)~~/,text:s(h.text)("]|","~]|")("|","|https?://|")()}),h.breaks=e({},h.gfm,{br:s(h.br)("{2,}","*")(),text:s(h.gfm.text)("{2,}","*")()});var p=function(e,t){if(void 0===t&&(t=a),this.options=t,this.links=e,this.renderer=this.options.renderer||new o,this.renderer.options=this.options,!this.links)throw new Error("Tokens array requires a `links` property.");this.options.gfm?this.options.breaks?this.rules=h.breaks:this.rules=h.gfm:this.options.pedantic?this.rules=h.pedantic:this.rules=h.normal};p.output=function(e,t,n){return new p(t,n).output(e)},p.prototype.output=function(e){for(var t,r,s,i,l=this,o="";e;)if(i=l.rules.escape.exec(e))e=e.substring(i[0].length),o+=i[1];else if(i=l.rules.autolink.exec(e))e=e.substring(i[0].length),"@"===i[2]?(r=":"===i[1].charAt(6)?l.mangle(i[1].substring(7)):l.mangle(i[1]),s=l.mangle("mailto:")+r):(r=n(i[1]),s=r),o+=l.renderer.link(s,null,r);else if(l.inLink||!(i=l.rules.url.exec(e))){if(i=l.rules.tag.exec(e))!l.inLink&&/^<a /i.test(i[0])?l.inLink=!0:l.inLink&&/^<\/a>/i.test(i[0])&&(l.inLink=!1),e=e.substring(i[0].length),o+=l.options.sanitize?l.options.sanitizer?l.options.sanitizer(i[0]):n(i[0]):i[0];else if(i=l.rules.link.exec(e))e=e.substring(i[0].length),l.inLink=!0,o+=l.outputLink(i,{href:i[2],title:i[3]}),l.inLink=!1;else if((i=l.rules.reflink.exec(e))||(i=l.rules.nolink.exec(e))){if(e=e.substring(i[0].length),t=(i[2]||i[1]).replace(/\s+/g," "),!(t=l.links[t.toLowerCase()])||!t.href){o+=i[0].charAt(0),e=i[0].substring(1)+e;continue}l.inLink=!0,o+=l.outputLink(i,t),l.inLink=!1}else if(i=l.rules.strong.exec(e))e=e.substring(i[0].length),o+=l.renderer.strong(l.output(i[2]||i[1]));else if(i=l.rules.em.exec(e))e=e.substring(i[0].length),o+=l.renderer.em(l.output(i[2]||i[1]));else if(i=l.rules.code.exec(e))e=e.substring(i[0].length),o+=l.renderer.codespan(n(i[2],!0));else if(i=l.rules.br.exec(e))e=e.substring(i[0].length),o+=l.renderer.br();else if(i=l.rules.del.exec(e))e=e.substring(i[0].length),o+=l.renderer.del(l.output(i[1]));else if(i=l.rules.text.exec(e))e=e.substring(i[0].length),o+=l.renderer.text(n(l.smartypants(i[0])));else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0))}else e=e.substring(i[0].length),r=n(i[1]),s=r,o+=l.renderer.link(s,null,r);return o},p.prototype.outputLink=function(e,t){var r=n(t.href),s=t.title?n(t.title):null;return"!"===e[0].charAt(0)?this.renderer.image(r,s,n(e[1])):this.renderer.link(r,s,this.output(e[1]))},p.prototype.smartypants=function(e){return this.options.smartypants?e.replace(/---/g,"—").replace(/--/g,"–").replace(/(^|[-\u2014\/(\[{"\s])'/g,"$1‘").replace(/'/g,"’").replace(/(^|[-\u2014\/(\[{\u2018\s])"/g,"$1“").replace(/"/g,"”").replace(/\.{3}/g,"…"):e},p.prototype.mangle=function(e){if(!this.options.mangle)return e;for(var t,n="",r=0;r<e.length;r++)t=e.charCodeAt(r),Math.random()>.5&&(t="x"+t.toString(16)),n+="&#"+t+";";return n},p.rules=h;var u=function(e){void 0===e&&(e=a),this.tokens=[],this.token=null,this.options=e,this.options.renderer=this.options.renderer||new o,this.renderer=this.options.renderer,this.renderer.options=this.options};u.parse=function(e,t,n){return new u(t,n).parse(e)},u.prototype.parse=function(e){var t=this;this.inline=new p(e.links,this.options,this.renderer),this.tokens=e.reverse();for(var n="";this.next();)n+=t.tok();return this.renderer._headings=[],n},u.prototype.next=function(){return this.token=this.tokens.pop(),this.token},u.prototype.peek=function(){return this.tokens[this.tokens.length-1]||0},u.prototype.parseText=function(){for(var e=this,t=this.token.text;"text"===this.peek().type;)t+="\n"+e.next().text;return this.inline.output(t)},u.prototype.tok=function(){var e=this;switch(this.token.type){case"space":return"";case"hr":return this.renderer.hr();case"heading":return this.renderer.heading(this.inline.output(this.token.text),this.token.depth,this.token.text);case"code":return this.renderer.code(this.token.text,this.token.lang,this.token.escaped);case"table":var t,n,r,s,i="",l="";for(r="",t=0;t<this.token.header.length;t++)r+=e.renderer.tablecell(e.inline.output(e.token.header[t]),{header:!0,align:e.token.align[t]});for(i+=this.renderer.tablerow(r),t=0;t<this.token.cells.length;t++){for(n=e.token.cells[t],r="",s=0;s<n.length;s++)r+=e.renderer.tablecell(e.inline.output(n[s]),{header:!1,align:e.token.align[s]});l+=e.renderer.tablerow(r)}return this.renderer.table(i,l);case"blockquote_start":for(var o="";"blockquote_end"!==this.next().type;)o+=e.tok();return this.renderer.blockquote(o);case"list_start":for(var a="",h=!1,p=this.token.ordered;"list_end"!==this.next().type;)void 0!==e.token.checked&&(h=!0),a+=e.tok();return this.renderer.list(a,p,h);case"list_item_start":for(var u="",c=this.token.checked;"list_item_end"!==this.next().type;)u+="text"===e.token.type?e.parseText():e.tok();return this.renderer.listitem(u,c);case"loose_item_start":for(var g="",d=this.token.checked;"list_item_end"!==this.next().type;)g+=e.tok();return this.renderer.listitem(g,d);case"html":var f=this.token.pre||this.options.pedantic?this.token.text:this.inline.output(this.token.text);return this.renderer.html(f);case"paragraph":return this.renderer.paragraph(this.inline.output(this.token.text));case"text":return this.renderer.paragraph(this.parseText());default:throw new Error("Unknow type")}};var c={newline:/^\n+/,code:/^( {4}[^\n]+\n*)+/,fences:t,hr:/^( *[-*_]){3,} *(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,nptable:t,lheading:/^([^\n]+)\n *(=|-){2,} *(?:\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:t,paragraph:/^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,text:/^[^\n]+/};c.bullet=/(?:[*+-]|\d+\.)/,c.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/,c.item=s(c.item,"gm")(/bull/g,c.bullet)(),c.list=s(c.list)(/bull/g,c.bullet)("hr","\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))")("def","\\n+(?="+c.def.source+")")(),c.blockquote=s(c.blockquote)("def",c.def)(),c._tag="(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b",c.html=s(c.html)("comment",/<!--[\s\S]*?-->/)("closed",/<(tag)[\s\S]+?<\/\1>/)("closing",/<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)(/tag/g,c._tag)(),c.paragraph=s(c.paragraph)("hr",c.hr)("heading",c.heading)("lheading",c.lheading)("blockquote",c.blockquote)("tag","<"+c._tag)("def",c.def)(),c.normal=e({},c),c.gfm=e({},c.normal,{fences:/^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/,paragraph:/^/,heading:/^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/,checkbox:/^\[([ x])\] +/}),c.gfm.paragraph=s(c.paragraph)("(?!","(?!"+c.gfm.fences.source.replace("\\1","\\2")+"|"+c.list.source.replace("\\1","\\3")+"|")(),c.tables=e({},c.gfm,{nptable:/^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,table:/^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/});var g=function(e){void 0===e&&(e=a),this.tokens=[],this.tokens.links={},this.options=e,this.options.gfm?this.options.table?this.rules=c.table:this.rules=c.gfm:this.rules=c.normal};return g.lex=function(e,t){return new g(t).lex(e)},g.prototype.lex=function(e){return e=e.replace(/\r\n|\r/g,"\n").replace(/\t/g," ").replace(/\u00a0/g," ").replace(/\u2424/g,"\n"),this.token(e,!0)},g.prototype.token=function(e,t,n){var r=this;e=e.replace(/^ +$/gm,"");for(var s,i,l,o,a,h,p,u,c,g;e;)if((l=r.rules.newline.exec(e))&&(e=e.substring(l[0].length),l[0].length>1&&r.tokens.push({type:"space"})),l=r.rules.code.exec(e))e=e.substring(l[0].length),l=l[0].replace(/^ {4}/gm,""),r.tokens.push({type:"code",text:r.options.pedantic?l:l.replace(/\n+$/,"")});else if(l=r.rules.fences.exec(e))e=e.substring(l[0].length),r.tokens.push({type:"code",lang:l[2],text:l[3]||""});else if(l=r.rules.heading.exec(e))e=e.substring(l[0].length),r.tokens.push({type:"heading",depth:l[1].length,text:l[2]});else if(t&&(l=r.rules.nptable.exec(e))){for(e=e.substring(l[0].length),h={type:"table",header:l[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:l[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:l[3].replace(/\n$/,"").split("\n")},u=0;u<h.align.length;u++)/^ *-+: *$/.test(h.align[u])?h.align[u]="right":/^ *:-+: *$/.test(h.align[u])?h.align[u]="center":/^ *:-+ *$/.test(h.align[u])?h.align[u]="left":h.align[u]=null;for(u=0;u<h.cells.length;u++)h.cells[u]=h.cells[u].split(/ *\| */);r.tokens.push(h)}else if(l=r.rules.lheading.exec(e))e=e.substring(l[0].length),r.tokens.push({type:"heading",depth:"="===l[2]?1:2,text:l[1]});else if(l=r.rules.hr.exec(e))e=e.substring(l[0].length),r.tokens.push({type:"hr"});else if(l=r.rules.blockquote.exec(e))e=e.substring(l[0].length),r.tokens.push({type:"blockquote_start"}),l=l[0].replace(/^ *> ?/gm,""),r.token(l,t,!0),r.tokens.push({type:"blockquote_end"});else if(l=r.rules.list.exec(e)){for(e=e.substring(l[0].length),o=l[2],r.tokens.push({type:"list_start",ordered:o.length>1}),l=l[0].match(r.rules.item),s=!1,c=l.length,u=0;u<c;u++)h=l[u],p=h.length,h=h.replace(/^ *([*+-]|\d+\.) +/,""),r.options.gfm&&r.options.taskLists&&(g=r.rules.checkbox.exec(h),g?(g="x"===g[1],h=h.replace(r.rules.checkbox,"")):g=void 0),-1!==h.indexOf("\n ")&&(p-=h.length,h=r.options.pedantic?h.replace(/^ {1,4}/gm,""):h.replace(new RegExp("^ {1,"+p+"}","gm"),"")),r.options.smartLists&&u!==c-1&&(a=r.rules.bullet.exec(l[u+1])[0],o===a||o.length>1&&a.length>1||(e=l.slice(u+1).join("\n")+e,u=c-1)),i=s||/\n\n(?!\s*$)/.test(h),u!==c-1&&(s="\n"===h.charAt(h.length-1),i||(i=s)),r.tokens.push({checked:g,type:i?"loose_item_start":"list_item_start"}),r.token(h,!1,n),r.tokens.push({type:"list_item_end"});r.tokens.push({type:"list_end"})}else if(l=r.rules.html.exec(e))e=e.substring(l[0].length),r.tokens.push({type:r.options.sanitize?"paragraph":"html",pre:!r.options.sanitizer&&("pre"===l[1]||"script"===l[1]||"style"===l[1]),text:l[0]});else if(!n&&t&&(l=r.rules.def.exec(e)))e=e.substring(l[0].length),r.tokens.links[l[1].toLowerCase()]={href:l[2],title:l[3]};else if(t&&(l=r.rules.table.exec(e))){for(e=e.substring(l[0].length),h={type:"table",header:l[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:l[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:l[3].replace(/(?: *\| *)?\n$/,"").split("\n")},u=0;u<h.align.length;u++)/^ *-+: *$/.test(h.align[u])?h.align[u]="right":/^ *:-+: *$/.test(h.align[u])?h.align[u]="center":/^ *:-+ *$/.test(h.align[u])?h.align[u]="left":h.align[u]=null;for(u=0;u<h.cells.length;u++)h.cells[u]=h.cells[u].replace(/^ *\| *| *\| *$/g,"").split(/ *\| */);r.tokens.push(h)}else if(t&&(l=r.rules.paragraph.exec(e)))e=e.substring(l[0].length),r.tokens.push({type:"paragraph",text:"\n"===l[1].charAt(l[1].length-1)?l[1].slice(0,-1):l[1]});else if(l=r.rules.text.exec(e))e=e.substring(l[0].length),r.tokens.push({type:"text",text:l[0]});else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0));return this.tokens},g.rules=c,l.Renderer=o,l.Parser=u,l.Lexer=g,l.InlineLexer=p,l});
//# sourceMappingURL=mmark.min.js.map