All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.web.lib.editor.xheditor_plugins.html2markdown.js Maven / Gradle / Ivy

There is a newer version: 6.2.04
Show newest version
if(typeof require!="undefined"){var htmlparser=require("./htmldomparser");var HTMLParser=htmlparser.HTMLParser}function HTML2Markdown(J,H){var O=false;var z=[];var i=[];var A=[];var G=[];var I=[];var T=[];H=H||{};var B=H.inlineStyle||false;var S={hr:"- - -\n\n",br:"  \n",title:"# ",h1:"# ",h2:"## ",h3:"### ",h4:"#### ",h5:"##### ",h6:"###### ",b:"**",strong:"**",i:"_",em:"_",dfn:"_","var":"_",cite:"_",span:" ",ul:"* ",ol:"1. ",dl:"- ",blockquote:"> "};function e(){var b="";if(i){for(var a=0;a0){return a.slice(-1)[0]}return""}function L(b){if(!b){return""}for(var a=b.length-1;a>=0;a--){if(b[a]!=""){return b[a]}}return""}function N(a){var b=false;if(a==L(z)){while(F(z)!=a){z.pop()}z.pop();b=true}return b}function R(c){var a=[];while(z.length>0&&F(z)!=c){var b=z.pop();a.unshift(b)}return a.join("")}function M(b){var a=z.pop();if(!a){return}if(!b){var c;if(/\s*\n\n\s*$/.test(a)){a=a.replace(/\s*\n\n\s*$/,"\n\n");c=""}else{if(/\s*\n\s*$/.test(a)){a=a.replace(/\s*\n\s*$/,"\n");c="\n"}else{if(/\s+$/.test(a)){c="\n\n"}else{c="\n\n"}}}z.push(a);z.push(c)}else{z.push(a);if(!a.endsWith("\n")){z.push("\n\n")}}}function P(){if(z.length>0){var a=F(z);if(!a.endsWith("\n")){z.push("\n")}}else{z.push("\n")}}try{var D;if(J){var C=document.createElement("div");C.innerHTML=J;D=C}else{D=window.document.body}HTMLParser(D,{start:function(j,a,d){j=j.toLowerCase();if(O){console.log("start: "+j)}if(d&&(j!="br"&&j!="hr"&&j!="img")){return}switch(j){case"br":z.push(S[j]);break;case"hr":M();z.push(S[j]);break;case"title":case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":M();z.push(S[j]);break;case"b":case"strong":case"i":case"em":case"dfn":case"var":case"cite":z.push(S[j]);break;case"span":if(!/\s+$/.test(F(z))){z.push(S[j])}break;case"p":case"div":case"td":M();break;case"ul":case"ol":case"dl":i.push(S[j]);if(i.length>1){P()}else{M()}break;case"li":case"dt":var k=e();z.push(k);break;case"a":var g=Q(a);A.push(g);z.push("[");break;case"img":var g=Q(a);var c,b,f;g.src?f=getNormalizedUrl(g.src.value):f="";if(!f){break}g.alt?c=g.alt.value.trim():c="";g.title?b=g.title.value.trim():b="";if(!B&&!L(z).startsWith("[")){var h=T.indexOf(f);if(h==-1){T.push(f);h=T.length-1}M();z.push("![");if(c!=""){z.push(c)}else{if(b!=null){z.push(b)}}z.push("]["+h+"]");M()}else{if(!L(z).startsWith("[")){M()}z.push("!["+c+"]("+f+(b?' "'+b+'"':"")+")");if(!L(z).startsWith("[")){M(true)}}break;case"blockquote":M();G.push(S[j]);z.push(G.join(""));break;case"pre":case"code":M();I.push(true);break}},chars:function(a){if(I.length>0){a="    "+a.replace(/\n/g,"\n    ")}else{if(a.trim()!=""){a=a.replace(/\s+/g," ");var b=L(z);if(/\s+$/.test(b)){a=a.replace(/^\s+/g,"")}}else{z.push("");return}}if(O){console.log("text: "+a)}z.push(a)},end:function(b){b=b.toLowerCase();if(O){console.log("end: "+b)}switch(b){case"title":case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":if(!N(S[b])){M(true)}break;case"p":case"div":case"td":while(z.length>0&&F(z).trim()==""){z.pop()}M(true);break;case"b":case"strong":case"i":case"em":case"dfn":case"var":case"cite":if(!N(S[b])){z.push(R(S[b]).trim());z.push(S[b])}break;case"a":var d=R("[");d=d.replace(/\s+/g," ");d=d.trim();if(d==""){z.pop();break}var h=A.pop();var a;h.href&&h.href.value!=""?a=getNormalizedUrl(h.href.value):a="";if(a==""){z.pop();z.push(d);break}z.push(d);if(!B&&!F(z).startsWith("!")){var g=T.indexOf(a);if(g==-1){T.push(a);g=T.length-1}z.push("]["+g+"]")}else{if(F(z).startsWith("!")){var d=z.pop();d=z.pop()+d;M();z.push(d)}var f=h.title;z.push("]("+a+(f?' "'+f.value.trim().replace(/\s+/g," ")+'"':"")+")");if(F(z).startsWith("!")){M(true)}}break;case"ul":case"ol":case"dl":P();i.pop();break;case"li":case"dt":var c=e();if(!N(c)){var d=R(c).trim();if(d.startsWith("[![")){z.pop();M();z.push(d);M(true)}else{z.push(d);P()}}break;case"blockquote":G.pop();break;case"pre":case"code":M(true);I.pop();break;case"span":if(F(z).trim()==""){z.pop();if(F(z)==" "){z.pop()}else{z.push(S[b])}}else{var d=z.pop();z.push(d.trim());z.push(S[b])}break;case"br":case"hr":case"img":case"table":case"tr":break}}},{nodesToIgnore:["script","noscript","object","iframe","frame","head","style","label"]});if(!B){for(var E=0;E




© 2015 - 2024 Weber Informatics LLC | Privacy Policy