1 Star 0 Fork 38

K./mdeditor

forked from 笔心/mdeditor 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mdeditor.min.js 5.05 KB
一键复制 编辑 原始数据 按行查看 历史
笔心 提交于 2016-01-22 17:26 . no commit message
(function(e){var t=function(e){return new t.prototype.init(e)};t.prototype={init:function(e){var t=this;var r={id:"",placeholder:"",name:"",aTarget:"_blank"};if(e&&e.id){t.copy(r,e);var i=this.getDom(e.id);var n='<textarea id="mdeditor" class="mdeditor" name="{name}" placeholder="{placeholder}"></textarea><div id="mdeditor-html" class="mdeditor-html"></div>';n=t.formatString(n,r);i.innerHTML=n;var a=this.getDom("mdeditor");var l=this.getDom("mdeditor-html");a.addEventListener("input",function(){var e=this.value;t.markdownToHtml(e)});t.editor=a;t.editor2Html=l}t.options=r;return t},regLib:{code:/^\`{3}.*$/,ul:/^[\.\-\*]\s?.+$/,ol:/^\d+\.\s?.+$/,toc:/^\s*\[TOC\]\s*$/,img:/\!\[(.*?)\]\((.*?)\)/g,title:/^#{1,6}.+$/,a:/\[(.*?)\]\((.*?)\)/g,b:/\*\*(.+?)\*\*/g,inline_code:/\`(.+?)\`/g,table:/^(\|[^|]+)+\|$/,table_td_align:/^(\|\s*:?-+:?\s*)+\|$/,table_td_align_left:/^\s*:-+\s*$/,table_td_align_center:/^\s*:-+:\s*$/,table_td_align_right:/^\s*-+:\s*$/},formatString:function(e,t){return e.replace(/{\w+}/g,function(e){var r=e.substr(1,e.length-2);return t[r]})},copy:function(e,t){for(var r in t){e[r]=t[r]}return e},getDom:function(e){return document.getElementById(e)},getMarkdown:function(){if(this.editor){return this.editor.value}else{return null}},setMarkdown:function(e){if(this.editor){this.editor.value=e}return this.markdownToHtml(e)},getHTML:function(){if(this.editor2Html){return this.editor2Html.innerHTML}else{return""}},markdownToHtml:function(e){var t=this;var r=e.match(/.+/gm)||[];var i=[];var n="";var a="";var l=r.length;var s=0;var o=null;var d=[];if(l&&t.regLib.toc.test(r[0])){s=1;o=['<div class="mdeditor-toc">']}for(var h=s;h<l;h++){var u=r[h];u=t.replaceHtmlTag(u);console.log(u);switch(n){case"ol":if(!t.regLib.ol.test(u)){h--;n="";i.push("</ol>")}else{i.push(t.handleOrderList(u));if(h==l-1){i.push("</ol>")}}break;case"ul":if(!t.regLib.ul.test(u)){h--;n="";i.push("</ul>")}else{i.push(t.handleUnorderedList(u));if(h==l-1){i.push("</ul>")}}break;case"code":if(t.regLib.code.test(u)){n="";i.push("</code>");i.push("</pre>")}else{i.push("<li><div>"+t.handleCodeType(a,u)+"</div></li>")}break;case"table":if(!t.regLib.table.test(u)){h--;n="";i.push("</table>")}else{i.push(t.handleTr(u,d));if(h==l-1){i.push("</table>")}}break;default:if(t.regLib.title.test(u)){i.push(t.handleTitle(u,o))}else if(t.regLib.ol.test(u)){h--;n="ol";i.push('<ol class="mdeditor-ol">')}else if(t.regLib.ul.test(u)){h--;n="ul";i.push('<ul class="mdeditor-ul">')}else if(t.regLib.code.test(u)){n="code";a=u.replace(/[`\s]/g,"");i.push('<pre class="mdeditor-code mdeditor-code-'+a.toLowerCase()+'">');i.push("<ol>")}else if(t.regLib.table.test(u)){if(h!=l-1&&t.regLib.table_td_align.test(r[h+1])){n="table";i.push('<table class="mdeditor-table"><tr>');var c=u.match(/[^|]+/g);d=t.handleTdAlign(r[h+1]);for(var p=0,g=c.length;p<g;p++){i.push('<th align="'+d[p]+'">'+c[p]+"</th>")}i.push("</tr>");h++;if(h==l-1){i.push("</table>")}}else{i.push(t.handleParagraph(u))}}else if(t.regLib.img.test(u)){i.push(t.handleImg(u))}else{i.push(t.handleParagraph(u))}break}}i=(o?o.join("")+"</div>":"")+i.join("");console.log(i);if(this.editor2Html){this.editor2Html.innerHTML=i}return i},handleTr:function(e,t){var r=e.match(/[^|]+/g);var i="<tr>";for(var n=0,a=r.length;n<a;n++){i+='<td align="'+t[n]+'">'+this.handleInlineSet(r[n])+"</td>"}i+="</tr>";return i},handleTdAlign:function(e){var t=e.match(/[^|]+/g);var r=[];for(var i=0,n=t.length;i<n;i++){if(this.regLib.table_td_align_right.test(t[i])){r.push("right")}else if(this.regLib.table_td_align_center.test(t[i])){r.push("center")}else{r.push("left")}}return r},handleTitle:function(e,t){var r=this;return e.replace(/(#{1,6})(.+)/,function(e,r,i){var n=r.length;if(t){t.push('<a class="mdeditor-toc-'+n+'" href="#'+i+'">'+i+"</a>")}return"<h"+n+' id="'+i+'" >'+i+"</h"+n+">"})},handleParagraph:function(e){return"<p>"+this.handleInlineSet(e)+"</p>"},handleInlineSet:function(e){e=this.handleInlineCode(e);e=this.handleLink(e);e=this.handleBold(e);return e},handleImg:function(e){return'<p class="mdeditor-img">'+e.replace(this.regLib.img,function(e,t,r){return'<img alt="'+t+'" src="'+r+'">'})+"</p>"},handleLink:function(e){var t=this;return e.replace(t.regLib.a,function(e,r,i){r=t.handleBold(r);return'<a href="'+i+'" target="'+t.options.aTarget+'">'+r+"</a>"})},handleBold:function(e){var t=this;return e.replace(t.regLib.b,function(e,t){return"<b>"+t+"</b>"})},handleInlineCode:function(e){var t=this;return e.replace(t.regLib.inline_code,function(e,t){return'<span class="mdeditor-inline-code">'+t+"</span>"})},handleUnorderedList:function(e){e=e.replace(/^[\.\*\-]\s*/,"");e=this.handleInlineSet(e);return"<li>"+e+"</li>"},handleOrderList:function(e){e=e.replace(/^\d+\.\s*/,"");e=this.handleInlineSet(e);return"<li>"+e+"</li>"},handleCodeType:function(e,t){switch(e){case"css":return t.replace(/([a-zA-Z-]+:)([^;]+)(;?)/g,'<span class="css-property-name">$1</span><span class="css-property-value">$2</span><span class="css-semicolon">$3</span>');default:return t}},replaceHtmlTag:function(e){return e.replace(/\</g,"&lt;").replace(/\>/,"&gt;")}};t.prototype.init.prototype=t.prototype;e.mdeditor=t})(window);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/janpoem/mdeditor.git
[email protected]:janpoem/mdeditor.git
janpoem
mdeditor
mdeditor
master

搜索帮助