pyfedi/app/static/js/markdown/index.min.js

1 line
11 KiB
JavaScript

var easyMarkdown=function(){"use strict";var t=new RegExp("^(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))(?::\\d{2,5})?(?:/\\S*)?$","i"),e=new RegExp("^(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))(?::\\d{2,5})?(?:/\\S*)?(?:jpg|gif|png)$","i");function i(t){var e=[];for(var i in t){var n=document.createElement(t[i].type);for(var l in t[i].attrs)n.setAttribute(l,t[i].attrs[l]);t[i].text&&n.appendChild(document.createTextNode(t[i].text)),e[i]=n,void 0!==t[i].childrenOf&&e[t[i].childrenOf].appendChild(n)}return e[0]}function n(t,e,i){var n=document.createElement(t);for(var l in e)n.setAttribute(l,e[l]);return i&&n.appendChild(document.createTextNode(i)),n}function l(t){for(var e,i,n=1;n<arguments.length;n+=1)for(i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}function s(t,e){return new c(t,e)}function r(i,n,r){return this.element=i,this.options=n,this.locale=l({},s.locale,s.locale[n.locale]||{}),this.buttons={header:{title:this.locale.header.title,text:"header",group:0,callback:function(t){var e,i,n=t.getSelection(),l=t.getContent();e=0===n.length?t.locale.header.description+"\n":n.text+"\n";for(var s=0,r="",o=n.start-1,a=n.start,c=l.substring(o,a);/^\s+$|^#+$/.test(c);)/^#+$/.test(c)&&(r+="#"),s+=1,c=l.substring(o-s,a-s);if(r.length>0){var d,p=l.indexOf("\n",n.start);r.length>2?(r="#",d=l.indexOf("\n",n.start-5),t.setSelection(d,p+1),t.replaceSelection("\n"+r+" "+e),i=d+3):(r+="#",d=l.indexOf("\n",n.start-(r.length+1)),t.setSelection(d,p+1),t.replaceSelection("\n"+r+" "+e),i=n.start+1)}else r="#",t.replaceSelection("\n"+r+" "+e),i=n.start+3;return t.setSelection(i,i+e.length-1),!1}},bold:{title:this.locale.bold.title,text:"bold",group:0,callback:function(t){var e,i,n=t.getSelection(),l=t.getContent();e=0===n.length?t.locale.bold.description:n.text,"**"===l.substr(n.start-2,2)&&"**"===l.substr(n.end,2)?(t.setSelection(n.start-2,n.end+2),t.replaceSelection(e),i=n.start-2):(t.replaceSelection("**"+e+"**"),i=n.start+2),t.setSelection(i,i+e.length)}},italic:{title:this.locale.italic.title,text:"italic",group:0,callback:function(t){var e,i,n=t.getSelection(),l=t.getContent();e=0===n.length?t.locale.italic.description:n.text,"_"===l.substr(n.start-1,1)&&"_"===l.substr(n.end,1)?(t.setSelection(n.start-1,n.end+1),t.replaceSelection(e),i=n.start-1):(t.replaceSelection("_"+e+"_"),i=n.start+1),t.setSelection(i,i+e.length)}},image:{title:this.locale.image.title,text:"image",group:1,callback:function(t){var i,n,l,s=t.getSelection();return i=0===s.length?t.locale.image.description:s.text,l=prompt(t.locale.image.title,"http://"),e.test(l)&&(t.replaceSelection("!["+i+"]("+l+' "'+t.locale.image.description+'")'),n=s.start+2,t.setSelection(n,n+i.length)),!1}},link:{title:this.locale.link.title,text:"link",group:1,callback:function(e){var i,n,l,s=e.getSelection();return i=0===s.length?e.locale.link.description:s.text,l=prompt(e.locale.link.title,"http://"),t.test(l)&&(e.replaceSelection("["+i+"]("+l+")"),n=s.start+1,e.setSelection(n,n+i.length)),!1}},ol:{title:this.locale.ol.title,text:"ol",group:2,callback:function(t){var e,i,n=t.getSelection();if(0===n.length)e=t.locale.ol.description,t.replaceSelection("1. "+e),i=n.start+3;else if(n.text.indexOf("\n")<0)e=n.text,t.replaceSelection("1. "+e),i=n.start+3;else{var l=[];for(var s in e=(l=n.text.split("\n"))[0],l){var r=parseInt(s)+parseInt(1);l[s]=r+". "+l[s]}t.replaceSelection("\n\n"+l.join("\n")),i=n.start+5}t.setSelection(i,i+e.length)}},ul:{title:this.locale.ul.title,text:"ul",group:2,callback:function(t){var e,i,n=t.getSelection();if(0===n.length)e=t.locale.ul.description,t.replaceSelection("- "+e),i=n.start+2;else if(n.text.indexOf("\n")<0)e=n.text,t.replaceSelection("- "+e),i=n.start+2;else{var l=[];for(var s in e=(l=n.text.split("\n"))[0],l)l[s]="- "+l[s];t.replaceSelection("\n\n"+l.join("\n")),i=n.start+4}t.setSelection(i,i+e.length)}},comment:{title:this.locale.comment.title,text:"comment",group:3,callback:function(t){var e,i,n=t.getSelection();t.getContent();if(0===n.length)e=t.locale.comment.description,t.replaceSelection("> "+e),i=n.start+2;else if(n.text.indexOf("\n")<0)e=n.text,t.replaceSelection("> "+e),i=n.start+2;else{var l=[];for(var s in e=(l=n.text.split("\n"))[0],l)l[s]="> "+l[s];t.replaceSelection("\n\n"+l.join("\n")),i=n.start+4}t.setSelection(i,i+e.length)}},code:{title:this.locale.code.title,text:"code",group:3,callback:function(t){var e,i,n=t.getSelection(),l=t.getContent();e=0===n.length?t.locale.code.description:n.text,"```\n"===l.substr(n.start-4,4)&&"\n```"===l.substr(n.end,4)?(t.setSelection(n.start-4,n.end+4),t.replaceSelection(e),i=n.start-4):"`"===l.substr(n.start-1,1)&&"`"===l.substr(n.end,1)?(t.setSelection(n.start-1,n.end+1),t.replaceSelection(e),i=n.start-1):l.indexOf("\n")>-1?(t.replaceSelection("```\n"+e+"\n```"),i=n.start+4):(t.replaceSelection("`"+e+"`"),i=n.start+1),t.setSelection(i,i+e.length)}},preview:{title:this.locale.preview.title,text:"preview",group:4,callback:function(t){var e=document.getElementById("easy-markdown"),i=document.getElementById("easy-preview");document.getElementById("easy-preview-close").removeAttribute("disabled");var n=window.markdownit();i.childNodes[1].innerHTML=n.render(t.element.value),e.classList.add("is-hidden"),i.classList.add("is-visible")}}},"bootstrap"===this.options.framework||"foundation"===this.options.framework?this[this.options.framework]():this.none()}function o(t,e,i){return this.element=e,this.options=t,this.buttons=i,this.build()}function a(t,e){return this.parent=e,this.options=t,this.locale=l({},s.locale,s.locale[t.locale]||{}),this.build()}function c(t,e){this.element=t,this.parent=t.parentNode,this.parent.innerHTML="",this.options=l({},c.defaults,e||{}),"undefined"==typeof markdownit&&(this.options.disabled.preview=!0),this.preview="off";var i=new o(this.options,this.element);t.style.width=this.options.width,this.parent.appendChild(i),function(t,e){for(var i in e)t.style[i]=e[i]}(this.parent,{position:"relative",height:i.clientHeight+"px",overflow:"hidden"}),new a(this.options,this.parent)}return r.prototype={getContent:function(){return this.element.value},findSelection:function(t){var e;if((e=this.getContent().indexOf(t))>=0&&t.length>0){var i,n=this.getSelection();return this.setSelection(e,e+t.length),i=this.getSelection(),this.setSelection(n.start,n.end),i}return null},getSelection:function(){var t=this.element;return("selectionStart"in t&&function(){var e=t.selectionEnd-t.selectionStart;return{start:t.selectionStart,end:t.selectionEnd,length:e,text:t.value.substr(t.selectionStart,e)}}||function(){return null})()},setIcons:function(t,e){if("string"==typeof this.options.icons){var i=document.createTextNode(t.title);e.appendChild(i)}else{var n=document.createElement("I");n.setAttribute("class",this.options.icons[t.text]),e.appendChild(n)}},setListener:function(t){var e=this;return t.addEventListener("click",(function(t){var i=t.target,n="I"===i.nodeName?i.parentNode:i;e.buttons[n.getAttribute("data-md")].callback(e),t.preventDefault()}),!1),t},setSelection:function(t,e){var i=this.element;return("selectionStart"in i&&function(){i.selectionStart=t,i.selectionEnd=e}||function(){return null})()},replaceSelection:function(t){var e=this.element;return("selectionStart"in e&&function(){return e.value=e.value.substr(0,e.selectionStart)+t+e.value.substr(e.selectionEnd,e.value.length),e.selectionStart=e.value.length,this})()}},r.prototype.bootstrap=function(){var t=i({0:{type:"div"},1:{type:"div",attrs:{class:"btn-group",role:"group",style:"margin:5px;"},childrenOf:0},2:{type:"div",attrs:{class:"btn-group",role:"group",style:"margin:5px;"},childrenOf:0},3:{type:"div",attrs:{class:"btn-group",role:"group",style:"margin:5px;"},childrenOf:0},4:{type:"div",attrs:{class:"btn-group",role:"group",style:"margin:5px;"},childrenOf:0},5:{type:"div",attrs:{class:"btn-group",role:"group",style:"margin:5px;"},childrenOf:0}});for(var e in this.buttons){var l=this.buttons[e];if(!0!==this.options.disabled[l.text]){var s=n("BUTTON",{class:this.options.btnClass,"data-md":l.text,title:l.title});this.setIcons(l,s),t.childNodes[l.group].appendChild(s)}}return this.setListener(t),t},r.prototype.foundation=function(){var t=n("UL",{class:"button-group",style:"margin: 0 0 10px 0;"});for(var e in this.buttons){var i=this.buttons[e];if(!0!==this.options.disabled[i.text]){var l=n("LI"),s=n("A",{class:this.options.btnClass,"data-md":i.text,title:i.title});this.setIcons(i,s),t.appendChild(l).appendChild(s)}}return this.setListener(t),t},r.prototype.none=function(){var t=document.createElement("DIV");for(var e in this.buttons){var i=this.buttons[e];if(!0!==this.options.disabled[i.text]){var l=n("BUTTON",{class:this.options.btnClass,"data-md":i.text,title:i.title});this.setIcons(i,l),t.appendChild(l)}}return this.setListener(t),t},o.prototype={build:function(){var t=new r(this.element,this.options,this.buttons),e=i({0:{type:"div",attrs:{class:"easy-markdown",id:"easy-markdown"}},1:{type:"div",attrs:{id:"easy-markdown-buttons"},childrenOf:0},2:{type:"div",attrs:{id:"easy-markdown-textarea"},childrenOf:0}});return e.childNodes[0].appendChild(t),e.childNodes[1].appendChild(this.element),e}},a.prototype={build:function(){var t=i({0:{type:"div",attrs:{class:"easy-preview",id:"easy-preview",style:"height:"+this.parent.clientHeight+"px;"}},1:{type:"div",attrs:{id:"easy-preview-buttons"},childrenOf:0},2:{type:"div",attrs:{id:"easy-preview-html",style:"overflow:auto;"},childrenOf:0},3:{type:"button",attrs:{class:this.options.btnClass,disabled:"disabled",id:"easy-preview-close"},text:this.locale.getback.title,childrenOf:1},4:{type:"HR",childrenOf:1}});this.setListener(t.childNodes[0].childNodes[0]),this.parent.appendChild(t),t.childNodes[1].style.height=this.parent.clientHeight-t.childNodes[0].clientHeight-20+"px"},setListener:function(t){return t.addEventListener("click",(function(t){var e=document.getElementById("easy-markdown"),i=document.getElementById("easy-preview");document.getElementById("easy-preview-close").setAttribute("disabled","disabled"),e.classList.remove("is-hidden"),i.classList.remove("is-visible"),t.preventDefault()}),!1),t}},s.Preview=a,s.Buttons=r,s.Skeleton=o,s.Editor=c,s.locale={bold:{title:"Bold",description:"Strong Text"},italic:{title:"Italic",description:"Emphasized text"},header:{title:"Header",description:"Heading text"},image:{title:"Image",description:"Image description"},link:{title:"Link",description:"Link description"},ol:{title:"Numbered",description:"Numbered list"},ul:{title:"Bullet",description:"Bulleted list"},comment:{title:"Comment",description:"Comment"},code:{title:"Code",description:"code text"},preview:{title:"Preview"},getback:{title:"Get back"}},c.defaults={width:"100%",btnClass:"",framework:"none",locale:"",icons:"",disabled:{bold:!1,italic:!1,header:!1,image:!1,link:!1,ol:!1,ul:!1,comment:!1,code:!1,preview:!1}},s}();