-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmarkdown-helper.min.js
More file actions
7 lines (7 loc) · 1.07 KB
/
markdown-helper.min.js
File metadata and controls
7 lines (7 loc) · 1.07 KB
1
2
3
4
5
6
7
/*
Title: Markdown Helper
URL: http://github.com/jamiebicknell/Markdown-Helper
Author: Jamie Bicknell
Twitter: @jamiebicknell
*/
function MarkdownHelper(e,t){var n,r,i,s,o,u,a,f,l,c,h,p,d;if(t.keyCode===13){n=false;r=e.value.replace(/\r\n/g,"\n");if(e.selectionStart){i=e.selectionStart}else{e.focus();s=document.selection.createRange();s.moveStart("character",-r.length);i=s.text.replace(/\r\n/g,"\n").length}o=r.split("\n");u=r.substr(0,i).split("\n").length;a=o[u-1].replace(/^\s+/,"");f=a.substr(0,2);if((new RegExp("^[0-9]+[.] (.*)$")).test(a)){l=a.substr(0,a.indexOf(". "));h=l+". ";c=parseInt(l,10)+1+". ";n=true}if(a&&!n&&o[u-1].substr(0,4)===" "){h=c=" ";n=true}if(["* ","+ ","- ","> "].indexOf(f)>=0){h=c=f;n=true}if(n){p=o[u-1].indexOf(h);if(a.replace(/^\s+/,"")===h){e.value=r.substr(0,i-1-p-c.length)+"\n\n"+r.substr(i,r.length);d=i+1-c.length-p}else{e.value=r.substr(0,i)+"\n"+(new Array(p+1)).join(" ")+c+r.substr(i,r.length);d=i+1+c.length+p}if(e.selectionStart){e.setSelectionRange(d,d)}else{s=e.createTextRange();s.move("character",d);s.select()}return false}}}