1 line
9.3 KiB
JavaScript
1 line
9.3 KiB
JavaScript
|
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=/^([-+*]*)( )*(#) (.+)/;this.h2Reg=/^([-+*]*)( )*(#{2}) (.+)/;this.h3Reg=/^([-+*]*)( )*(#{3}) (.+)/;this.h4Reg=/^([-+*]*)( )*(#{4}) (.+)/;this.h5Reg=/^([-+*]*)( )*(#{5}) (.+)/;this.h6Reg=/^([-+*]*)( )*(#{6}) (.+)/;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(>.*(\n>.*)*)/;this.codeReg=/\n```\n([\s\S]*?)\n```[\n]?/;this.tableReg=/\n(\|.*?\|<br>\n(\|(nbsp;)*-+)+\|<br>\n(\|.*?\|<br>\n)*)/;this.tranRegs=[{reg:/\\\\/,to:"<tranBackslash>"},{reg:/\\&/,to:"<tranAnd>"},{reg:/\\ /,to:"<tranSpace>"},{reg:/\\</,to:"<tranLess>"},{reg:/\\>/,to:"<tranGreater>"},{reg:/\\'/,to:"<tranSingleQuotation>"},{reg:/\\"/,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:"&"},{reg:/<tranSpace>/,to:"\\ "},{reg:/<tranLess>/,to:"<"},{reg:/<tranGreater>/,to:">"},{reg:/<tranSingleQuotation>/,to:"'"},{reg:/<tranDoubleQuotation>/,to:"""},{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,"&").replace(/ /g," ").replace(/</g,"<").replace(/>/g,">").replace(/'/g,"'").replace(/"/g,""");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
|