farallon/dist/js/Markdown.min.js

1 line
9.3 KiB
JavaScript
Raw Normal View History

2024-05-17 15:47:27 +08:00
self.addEventListener("message",function(e){let data=e.data;let mdText=data.mdText;let markdown=new Markdown({mdText:mdText});let htmlText=markdown.markdown();self.postMessage({htmlText:htmlText})},false);class Markdown{constructor(props={mdText:""}){this.h1Reg=/^([-+*]*)(&nbsp;)*(#)&nbsp;(.+)/;this.h2Reg=/^([-+*]*)(&nbsp;)*(#{2})&nbsp;(.+)/;this.h3Reg=/^([-+*]*)(&nbsp;)*(#{3})&nbsp;(.+)/;this.h4Reg=/^([-+*]*)(&nbsp;)*(#{4})&nbsp;(.+)/;this.h5Reg=/^([-+*]*)(&nbsp;)*(#{5})&nbsp;(.+)/;this.h6Reg=/^([-+*]*)(&nbsp;)*(#{6})&nbsp;(.+)/;this.hrReg=/^[-+*_]{3,}$/;this.aReg=/\[([^\[\]]+?)]\(([^()]+?)\)/;this.imgReg=/!\[([^\[\]]+?)]\(([^()]+?)\)/;this.strongReg=/\*\*(.+?)\*\*/;this.emReg=/\*(.+?)\*/;this.delReg=/~~(.+?)~~/;this.markReg=/==(.+?)==/;this.subReg=/~(.+?)~/;this.supReg=/\^(.+?)\^/;this.inlineCodeReg=/`{1,2}(.+?)`{1,2}/;this.ulReg=/\n([-+*].*(\n[-+*].*)*)/;this.olReg=/\n([1-9]+\..*(\n[0-9]+\..*)*)/;this.blockquoteReg=/\n(&gt;.*(\n&gt;.*)*)/;this.codeReg=/\n```\n([\s\S]*?)\n```[\n]?/;this.tableReg=/\n(\|.*?\|<br>\n(\|(nbsp;)*-+)+\|<br>\n(\|.*?\|<br>\n)*)/;this.tranRegs=[{reg:/\\\\/,to:"<tranBackslash>"},{reg:/\\&amp;/,to:"<tranAnd>"},{reg:/\\&nbsp;/,to:"<tranSpace>"},{reg:/\\&lt;/,to:"<tranLess>"},{reg:/\\&gt;/,to:"<tranGreater>"},{reg:/\\&#39;/,to:"<tranSingleQuotation>"},{reg:/\\&quot;/,to:"<tranDoubleQuotation>"},{reg:/\\#/,to:"<tranNumberSign>"},{reg:/\\=/,to:"<tranEqual>"},{reg:/\\\+/,to:"<tranAdd>"},{reg:/\\-/,to:"<tranReduce>"},{reg:/\\\*/,to:"<tranAsterisk>"},{reg:/\\_/,to:"<tranUnderline>"},{reg:/\\`/,to:"<tranBackquote>"},{reg:/\\\|/,to:"<tranDelimiter>"},{reg:/\\~/,to:"<tranWaveSign>"},{reg:/\\\^/,to:"<tranPower>"},{reg:/\\!/,to:"<tranExclamatoryMark>"},{reg:/\\{/,to:"<tranLeftCurlyBracket>"},{reg:/\\}/,to:"<tranRightCurlyBracket>"},{reg:/\\\[/,to:"<tranLeftSquareBracket>"},{reg:/\\]/,to:"<tranRightSquareBracket>"},{reg:/\\\(/,to:"<tranLeftBracket>"},{reg:/\\\)/,to:"<tranRightBracket>"},{reg:/\\./,to:"<tranDot>"}];this.tranToRegs=[{reg:/<tranBackslash>/,to:"\\"},{reg:/<tranAnd>/,to:"&amp;"},{reg:/<tranSpace>/,to:"\\&nbsp;"},{reg:/<tranLess>/,to:"&lt;"},{reg:/<tranGreater>/,to:"&gt;"},{reg:/<tranSingleQuotation>/,to:"&#39;"},{reg:/<tranDoubleQuotation>/,to:"&quot;"},{reg:/<tranNumberSign>/,to:"#"},{reg:/<tranEqual>/,to:"="},{reg:/<tranAdd>/,to:"+"},{reg:/<tranReduce>/,to:"-"},{reg:/<tranAsterisk>/,to:"*"},{reg:/<tranUnderline>/,to:"_"},{reg:/<tranBackquote>/,to:"`"},{reg:/<tranDelimiter>/,to:"|"},{reg:/<tranWaveSign>/,to:"~"},{reg:/<tranPower>/,to:"^"},{reg:/<tranExclamatoryMark>/,to:"!"},{reg:/<tranLeftCurlyBracket>/,to:"{"},{reg:/<tranRightCurlyBracket>/,to:"}"},{reg:/<tranLeftSquareBracket>/,to:"["},{reg:/<tranRightSquareBracket>/,to:"]"},{reg:/<tranLeftBracket>/,to:"("},{reg:/<tranRightBracket>/,to:")"},{reg:/<tranDot>/,to:"."}];let htmlEncode=str=>{let s;if(str.length===0){return""}s=str.replace(/&/g,"&amp;").replace(/ /g,"&nbsp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/'/g,"&#39;").replace(/"/g,"&quot;");return s};this.props=props;this.props.mdText=htmlEncode(this.props.mdText)}markdown(){const replaceTran=htmlText=>{let reg;let i=0;for(let tranReg of this.tranRegs){while(reg=tranReg.reg.exec(htmlText)){htmlText=htmlText.replace(tranReg.reg,tranReg.to)}i++}return htmlText};const replaceTranTo=htmlText=>{let reg;let i=0;for(let tranToReg of this.tranToRegs){while(reg=tranToReg.reg.exec(htmlText)){htmlText=htmlText.replace(tranToReg.reg,tranToReg.to)}i++}return htmlText};const replaceA=htmlText=>{let reg;while(reg=this.aReg.exec(htmlText)){htmlText=htmlText.replace(this.aReg,`<a class="md-a" href="${reg[2]}" target="_blank">${reg[1]}</a>`)}return htmlText};const replaceImg=htmlText=>{let reg;while(reg=this.imgReg.exec(htmlText)){htmlText=htmlText.replace(this.imgReg,`<img class="md-img" src="${reg[2]}" alt="${reg[1]}"/>`)}return htmlText};const replaceStrong=htmlText=>{let reg;while(reg=this.strongReg.exec(htmlText)){htmlText=htmlText.replace(this.strongReg,`<strong class="md-strong">${reg[1]}</strong>`)}return htmlText};const replaceEm=htmlText=>{let reg;while(reg=this.emReg.exec(htmlText)){ht