forked from markedjs/marked
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marked.min.js
6 lines (6 loc) · 16.7 KB
/
marked.min.js
1
2
3
4
5
6
/**
* marked - a markdown parser
* Copyright (c) 2011-2014, Christopher Jeffrey. (MIT Licensed)
* https://github.com/chjj/marked
*/
(function(){function e(e){this.tokens=[],this.tokens.links={},this.options=e||h.defaults,this.rules=a.normal,this.options.gfm&&(this.options.tables?this.rules=a.tables:this.rules=a.gfm)}function t(e,t){if(this.options=t||h.defaults,this.links=e,this.rules=p.normal,this.renderer=this.options.renderer||new n,this.renderer.options=this.options,!this.links)throw new Error("Tokens array requires a `links` property.");this.options.gfm?this.options.breaks?this.rules=p.breaks:this.rules=p.gfm:this.options.pedantic&&(this.rules=p.pedantic)}function n(e){this.options=e||{}}function s(e){this.tokens=[],this.token=null,this.options=e||h.defaults,this.options.renderer=this.options.renderer||new n,this.renderer=this.options.renderer,this.renderer.options=this.options}function r(e){return e.replace(/&([#\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(+t.substring(1)):""})}function i(e,t){return e=e.source,t=t||"",function n(s,r){return s?(r=r.source||r,r=r.replace(/(^|[^\[])\^/g,"$1"),e=e.replace(s,r),n):new RegExp(e,t)}}function o(){}function l(e){for(var t,n,s=1;s<arguments.length;s++){t=arguments[s];for(n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}function h(t,n,r){if(r||"function"==typeof n){r||(r=n,n=null),n=l({},h.defaults,n||{});var i,o,a=n.highlight,p=0;try{i=e.lex(t,n)}catch(u){return r(u)}o=i.length;var c=function(e){if(e)return n.highlight=a,r(e);var t;try{t=s.parse(i,n)}catch(o){e=o}return n.highlight=a,e?r(e):r(null,t)};if(!a||a.length<3)return c();if(delete n.highlight,!o)return c();for(;p<i.length;p++)!function(e){return"code"!==e.type?--o||c():a(e.text,e.lang,function(t,n){return t?c(t):null==n||n===e.text?--o||c():(e.text=n,e.escaped=!0,void(--o||c()))})}(i[p])}else try{return n&&(n=l({},h.defaults,n)),s.parse(e.lex(t,n),n)}catch(u){if(u.message+="\nPlease report this to https://github.com/chjj/marked.",(n||h.defaults).silent)return"<p>An error occured:</p><pre>"+this.renderer.escape(u.message+"",!0)+"</pre>";throw u}}var a={newline:/^\n+/,code:/^( {4}[^\n]+\n*)+/,fences:o,hr:/^( *[-*_]){3,} *(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,nptable:o,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:o,paragraph:/^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,text:/^[^\n]+/};a.checkbox=/^\[([ x])\] +/,a.bullet=/(?:[*+-]|\d+\.)/,a.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/,a.item=i(a.item,"gm")(/bull/g,a.bullet)(),a.list=i(a.list)(/bull/g,a.bullet)("hr","\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))")("def","\\n+(?="+a.def.source+")")(),a.blockquote=i(a.blockquote)("def",a.def)(),a._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",a.html=i(a.html)("comment",/<!--[\s\S]*?-->/)("closed",/<(tag)[\s\S]+?<\/\1>/)("closing",/<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)(/tag/g,a._tag)(),a.paragraph=i(a.paragraph)("hr",a.hr)("heading",a.heading)("lheading",a.lheading)("blockquote",a.blockquote)("tag","<"+a._tag)("def",a.def)(),a.normal=l({},a),a.gfm=l({},a.normal,{fences:/^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/,paragraph:/^/,heading:/^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/}),a.gfm.paragraph=i(a.paragraph)("(?!","(?!"+a.gfm.fences.source.replace("\\1","\\2")+"|"+a.list.source.replace("\\1","\\3")+"|")(),a.tables=l({},a.gfm,{nptable:/^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,table:/^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/}),e.rules=a,e.lex=function(t,n){var s=new e(n);return s.lex(t)},e.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)},e.prototype.token=function(e,t,n){for(var s,r,i,o,l,h,p,u,c,g,e=e.replace(/^ +$/gm,"");e;)if((i=this.rules.newline.exec(e))&&(e=e.substring(i[0].length),i[0].length>1&&this.tokens.push({type:"space"})),i=this.rules.code.exec(e))e=e.substring(i[0].length),i=i[0].replace(/^ {4}/gm,""),this.tokens.push({type:"code",text:this.options.pedantic?i:i.replace(/\n+$/,"")});else if(i=this.rules.fences.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"code",lang:i[2],text:i[3]||""});else if(i=this.rules.heading.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"heading",depth:i[1].length,text:i[2]});else if(t&&(i=this.rules.nptable.exec(e))){for(e=e.substring(i[0].length),h={type:"table",header:i[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:i[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:i[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(/ *\| */);this.tokens.push(h)}else if(i=this.rules.lheading.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"heading",depth:"="===i[2]?1:2,text:i[1]});else if(i=this.rules.hr.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"hr"});else 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,t,!0),this.tokens.push({type:"blockquote_end"});else if(i=this.rules.list.exec(e)){for(e=e.substring(i[0].length),o=i[2],this.tokens.push({type:"list_start",ordered:o.length>1}),i=i[0].match(this.rules.item),s=!1,c=i.length,u=0;u<c;u++)h=i[u],p=h.length,h=h.replace(/^ *([*+-]|\d+\.) +/,""),this.options.gfm&&(g=a.checkbox.exec(h),g?(g="x"===g[1],h=h.replace(a.checkbox,"")):g=void 0),~h.indexOf("\n ")&&(p-=h.length,h=this.options.pedantic?h.replace(/^ {1,4}/gm,""):h.replace(new RegExp("^ {1,"+p+"}","gm"),"")),this.options.smartLists&&u!==c-1&&(l=a.bullet.exec(i[u+1])[0],o===l||o.length>1&&l.length>1||(e=i.slice(u+1).join("\n")+e,u=c-1)),r=s||/\n\n(?!\s*$)/.test(h),u!==c-1&&(s="\n"===h.charAt(h.length-1),r||(r=s)),this.tokens.push({checked:g,type:r?"loose_item_start":"list_item_start"}),this.token(h,!1,n),this.tokens.push({type:"list_item_end"});this.tokens.push({type:"list_end"})}else if(i=this.rules.html.exec(e))e=e.substring(i[0].length),this.tokens.push({type:this.options.sanitize?"paragraph":"html",pre:!this.options.sanitizer&&("pre"===i[1]||"script"===i[1]||"style"===i[1]),text:i[0]});else if(!n&&t&&(i=this.rules.def.exec(e)))e=e.substring(i[0].length),this.tokens.links[i[1].toLowerCase()]={href:i[2],title:i[3]};else if(t&&(i=this.rules.table.exec(e))){for(e=e.substring(i[0].length),h={type:"table",header:i[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:i[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:i[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(/ *\| */);this.tokens.push(h)}else if(t&&(i=this.rules.paragraph.exec(e)))e=e.substring(i[0].length),this.tokens.push({type:"paragraph",text:"\n"===i[1].charAt(i[1].length-1)?i[1].slice(0,-1):i[1]});else if(i=this.rules.text.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"text",text:i[0]});else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0));return this.tokens};var p={escape:/^\\([\\`*{}\[\]()#+\-.!_>])/,autolink:/^<([^ >]+(@|:\/)[^ >]+)>/,url:o,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:o,text:/^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/};p._inside=/(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/,p._href=/\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/,p.link=i(p.link)("inside",p._inside)("href",p._href)(),p.reflink=i(p.reflink)("inside",p._inside)(),p.normal=l({},p),p.pedantic=l({},p.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/}),p.gfm=l({},p.normal,{escape:i(p.escape)("])","~|])")(),url:/^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,del:/^~~(?=\S)([\s\S]*?\S)~~/,text:i(p.text)("]|","~]|")("|","|https?://|")()}),p.breaks=l({},p.gfm,{br:i(p.br)("{2,}","*")(),text:i(p.gfm.text)("{2,}","*")()}),t.rules=p,t.output=function(e,n,s){var r=new t(n,s);return r.output(e)},t.prototype.output=function(e){for(var t,n,s,r,i=[];e;)if(r=this.rules.escape.exec(e))e=e.substring(r[0].length),i.push(r[1]);else if(r=this.rules.autolink.exec(e))e=e.substring(r[0].length),"@"===r[2]?(n=":"===r[1].charAt(6)?this.mangle(r[1].substring(7)):this.mangle(r[1]),s=this.mangle("mailto:")+n):(n=this.renderer.escape(r[1]),s=n),i.push(this.renderer.link(s,null,n));else if(this.inLink||!(r=this.rules.url.exec(e))){if(r=this.rules.tag.exec(e))!this.inLink&&/^<a /i.test(r[0])?this.inLink=!0:this.inLink&&/^<\/a>/i.test(r[0])&&(this.inLink=!1),e=e.substring(r[0].length),i.push(this.options.sanitize?this.options.sanitizer?this.options.sanitizer(r[0]):this.renderer.escape(r[0]):r[0]);else if(r=this.rules.link.exec(e))e=e.substring(r[0].length),this.inLink=!0,i.push(this.outputLink(r,{href:r[2],title:r[3]})),this.inLink=!1;else if((r=this.rules.reflink.exec(e))||(r=this.rules.nolink.exec(e))){if(e=e.substring(r[0].length),t=(r[2]||r[1]).replace(/\s+/g," "),t=this.links[t.toLowerCase()],!t||!t.href){i.push(r[0].charAt(0)),e=r[0].substring(1)+e;continue}this.inLink=!0,i.push(this.outputLink(r,t)),this.inLink=!1}else if(r=this.rules.strong.exec(e))e=e.substring(r[0].length),i.push(this.renderer.strong(this.output(r[2]||r[1])));else if(r=this.rules.em.exec(e))e=e.substring(r[0].length),i.push(this.renderer.em(this.output(r[2]||r[1])));else if(r=this.rules.code.exec(e))e=e.substring(r[0].length),i.push(this.renderer.codespan(this.renderer.escape(r[2],!0)));else if(r=this.rules.br.exec(e))e=e.substring(r[0].length),i.push(this.renderer.br());else if(r=this.rules.del.exec(e))e=e.substring(r[0].length),i.push(this.renderer.del(this.output(r[1])));else if(r=this.rules.text.exec(e))e=e.substring(r[0].length),i.push(this.renderer.text(this.renderer.escape(this.smartypants(r[0]))));else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0))}else e=e.substring(r[0].length),n=this.renderer.escape(r[1]),s=n,i.push(this.renderer.link(s,null,n));return this.renderer.combine(i)},t.prototype.outputLink=function(e,t){var n=this.renderer.escape(t.href),s=t.title?this.renderer.escape(t.title):null;return"!"!==e[0].charAt(0)?this.renderer.link(n,s,this.output(e[1])):this.renderer.image(n,s,this.renderer.escape(e[1]))},t.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},t.prototype.mangle=function(e){if(!this.options.mangle)return e;for(var t,n="",s=e.length,r=0;r<s;r++)t=e.charCodeAt(r),Math.random()>.5&&(t="x"+t.toString(16)),n+="&#"+t+";";return n},n.prototype.combine=function(e){return e.join("")},n.prototype.escape=function(e,t){return e.replace(t?/&/g:/&(?!#?\w+;)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")},n.prototype.code=function(e,t,n){if(this.options.highlight){var s=this.options.highlight(e,t);null!=s&&s!==e&&(n=!0,e=s)}return t?'<pre><code class="'+this.options.langPrefix+this.escape(t,!0)+'">'+(n?e:this.escape(e,!0))+"\n</code></pre>\n":"<pre><code>"+(n?e:this.escape(e,!0))+"\n</code></pre>"},n.prototype.blockquote=function(e){return"<blockquote>\n"+e+"</blockquote>\n"},n.prototype.html=function(e){return e},n.prototype.heading=function(e,t,n){return"<h"+t+' id="'+this.options.headerPrefix+n.toLowerCase().replace(/[^\w]+/g,"-")+'">'+e+"</h"+t+">\n"},n.prototype.hr=function(){return this.options.xhtml?"<hr/>\n":"<hr>\n"},n.prototype.list=function(e,t,n){var s=t?"ol":"ul",r=n?' class="task-list"':"";return"<"+s+r+">\n"+e+"</"+s+">\n"},n.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"},n.prototype.paragraph=function(e){return"<p>"+e+"</p>\n"},n.prototype.table=function(e,t){return"<table>\n<thead>\n"+e+"</thead>\n<tbody>\n"+t+"</tbody>\n</table>\n"},n.prototype.tablerow=function(e){return"<tr>\n"+e+"</tr>\n"},n.prototype.tablecell=function(e,t){var n=t.header?"th":"td",s=t.align?"<"+n+' style="text-align:'+t.align+'">':"<"+n+">";return s+e+"</"+n+">\n"},n.prototype.strong=function(e){return"<strong>"+e+"</strong>"},n.prototype.em=function(e){return"<em>"+e+"</em>"},n.prototype.codespan=function(e){return"<code>"+e+"</code>"},n.prototype.br=function(){return this.options.xhtml?"<br/>":"<br>"},n.prototype.del=function(e){return"<del>"+e+"</del>"},n.prototype.link=function(e,t,n){if(this.options.sanitize){try{var s=decodeURIComponent(r(e)).replace(/[^\w:]/g,"").toLowerCase()}catch(i){return""}if(0===s.indexOf("javascript:")||0===s.indexOf("vbscript:"))return""}var o='<a href="'+e+'"';return t&&(o+=' title="'+t+'"'),o+=">"+n+"</a>"},n.prototype.image=function(e,t,n){var s='<img src="'+e+'" alt="'+n+'"';return t&&(s+=' title="'+t+'"'),s+=this.options.xhtml?"/>":">"},n.prototype.text=function(e){return e},s.parse=function(e,t,n){var r=new s(t,n);return r.parse(e)},s.prototype.parse=function(e){this.inline=new t(e.links,this.options,this.renderer),this.tokens=e.reverse();for(var n=[];this.next();)n.push(this.tok());return this.renderer.combine(n)},s.prototype.next=function(){return this.token=this.tokens.pop()},s.prototype.peek=function(){return this.tokens[this.tokens.length-1]||0},s.prototype.parseText=function(){for(var e=this.token.text;"text"===this.peek().type;)e+="\n"+this.next().text;return this.inline.output(e)},s.prototype.tok=function(){var e=[];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,s,r,i=[],o=[];for(t=0;t<this.token.header.length;t++)s={header:!0,align:this.token.align[t]},o.push(this.renderer.tablecell(this.inline.output(this.token.header[t]),{header:!0,align:this.token.align[t]}));for(i.push(this.renderer.tablerow(this.renderer.combine(o))),t=0;t<this.token.cells.length;t++){for(n=this.token.cells[t],o=[],r=0;r<n.length;r++)o.push(this.renderer.tablecell(this.inline.output(n[r]),{header:!1,align:this.token.align[r]}));e.push(this.renderer.tablerow(this.renderer.combine(o)))}return this.renderer.table(this.renderer.combine(i),this.renderer.combine(e));case"blockquote_start":for(;"blockquote_end"!==this.next().type;)e.push(this.tok());return this.renderer.blockquote(this.renderer.combine(e));case"list_start":for(var l=this.token.ordered,h=!1;"list_end"!==this.next().type;)void 0!==this.token.checked&&(h=!0),e.push(this.tok());return this.renderer.list(this.renderer.combine(e),l,h);case"list_item_start":for(var a=this.token.checked;"list_item_end"!==this.next().type;)e.push("text"===this.token.type?this.parseText():this.tok());return this.renderer.listitem(this.renderer.combine(e),a);case"loose_item_start":for(;"list_item_end"!==this.next().type;)e.push(this.tok());return this.renderer.listitem(this.renderer.combine(e),a);case"html":var p=this.token.pre||this.options.pedantic?this.token.text:this.inline.output(this.token.text);return this.renderer.html(p);case"paragraph":return this.renderer.paragraph(this.inline.output(this.token.text));case"text":return this.renderer.paragraph(this.parseText())}},o.exec=o,h.options=h.setOptions=function(e){return l(h.defaults,e),h},h.defaults={gfm:!0,tables:!0,breaks:!1,pedantic:!1,sanitize:!1,sanitizer:null,mangle:!0,smartLists:!1,silent:!1,highlight:null,langPrefix:"lang-",smartypants:!1,headerPrefix:"",renderer:new n,xhtml:!1},h.Parser=s,h.parser=s.parse,h.Renderer=n,h.Lexer=e,h.lexer=e.lex,h.InlineLexer=t,h.inlineLexer=t.output,h.parse=h,"undefined"!=typeof module&&"object"==typeof exports?module.exports=h:"function"==typeof define&&define.amd?define(function(){return h}):this.marked=h}).call(function(){return this||("undefined"!=typeof window?window:global)}());