UNPKG

@flyffydiscord2/snarkdown

Version:
1 lines • 2.81 kB
const e={"":[`<em>`,`</em>`],_:[`<strong>`,`</strong>`],"*":[`<strong>`,`</strong>`],"~":[`<s>`,`</s>`],"\n":[`<br />`]," ":[`<br />`],"-":[`<hr />`]};function t(e){return e.replace(RegExp(`^`+(e.match(/^(\t| )+/)||``)[0],`gm`),``)}function n(e){return(e+``).replace(/"/g,`&quot;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`)}function r(e){return e.trim().replace(/^\||\|$/g,``).split(`|`).map(e=>e.trim())}function i(e,t){return a(e,t)}function a(i,o,s){let c=/((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n(?:[ \t]*\n)*|\n(?:[ \t]*\n)+|__|\*\*|[_*]|~~)|(?:^|\n+)([^\n]*\|[^\n]*\n)(\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?)\n?((?:[^\n]*\|[^\n]*\n?)*)|(\bhttps?:\/\/(?:&(?!quot;|lt;|gt;)|\](?!\()|[^\s<>"&\]])*[^\s<>"&.,:;!?')\]*_~])/gm,l=[],u=``,d=[],f=o||{},p=0,m,h,g,_,v,y;function b(t){let n=e[t[1]||``],r=l[l.length-1]==t;return n?n[1]?(r?l.pop():l.push(t),n[r|0]):n[0]:t}function x(){let e=``;for(;l.length;)e+=b(l[l.length-1]);return e}function S(){u=(u+x()).replace(/^\s+|\s+$/g,``),u&&d.push([u,1]),u=``}for(i=i.replace(/^\[(.+?)\]:\s*(.+)$/gm,(e,t,n)=>(f[t.toLowerCase()]=n,``)).replace(/^\n+|\n+$/g,``);g=c.exec(i);){if(h=i.substring(p,g.index),p=c.lastIndex,m=g[0],y=0,!h.match(/[^\\](\\\\)*\\$/)){if(v=g[3]||g[4])m=`<pre class="code `+(g[4]?`poetry`:g[2].toLowerCase())+`"><code`+(g[2]?` class="language-${g[2].toLowerCase()}"`:``)+`>`+t(n(v).replace(/^\n+|\n+$/g,``))+`</code></pre>`,y=1;else if(v=g[6])v.match(/\./)&&(g[5]=g[5].replace(/^\d+/gm,``)),_=a(t(g[5].replace(/^\s*[>*+.-]/gm,``)),0,1),v==`>`?v=`blockquote`:(v=v.match(/\./)?`ol`:`ul`,_=_.replace(/^(.*)(\n|$)/gm,`<li>$1</li>`)),m=`<`+v+`>`+_+`</`+v+`>`,y=1;else if(g[8])m=`<img src="${n(g[8])}" alt="${n(g[7])}">`;else if(g[10])u=u.replace(`<a>`,`<a href="${n(g[11]||f[h.toLowerCase()])}">`),m=x()+`</a>`;else if(g[9])m=`<a>`;else if(g[12]||g[14])v=`h`+(g[14]?g[14].length:g[13]>`=`?1:2),m=`<`+v+`>`+a(g[12]||g[15],f,1)+`</`+v+`>`,y=1;else if(g[16])m=`<code>`+n(g[16])+`</code>`;else if(!s&&/\n[ \t]*\n/.test(g[17]))y=2;else if(g[17]||g[1])m=b(g[17]||`--`),y=+!!g[1];else if(g[18]){let e=(e,t)=>`<tr>`+r(e).map(e=>`<`+t+`>`+a(e,f,1)+`</`+t+`>`).join(``)+`</tr>`;m=`<table><thead>`+e(g[18],`th`)+`</thead><tbody>`+(g[20].match(/[^\n]+/g)||[]).map(t=>e(t,`td`)).join(``)+`</tbody></table>`,y=1}else g[21]&&u.indexOf(`<a>`)<0&&(m=`<a href="${n(g[21])}">${g[21]}</a>`)}u+=h,s||!y?u+=m:(S(),y==1&&d.push([m]))}return u+=i.substring(p),s?(u+x()).replace(/^\n+|\n+$/g,``):(S(),d.length==1&&d[0][1]?d[0][0]:d.map(e=>e[1]?`<p>`+e[0]+`</p>`:e[0]).join(``))}module.exports=i;