15 lines
38 KiB
JavaScript
15 lines
38 KiB
JavaScript
|
function smartypants(e){return e.replace(/---/g,"—").replace(/--/g,"–").replace(/(^|[-\u2014/(\[{"\s])'/g,"$1‘").replace(/'/g,"’").replace(/(^|[-\u2014/(\[{\u2018\s])"/g,"$1“").replace(/"/g,"”").replace(/\.{3}/g,"…")}function mangle(e){let t="",n,r;var i=e.length;for(n=0;n<i;n++)r=e.charCodeAt(n),.5<Math.random()&&(r="x"+r.toString(16)),t+="&#"+r+";";return t}class Lexer{constructor(e){this.tokens=[],this.tokens.links=Object.create(null),this.options=e||defaults,this.options.tokenizer=this.options.tokenizer||new Tokenizer,this.tokenizer=this.options.tokenizer,this.tokenizer.options=this.options,(this.tokenizer.lexer=this).inlineQueue=[],this.state={inLink:!1,inRawBlock:!1,top:!0};e={block:block.normal,inline:inline.normal};this.options.pedantic?(e.block=block.pedantic,e.inline=inline.pedantic):this.options.gfm&&(e.block=block.gfm,this.options.breaks?e.inline=inline.breaks:e.inline=inline.gfm),this.tokenizer.rules=e}static get rules(){return{block:block,inline:inline}}static lex(e,t){return new Lexer(t).lex(e)}static lexInline(e,t){return new Lexer(t).inlineTokens(e)}lex(e){var t;for(e=e.replace(/\r\n|\r/g,"\n"),this.blockTokens(e,this.tokens);t=this.inlineQueue.shift();)this.inlineTokens(t.src,t.tokens);return this.tokens}blockTokens(r,t=[]){r=this.options.pedantic?r.replace(/\t/g," ").replace(/^ +$/gm,""):r.replace(/^( *)(\t+)/gm,(e,t,n)=>t+" ".repeat(n.length));let n,e,i,s;for(;r;)if(!(this.options.extensions&&this.options.extensions.block&&this.options.extensions.block.some(e=>!!(n=e.call({lexer:this},r,t))&&(r=r.substring(n.raw.length),t.push(n),!0))))if(n=this.tokenizer.space(r))r=r.substring(n.raw.length),1===n.raw.length&&0<t.length?t[t.length-1].raw+="\n":t.push(n);else if(n=this.tokenizer.code(r))r=r.substring(n.raw.length),!(e=t[t.length-1])||"paragraph"!==e.type&&"text"!==e.type?t.push(n):(e.raw+="\n"+n.raw,e.text+="\n"+n.text,this.inlineQueue[this.inlineQueue.length-1].src=e.text);else if(n=this.tokenizer.fences(r))r=r.substring(n.raw.length),t.push(n);else if(n=this.tokenizer.heading(r))r=r.substring(n.raw.length),t.push(n);else if(n=this.tokenizer.hr(r))r=r.substring(n.raw.length),t.push(n);else if(n=this.tokenizer.blockquote(r))r=r.substring(n.raw.length),t.push(n);else if(n=this.tokenizer.list(r))r=r.substring(n.raw.length),t.push(n);else if(n=this.tokenizer.html(r))r=r.substring(n.raw.length),t.push(n);else if(n=this.tokenizer.def(r))r=r.substring(n.raw.length),!(e=t[t.length-1])||"paragraph"!==e.type&&"text"!==e.type?this.tokens.links[n.tag]||(this.tokens.links[n.tag]={href:n.href,title:n.title}):(e.raw+="\n"+n.raw,e.text+="\n"+n.raw,this.inlineQueue[this.inlineQueue.length-1].src=e.text);else if(n=this.tokenizer.table(r))r=r.substring(n.raw.length),t.push(n);else if(n=this.tokenizer.lheading(r))r=r.substring(n.raw.length),t.push(n);else{if(i=r,this.options.extensions&&this.options.extensions.startBlock){let t=1/0;const a=r.slice(1);let n;this.options.extensions.startBlock.forEach(function(e){"number"==typeof(n=e.call({lexer:this},a))&&0<=n&&(t=Math.min(t,n))}),t<1/0&&0<=t&&(i=r.substring(0,t+1))}if(this.state.top&&(n=this.tokenizer.paragraph(i)))e=t[t.length-1],s&&"paragraph"===e.type?(e.raw+="\n"+n.raw,e.text+="\n"+n.text,this.inlineQueue.pop(),this.inlineQueue[this.inlineQueue.length-1].src=e.text):t.push(n),s=i.length!==r.length,r=r.substring(n.raw.length);else if(n=this.tokenizer.text(r))r=r.substring(n.raw.length),(e=t[t.length-1])&&"text"===e.type?(e.raw+="\n"+n.raw,e.text+="\n"+n.text,this.inlineQueue.pop(),this.inlineQueue[this.inlineQueue.length-1].src=e.text):t.push(n);else if(r){var l="Infinite loop on byte: "+r.charCodeAt(0);if(this.options.silent){console.error(l);break}throw new Error(l)}}return this.state.top=!0,t}inline(e,t=[]){return this.inlineQueue.push({src:e,tokens:t}),t}inlineTokens(r,t=[]){let n,e,i,s=r,l,a,o;if(this.tokens.links){var c=Object.keys(this.tokens.links);if(0<c.length)for(;null!=(l=this.tokenizer.rules.inline.reflinkSearch.exec(s));)c.includes(l[0].slice(l[0].lastIndexOf("[")+1,-1))&&(s=s.slice(0,l.index)+"["+repeatString("a",l[0].length-2)+"]"+s
|
|||
|
${e}</blockquote>
|
|||
|
`}html(e){return e}heading(e,t,n,r){return this.options.headerIds?`<h${t} id="${this.options.headerPrefix+r.slug(n)}">${e}</h${t}>
|
|||
|
`:`<h${t}>${e}</h${t}>
|
|||
|
`}hr(){return this.options.xhtml?"<hr/>\n":"<hr>\n"}list(e,t,n){var r=t?"ol":"ul";return"<"+r+(t&&1!==n?' start="'+n+'"':"")+">\n"+e+"</"+r+">\n"}listitem(e){return`<li>${e}</li>
|
|||
|
`}checkbox(e){return"<input "+(e?'checked="" ':"")+'disabled="" type="checkbox"'+(this.options.xhtml?" /":"")+"> "}paragraph(e){return`<p>${e}</p>
|
|||
|
`}table(e,t){return"<table>\n<thead>\n"+e+"</thead>\n"+(t=t&&`<tbody>${t}</tbody>`)+"</table>\n"}tablerow(e){return`<tr>
|
|||
|
${e}</tr>
|
|||
|
`}tablecell(e,t){var n=t.header?"th":"td";return(t.align?`<${n} align="${t.align}">`:`<${n}>`)+e+`</${n}>
|
|||
|
`}strong(e){return`<strong>${e}</strong>`}em(e){return`<em>${e}</em>`}codespan(e){return`<code>${e}</code>`}br(){return this.options.xhtml?"<br/>":"<br>"}del(e){return`<del>${e}</del>`}link(e,t,n){if(null===(e=cleanUrl(this.options.sanitize,this.options.baseUrl,e)))return n;let r='<a href="'+e+'"';return t&&(r+=' title="'+t+'"'),r+=">"+n+"</a>"}image(e,t,n){if(null===(e=cleanUrl(this.options.sanitize,this.options.baseUrl,e)))return n;let r=`<img src="${e}" alt="${n}"`;return t&&(r+=` title="${t}"`),r+=this.options.xhtml?"/>":">"}video(e,t,n){return null===(e=cleanUrl(this.options.sanitize,this.options.baseUrl,e))?n:`<video controls width="100%" alt="${n}" >
|
|||
|
<source src="${e}" ></source>
|
|||
|
</video>`}audio(e,t,n){return null===(e=cleanUrl(this.options.sanitize,this.options.baseUrl,e))?n:`<audio controls width="100%" alt="${n}" >
|
|||
|
<source src="${e}" ></source>
|
|||
|
</audio>`}text(e){return e}}class Slugger{constructor(){this.seen={}}serialize(e){return e.toLowerCase().trim().replace(/<[!\/a-z].*?>/gi,"").replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g,"").replace(/\s/g,"-")}getNextSafeSlug(e,t){let n=e,r=0;if(this.seen.hasOwnProperty(n))for(r=this.seen[e];r++,n=e+"-"+r,this.seen.hasOwnProperty(n););return t||(this.seen[e]=r,this.seen[n]=0),n}slug(e,t={}){e=this.serialize(e);return this.getNextSafeSlug(e,t.dryrun)}}class TextRenderer{strong(e){return e}em(e){return e}codespan(e){return e}del(e){return e}html(e){return e}text(e){return e}link(e,t,n){return""+n}image(e,t,n){return""+n}video(e,t,n){return""+n}audio(e,t,n){return""+n}br(){return""}}function outputLink(e,t,n,r){var i=t.href,s=t.title?escape(t.title):null,l=e[1].replace(/\\([\[\]])/g,"$1");return console.log("outputLink() link=",t,"====================="),"!"!==e[0].charAt(0)?(r.state.inLink=!0,t={type:"link",raw:n,href:i,title:s,text:l,tokens:r.inlineTokens(l)},r.state.inLink=!1,console.log("outputLink(): cap=",e[0],"return a link"),t):e[0].startsWith("!v")?(console.log("outputLink(): cap=",e[0],"return a video"),{type:"video",raw:n,href:i,title:s,text:escape(l)}):e[0].startsWith("!a")?(console.log("outputLink(): cap=",e[0],"return a audio"),{type:"audio",raw:n,href:i,title:s,text:escape(l)}):(console.log("outputLink(): cap=",e[0],"return a image"),{type:"image",raw:n,href:i,title:s,text:escape(l)})}function indentCodeCompensation(e,t){e=e.match(/^(\s+)(?:```)/);if(null===e)return t;const n=e[1];return t.split("\n").map(e=>{var t=e.match(/^\s+/);return null!==t&&([t]=t,t.length>=n.length)?e.slice(n.length):e}).join("\n")}class Tokenizer{constructor(e){this.options=e||defaults}space(e){e=this.rules.block.newline.exec(e);if(e&&0<e[0].length)return{type:"space",raw:e[0]}}code(e){var t,e=this.rules.block.code.exec(e);if(e)return t=e[0].replace(/^ {1,4}/gm,""),{type:"code",raw:e[0],codeBlockStyle:"indented",text:this.options.pedantic?t:rtrim(t,"\n")}}fences(e){var t,n,e=this.rules.block.fences.exec(e);if(e)return n=indentCodeCompensation(t=e[0],e[3]||""),{type:"code",raw:t,lang:e[2]&&e[2].trim().replace(this.rules.inline._escapes,"$1"),text:n}}heading(t){var n,t=this.rules.block.heading.exec(t);if(t){let e=t[2].trim();return/#$/.test(e)&&(n=rtrim(e,"#"),!this.options.pedantic&&n&&!/ $/.test(n)||(e=n.trim())),{type:"heading",raw:t[0],depth:t[1].length,text:e,tokens:this.lexer.inline(e)}}}hr(e){e=this.rules.block.hr.exec(e);if(e)return{type:"hr",raw:e[0]}}blockquote(e){var t,n,r,e=this.rules.block.blockquote.exec(e);if(e)return t=e[0].replace(/^ *>[ \t]?/gm,""),n=this.lexer.state.top,this.lexer.state.top=!0,r=this.lexer.blockTokens(t),this.lexer.state.top=n,{type:"blockquote",raw:e[0],tokens:r,text:t}}list(g){let k=this.rules.block.list.exec(g);if(k){let e,t,n,r,i,s,l,a,o,c,p,h,u=k[1].trim();for(var d=1<u.length,f={type:"list",raw:"",ordered:d,start:d?+u.slice(0,-1):"",loose:!1,items:[]},m=(u=d?"\\d{1,9}\\"+u.slice(-1):"\\"+u,this.options.pedantic&&(u=d?u:"[*+-]"),new RegExp(`^( {0,3}${u})((?:[ ][^\\n]*)?(?:\\n|$))`));g&&(h=!1,k=m.exec(g))&&!this.rules.block.hr.test(g);){if(e=k[0],g=g.substring(e.length),a=k[2].split("\n",1)[0].replace(/^\t+/,e=>" ".repeat(3*e.length)),o=g.split("\n",1)[0],this.options.pedantic?(r=2,p=a.trimLeft()):(r=4<(r=k[2].search(/[^ ]/))?1:r,p=a.slice(r),r+=k[1].length),s=!1,!a&&/^ *$/.test(o)&&(e+=o+"\n",g=g.substring(o.length+1),h=!0),!h)for(var x=new RegExp(`^ {0,${Math.min(3,r-1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ ][^\\n]*)?(?:\\n|$))`),b=new RegExp(`^ {0,${Math.min(3,r-1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`),w=new RegExp(`^ {0,${Math.min(3,r-1)}}(?:\`\`\`|~~~)`),_=new RegExp(`^ {0,${Math.min(3,r-1)}}#`);g&&(c=g.split("\n",1)[0],o=c,this.options.pedantic&&(o=o.replace(/^ {1,4}(?=( {4})*[^ ])/g," ")),!w.test(o))&&!_.test(o)&&!x.test(o)&&!b.test(g);){if(o.search(/[^ ]/)>=r||!o.trim())p+="\n"+o.slice(r);else{if(s)break;if(4<=a.search(/[^ ]/))break;if(w.test(a))break;if(_.test(a))break;if(b.test(a))break;p+="\n"+o}s||o.trim()||(s=!0),e+=c+"\n",g=g.substring(c.length
|