lys-markdown-parser
Advanced tools
Comparing version 3.6.1 to 3.7.0
@@ -1,1 +0,1 @@ | ||
"use strict";function _objectWithoutProperties(a,b){if(null==a)return{};var c,f,h=_objectWithoutPropertiesLoose(a,b);if(Object.getOwnPropertySymbols){var g=Object.getOwnPropertySymbols(a);for(f=0;f<g.length;f++)c=g[f],!(0<=b.indexOf(c))&&Object.prototype.propertyIsEnumerable.call(a,c)&&(h[c]=a[c])}return h}function _objectWithoutPropertiesLoose(a,b){if(null==a)return{};var c,f,h={},g=Object.keys(a);for(f=0;f<g.length;f++)c=g[f],0<=b.indexOf(c)||(h[c]=a[c]);return h}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,e=0;e<b.length;e++)c=b[e],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function _iterableToArrayLimit(a,b){var c,e=[],f=!0,h=!1;try{for(var i,j=a[Symbol.iterator]();!(f=(i=j.next()).done)&&(e.push(i.value),!(b&&e.length===b));f=!0);}catch(a){h=!0,c=a}finally{try{f||null==j["return"]||j["return"]()}finally{if(h)throw c}}return e}function _arrayWithHoles(a){if(Array.isArray(a))return a}function _objectSpread(a){for(var b=1;b<arguments.length;b++){var f=null==arguments[b]?{}:arguments[b],d=Object.keys(f);"function"==typeof Object.getOwnPropertySymbols&&(d=d.concat(Object.getOwnPropertySymbols(f).filter(function(a){return Object.getOwnPropertyDescriptor(f,a).enumerable}))),d.forEach(function(b){_defineProperty(a,b,f[b])})}return a}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(a){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a))return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a)){for(var b=0,d=Array(a.length);b<a.length;b++)d[b]=a[b];return d}}var fuckMD=function(q){"use strict";function r(c,d){if(!c)return{type:"add",prevNode:c,nextNode:d};if(!d)return{type:"del",prevNode:c,nextNode:d};if(c.type!==d.type)return{type:"replace",prevNode:c,nextNode:d};var e={type:"update",prevNode:c,nextNode:d,propsChange:[],children:[]},f=t(c,d);if(f.length){var a;(a=e.propsChange).push.apply(a,_toConsumableArray(f))}return(e.children=s(c,d).filter(function(a){return a}),0<e.propsChange.length+e.children.length)?e:(d.__htmlNode=c.__htmlNode,d.__update=c.__update,e)}function s(c,a){var b=c.children,e=void 0===b?[]:b,f=a.children,g=void 0===f?[]:f,h=[],l=[],i=e.filter(function(a){return!!g.some(function(b){return b.type===a.type})||(h.push(r(a,null)),!1)});g.forEach(function(c){i.some(function(d,a){return d.type===c.type&&(i[a]={isDel:!0,ele:d},!0)})}),i.filter(function(a){return!a.isDel}).forEach(function(b){return h.push(r(b,null))});var j=i.filter(function(a){return a.isDel}).map(function(a){return a.ele});return g.forEach(function(e,f){var c=j.some(function(a,b){if(a.type===e.type){j.splice(b,1),j.splice(f>b?f-1:f,0,{used:!0,ele:a}),b!==f&&l.push({type:"move",prevNode:a,nextNode:e,current:b,moveTo:f>b?f+1:f});var c=r(a,e);return l.push(c),!0}return!1});if(!c){j.splice(f,0,{add:!0,item:e});var a=r(null,e);l.push(_objectSpread({},a,{moveTo:f}))}}),h.concat(l,[],[])}function t(){var h=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},f=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},d=Object.keys(h),a=Object.keys(f),b=[];return d.forEach(function(c){return["__htmlNode","__parent","__update","children","type"].includes(c)?void 0:a.includes(c)?void(h[c]!==f[c]&&b.push({type:"change",key:c,prevNode:h,nextNode:f})):void b.push({type:"del",key:c,prevNode:h,nextNode:f})}),a.forEach(function(c){d.includes(c)||b.push({type:"add",key:c,prevNode:h,nextNode:f})}),b}function a(){for(var a,f=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",d=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"[",g=2<arguments.length&&void 0!==arguments[2]?arguments[2]:"]",c=0,h=-1,i=0,j=function(a){return f.slice(c,c+a.length)===a};c<f.length;){if(a=f[c],!i)if(!a.trim()){c+=1;continue}else if(j(d)){h=c,i+=1,c+=d.length;continue}else return[void 0,f];if(j(g)?(i-=1,c+=g.length):j(d)?(i+=1,c+=d.length):c+=1,!i)return[f.slice(h+d.length,c-g.length),f.slice(c)]}return[void 0,f]}function c(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"",c=a.indexOf(b);return[a.slice(0,c),a.slice(c+b.length)]}function v(){function n(a,b){var c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{},d=c.isPush,e=p;p=a,a.__parent=p,b&&b(),p=e,(void 0===d||d)&&p.children.push(a)}function b(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";i=i.slice(a.length)}function j(a){return[P.head,P.ul,P.code,P.queto].some(function(b){return b.test(a)})}function d(){function a(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:0,c=a.split("\n")[b].trim();return /^\|.+\|$/.test(c)?c.split("|").slice(1,-1):[]}var b=a(i,0),d=a(i,1),c=b.length;if(0!=c&&b.length==d.length&&d.every(function(a){return /^-+$/.test(a.replace(/\s/g,""))}))return n({type:"table",children:[]},function(){i=i.replace(/(.+)\n?/,function(b,c){return n({type:"thead",children:[]},function(){n({type:"tr",children:[]},function(){c.trim().split("|").slice(1,-1).map(function(b){n({type:"th",children:[]},function(){e(b)})})})}),""}),i=i.replace(/.+\n?/,""),n({type:"tbody",children:[]},function(){for(var a,b=function f(){var f=(i.match(/^.+\n?/)||[])[0];return f?j(f)?"break":void(i=i.replace(f,""),n({type:"tr",children:[]},function(){var d,g=f.trim().split("|");g=g[0]?g.slice(0,c):g.slice(1).slice(0,c),(d=g).push.apply(d,_toConsumableArray(Array(c-g.length).fill(""))),g.forEach(function(b){n({type:"td",children:[]},function(){e(b)})})})):"break"};a=b(),"break"!==a;);})}),!0}function e(){var c=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";if(c){if(P.url.test(c))return void e(c.replace(P.url,function(b,c,d){return n({type:"a",href:d,value:c,children:[]},function(){e(c)}),""}));if(P.blod.test(c))return void e(c.replace(P.blod,function(b,c){return n({type:"b",children:[]},function(){e(c)}),""}));if(P.italic.test(c))return void e(c.replace(P.italic,function(b,c){return n({type:"i",children:[]},function(){e(c)}),""}));if(P.inlineCode.test(c))return void e(c.replace(P.inlineCode,function(b,c){return c&&n({type:"inlineCode",children:[]},function(){e(c)}),""}));if(P.video.test(c))return void e(c.replace(P.video,function(a,b,c){return p.children.push({type:"video",src:c,alt:b}),""}));if(P.audio.test(c))return c=c.replace(P.audio,function(a,b,c){return p.children.push({type:"audio",src:c,alt:b}),""}),void e(c);if(P.img.test(c))return void e(c.replace(P.img,function(a,b,c){return p.children.push({type:"img",src:c,alt:b}),""}));if("\n"==c[0])return p.children.push({type:"br"}),void e(c.slice(1));var d=p.children[p.children.length-1];d&&"text"===d.type?d.value+=c[0]:p.children.push({type:"text",value:g(c[0])}),e(c.slice(1))}}function g(a){return a.replace(/>/g,">").replace(/\\#/g,"#").replace(/\\`/g,"`").replace(/\\-/g,"-").replace(/\\\*/g,"*")}function h(){function i(){var b=Math.floor,f=0<arguments.length&&void 0!==arguments[0]?arguments[0]:-1;if(l){var g=(l.match(/.+\n?/)||[])[0],j=g.match(/\s*/)[0],c=b(j.length/5);if(/^[-+]\s+/.test(g.trim())){var e="+"==g.match(/\s*[-+]/)[0].trim();if(c==f+1){var d={type:"ul",listStyleType:e?"decimal":a[c%a.length],children:[]};return n(d,function(){i(f+1)}),void i(f)}if(c==f)return n({type:"li",children:[]},function(){f(g.replace(/\s*[-+]\s*/,""))}),l=l.slice(g.length),void i(f);if(c<f)return}var k=p.children[p.children.length-1];n(k,function(){f(g)},{isPush:!1}),l=l.slice(g.length),i(f)}}var l=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";l="".concat(l,"\n");var a=["disc","circle","square"];i()}function f(){if(!/^\n{1,2}$/.test(i)&&i){if(/Reg.br/.test(i)){var g=i.match(P.br),l=_slicedToArray(g,1),m=l[0];return p.children.push({type:"br"}),b(m),void f()}if(P.head.test(i)){var o=i.match(P.head)||[],q=_slicedToArray(o,3),r=q[0],j=q[1],D=q[2],E={type:"h".concat(j.length),children:[]};return n(E,function(){e(D)}),b(r),void f()}if(P.queto.test(i)){var Q=i.match(P.queto),k=_slicedToArray(Q,2),R=k[0],w=k[1],S=a(w,"[","]"),T=_slicedToArray(S,2),s=T[0],t=T[1],u={type:"queto",tag:s,children:[]},x=v(t.replace(/^\s*\n/,""));return u.children=x.children,p.children.push(u),p.children.push({type:"br"}),b(R),void f()}if(P.code.test(i)){var y=i.match(P.code),U=_slicedToArray(y,2),z=U[0],A=U[1],B=c(A,"\n").map(function(a){return a.trim()}),C=_slicedToArray(B,2),V=C[0],X=C[1];return p.children.push({type:"code",language:V,value:X}),b(z),void f()}if(P.ul.test(i)){var F=i.match(P.ul),G=_slicedToArray(F,2),H=G[0],I=G[1];return n(p,function(){h(I)},{isPush:!1}),p.children.push({type:"br"}),b(H),void f()}if(i.match(/.+\n/)&&/\|.+\|/.test(i.match(/.+\n/)[0].trim())&&d(i))return void f();if(P.hr.test(i)){var J=i.match(P.hr)||[],K=_slicedToArray(J,1),L=K[0];return void 0!==L&&p.children.push({type:"hr",children:[]}),b(L),void f()}if(P.text.test(i)){var M=i.match(P.text)||[""],N=_slicedToArray(M,1),O=N[0];return e(O),b(O),void f()}throw new Error("cannot handle str:".concat(i))}}var i=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";i+="\n\n";var p={children:[],type:"root"};return f(),p}function u(a,b){return b&&b.parentElement&&b.parentElement.insertBefore(a,b),a}function g(f){var h=1<arguments.length&&void 0!==arguments[1]?arguments[1]:document.body;if(f){var i=f.nextNode;switch(f.type){case"del":{var a=f.prevNode.__htmlNode;a.parentElement||console.log("delete error::",f),a.parentElement.removeChild(a);break}case"add":{w(i,h,{beforeAppend:function(b){var c=h.childNodes[f.moveTo];if(c)return u(b,c),!0}});break}case"replace":{var j=f.prevNode.__htmlNode,k=document.createDocumentFragment();w(i,k),j.parentElement.replaceChild(k,j);break}case"move":{var c=f.moveTo,b=f.prevNode,d=b.__htmlNode.parentElement;d.childNodes[c]!==b.__htmlNode&&(d.childNodes[c]?u(b.__htmlNode,d.childNodes[c]):d.appendChild(b.__htmlNode));break}case"update":{var l=f.propsChange,e=f.children,m=f.prevNode,n=f.nextNode,o=m.__htmlNode;n.__htmlNode=o,m.__update&&(n.__update=m.__update),l.forEach(function(d){var a=d.key;switch(d.type){case"change":case"add":{var b=n[a];if(m.__update){m.__update(a,n);break}if(o instanceof Text){o.data=b;break}o.setAttribute(a,b);break}case"del":{o.removeAttribute(a);break}}}),e.forEach(function(b){return g(b,o)});break}default:console.error("canot handle type",f,f.type);}}}function b(d){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"";return"text"===d.type&&(e+=d.value||""),d.children&&d.children.forEach(function(c){e+=b(c)}),e}function w(f,a){var h,i=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};switch(f.type){case"audio":case"video":{if(/^<iframe(\s*.+)*><\/iframe>$/.test(f.src.trim())){h=document.createElement("div"),h.className="audio",h.innerHTML=f.src.replace("http://","//");var e=h.querySelector("iframe");e.style.cssText+=";max-width: 100%; max-height: 60vw; overflow: hidden;"}else h=document.createElement(f.type),h.src=f.src,h.alt=f.alt,h.controls="true";break}case"img":{var j=f.src.match(/\.(\d+)x(\d+)\./);if(j){var k=j.slice(1,3),n=_slicedToArray(k,2),d=n[0],o=n[1],g=f.src;h=document.createElement("div"),h.style.cssText=";position: relative; max-width: ".concat(d,"px; overflow: hidden; background: rgb(219, 221, 215);"),h.innerHTML="<div style=\"padding-top: ".concat(100*(o/d),"%;\">\n <img ").concat(LY.lazyLoad.caches.includes(g)?"src=\"".concat(g,"\" data-img-cache=\"true\""):"","\n class=\"lazy-load-img img-loading\"\n data-lazy-img=\"").concat(f.src,"\"\n style=\"position: absolute; width: 100%; height: 100%; top: 0;\" />\n </div>");break}else{h=document.createElement(f.type),h.src=f.src,h.alt=f.alt;break}}case"text":{var p=f.value;h=document.createTextNode(p);break}case"br":{h=document.createElement(f.type);break}case"a":{h=document.createElement(f.type),h.href=f.href,h.target="_blank";break}case"code":{h=document.createElement("pre");var q=document.createElement("code");f.__update=function(a,b){switch(a){case"language":{q.className=["highlight",b[a]||""].join(" ");break}case"value":{q.textContent=b[a];break}default:}},f.__update("language",f),f.__update("value",f),h.appendChild(q);break}case"inlineCode":{h=document.createElement("code"),h.className="inlineCode";break}case"h1":{h=document.createElement(f.type);var s=document.createElement("a"),r=b(f);s.href="#".concat(r),s.id=r,h.appendChild(s);break}case"ul":{h=document.createElement(f.type),f.__update=function(b,c){h.style.cssText+=";list-style-type:".concat(c[b],";")},f.__update("listStyleType",f);break}default:h=document.createElement(f.type),f.indent&&(h.style.cssText+=";padding-left: 2em;"),"table"==f.type&&h.setAttribute("border","1");}f.tag&&h.setAttribute("tag",f.tag),f.children&&f.children.forEach(function(a){return w(a,h)});var l=i.beforeAppend&&i.beforeAppend(h);return l||a.appendChild(h),f.__htmlNode=h,h}function h(a){function c(a){"text"==a.type&&(d+=a.value||""),"img"==a.type&&f.push(a.src),a.children&&a.children.forEach(c)}var d="",f=[];return c(a),{text:d,imgs:f}}function i(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",b=e[a];if(!b){var c=v(a);b=_objectSpread({root:c},h(c)),e[a]=b}return b}function d(a){return new Promise(function(b){if(l[a])return b();var c=function(){l[a]=!0,b()};if(a.endsWith(".js")){var d=document.createElement("script");d.onload=c,d.src=a,document.head.appendChild(d)}else if(a.endsWith(".css")){var e=document.createElement("link");e.onload=c,e.type="text/css",e.rel="stylesheet",e.charset="utf-8",e.href=a,document.head.appendChild(e)}})}function j(a,c){Promise.all(c.asset.map(d)).then(function(){window.hljs&&a&&(window.hljs.configure({tabReplace:4}),_toConsumableArray(a.querySelectorAll("code.highlight")).forEach(function(a){window.hljs.highlightBlock(a)}))})}function k(a){return _objectSpread({asset:[]},a)}var P={get hr(){return /(^-{3,}[^\n]+\n?)/},get code(){return /^`{3}(((?!```)[\s\S])*)`{3}/},get queto(){return /^>(((?!\n\n)[\s\S])*)\n\n/},get head(){return /^\s*(#{1,6})([^\n]*)\n?/},get ul(){return /^([-+]\s+((?!\n\n)[\s\S])*)\n\n/},get url(){return /^\[([^\]]+)\]\(([^)]+)\)/},get inlineCode(){return /^`([^`]*)`/},get br(){return /^\n/},get text(){return /^[^\n]*\n?/},get blod(){return /^\*{3}(((?!\*{3}).)*)\*{3}/},get italic(){return /^\*{2}(((?!\*{2}).)*)\*{2}/},get video(){return /^!{3}\[([^\]]*)\]\(([^)]+)\)/},get audio(){return /^!{2}\[([^\]]*)\]\(([^)]+)\)/},get img(){return /^!\[([^\]]*)\]\(([^)]+)\)/}},e={},l={},f=function(){function a(d,b){_classCallCheck(this,a),this.dom=d,this.config=b,this.prevRoot=null}return _createClass(a,[{key:"update",value:function(a){this.dom.classList.add("markdown");var d=i(a),b=r(this.prevRoot,d.root);this.prevRoot=d.root,g(b,this.dom);var e=k(this.config);e.useHighlight&&j(this.dom,e)}}]),a}();return q.Markdown=f,q.parser=v,q.trans=w,q.codeHighlight=j,q.getParseResult=i,q.markdown=function(c,b,a){c.innerHTML="",c.classList.add("markdown");var e=i(b);w(e.root,c),a=k(a),j(c,a)},q.markdownInfo=function(a){var e=i(a),b=e.root,c=_objectWithoutProperties(e,["root"]);return c},q}({}); | ||
"use strict";function _objectWithoutProperties(d,a){if(null==d)return{};var b,e,i=_objectWithoutPropertiesLoose(d,a);if(Object.getOwnPropertySymbols){var h=Object.getOwnPropertySymbols(d);for(e=0;e<h.length;e++)b=h[e],!(0<=a.indexOf(b))&&Object.prototype.propertyIsEnumerable.call(d,b)&&(i[b]=d[b])}return i}function _objectWithoutPropertiesLoose(d,a){if(null==d)return{};var b,e,i={},h=Object.keys(d);for(e=0;e<h.length;e++)b=h[e],0<=a.indexOf(b)||(i[b]=d[b]);return i}function _classCallCheck(c,a){if(!(c instanceof a))throw new TypeError("Cannot call a class as a function")}function _defineProperties(d,a){for(var b,f=0;f<a.length;f++)b=a[f],b.enumerable=b.enumerable||!1,b.configurable=!0,"value"in b&&(b.writable=!0),Object.defineProperty(d,b.key,b)}function _createClass(d,a,b){return a&&_defineProperties(d.prototype,a),b&&_defineProperties(d,b),d}function _slicedToArray(c,a){return _arrayWithHoles(c)||_iterableToArrayLimit(c,a)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function _iterableToArrayLimit(d,a){var b,g=[],e=!0,k=!1;try{for(var l,m=d[Symbol.iterator]();!(e=(l=m.next()).done)&&(g.push(l.value),!(a&&g.length===a));e=!0);}catch(c){k=!0,b=c}finally{try{e||null==m["return"]||m["return"]()}finally{if(k)throw b}}return g}function _arrayWithHoles(b){if(Array.isArray(b))return b}function _objectSpread(c){for(var a=1;a<arguments.length;a++){var e=null==arguments[a]?{}:arguments[a],f=Object.keys(e);"function"==typeof Object.getOwnPropertySymbols&&(f=f.concat(Object.getOwnPropertySymbols(e).filter(function(b){return Object.getOwnPropertyDescriptor(e,b).enumerable}))),f.forEach(function(a){_defineProperty(c,a,e[a])})}return c}function _defineProperty(d,a,b){return a in d?Object.defineProperty(d,a,{value:b,enumerable:!0,configurable:!0,writable:!0}):d[a]=b,d}function _toConsumableArray(b){return _arrayWithoutHoles(b)||_iterableToArray(b)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(b){if(Symbol.iterator in Object(b)||"[object Arguments]"===Object.prototype.toString.call(b))return Array.from(b)}function _arrayWithoutHoles(c){if(Array.isArray(c)){for(var a=0,e=Array(c.length);a<c.length;a++)e[a]=c[a];return e}}var fuckMD=function(m){"use strict";function n(b,c){if(!b)return{type:"add",prevNode:b,nextNode:c};if(!c)return{type:"del",prevNode:b,nextNode:c};if(b.type!==c.type)return{type:"replace",prevNode:b,nextNode:c};var d={type:"update",prevNode:b,nextNode:c,propsChange:[],children:[]},e=p(b,c);if(e.length){var f;(f=d.propsChange).push.apply(f,_toConsumableArray(e))}return(d.children=o(b,c).filter(function(b){return b}),0<d.propsChange.length+d.children.length)?d:(c.__htmlNode=b.__htmlNode,c.__update=b.__update,d)}function o(d,c){var a=d.children,b=void 0===a?[]:a,e=c.children,f=void 0===e?[]:e,g=[],h=[],k=b.filter(function(c){return!!f.some(function(a){return a.type===c.type})||(g.push(n(c,null)),!1)});f.forEach(function(b){k.some(function(c,d){return c.type===b.type&&(k[d]={isDel:!0,ele:c},!0)})}),k.filter(function(b){return!b.isDel}).forEach(function(a){return g.push(n(a,null))});var i=k.filter(function(b){return b.isDel}).map(function(b){return b.ele});return f.forEach(function(d,e){var b=i.some(function(f,a){if(f.type===d.type){i.splice(a,1),i.splice(e>a?e-1:e,0,{used:!0,ele:f}),a!==e&&h.push({type:"move",prevNode:f,nextNode:d,current:a,moveTo:e>a?e+1:e});var b=n(f,d);return h.push(b),!0}return!1});if(!b){i.splice(e,0,{add:!0,item:d});var c=n(null,d);h.push(_objectSpread({},c,{moveTo:e}))}}),g.concat(h,[],[])}function p(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},g=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},f=Object.keys(e),d=Object.keys(g),a=[];return f.forEach(function(b){return["__htmlNode","__parent","__update","children","type"].includes(b)?void 0:d.includes(b)?void(e[b]!==g[b]&&a.push({type:"change",key:b,prevNode:e,nextNode:g})):void a.push({type:"del",key:b,prevNode:e,nextNode:g})}),d.forEach(function(b){f.includes(b)||a.push({type:"add",key:b,prevNode:e,nextNode:g})}),a}function q(){for(var b,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",f=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"[",d=2<arguments.length&&void 0!==arguments[2]?arguments[2]:"]",g=0,k=-1,l=0,m=function(b){return e.slice(g,g+b.length)===b};g<e.length;){if(b=e[g],!l)if(!b.trim()){g+=1;continue}else if(m(f)){k=g,l+=1,g+=f.length;continue}else return[void 0,e];if(m(d)?(l-=1,g+=d.length):m(f)?(l+=1,g+=f.length):g+=1,!l)return[e.slice(k+f.length,g-d.length),e.slice(g)]}return[void 0,e]}function a(){var d=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",a=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"",b=d.indexOf(a);return[d.slice(0,b),d.slice(b+a.length)]}function r(){function m(f,a){var b=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{},c=b.isPush,d=o;o=f,f.__parent=o,a&&a(),o=d,(void 0===c||c)&&o.children.push(f)}function c(){var b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";P=P.slice(b.length)}function b(c){return[v.head,v.ul,v.code,v.queto].some(function(a){return a.test(c)})}function n(){function e(){var d=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",a=1<arguments.length&&void 0!==arguments[1]?arguments[1]:0,b=d.split("\n")[a].trim();return /^\|.+\|$/.test(b)?b.split("|").slice(1,-1):[]}var a=e(P,0),f=e(P,1),h=a.length;if(0!=h&&a.length==f.length&&f.every(function(b){return /^-+$/.test(b.replace(/\s/g,""))}))return m({type:"table",children:[]},function(){P=P.replace(/(.+)\n?/,function(a,b){return m({type:"thead",children:[]},function(){m({type:"tr",children:[]},function(){b.trim().split("|").slice(1,-1).map(function(a){m({type:"th",children:[]},function(){j(a)})})})}),""}),P=P.replace(/.+\n?/,""),m({type:"tbody",children:[]},function(){for(var c,d=function a(){var a=(P.match(/^.+\n?/)||[])[0];return a?b(a)?"break":void(P=P.replace(a,""),m({type:"tr",children:[]},function(){var b,c=a.trim().split("|");c=c[0]?c.slice(0,h):c.slice(1).slice(0,h),(b=c).push.apply(b,_toConsumableArray(Array(h-c.length).fill(""))),c.forEach(function(a){m({type:"td",children:[]},function(){j(a)})})})):"break"};c=d(),"break"!==c;);})}),!0}function j(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";if(a){if(v.url.test(a))return void j(a.replace(v.url,function(a,b,c){return m({type:"a",href:c,value:b,children:[]},function(){j(b)}),""}));if(v.blod.test(a))return void j(a.replace(v.blod,function(a,b){return m({type:"b",children:[]},function(){j(b)}),""}));if(v.italic.test(a))return void j(a.replace(v.italic,function(a,b){return m({type:"i",children:[]},function(){j(b)}),""}));if(v.inlineCode.test(a))return void j(a.replace(v.inlineCode,function(a,b){return b&&m({type:"inlineCode",children:[]},function(){j(b)}),""}));if(v.video.test(a))return void j(a.replace(v.video,function(d,a,b){return o.children.push({type:"video",src:b,alt:a}),""}));if(v.audio.test(a))return a=a.replace(v.audio,function(d,a,b){return o.children.push({type:"audio",src:b,alt:a}),""}),void j(a);if(v.img.test(a))return void j(a.replace(v.img,function(d,a,b){return o.children.push({type:"img",src:b,alt:a}),""}));if("\n"==a[0])return o.children.push({type:"br"}),void j(a.slice(1));var b=o.children[o.children.length-1];b&&"text"===b.type?b.value+=a[0]:o.children.push({type:"text",value:e(a[0])}),j(a.slice(1))}}function e(b){return b.replace(/>/g,">").replace(/\\#/g,"#").replace(/\\`/g,"`").replace(/\\-/g,"-").replace(/\\\*/g,"*")}function d(){function h(){var a=Math.floor,l=0<arguments.length&&void 0!==arguments[0]?arguments[0]:-1;if(i){var f=(i.match(/.+\n?/)||[])[0],g=f.match(/\s*/)[0],j=a(g.length/5);if(/^[-+]\s+/.test(f.trim())){var c="+"==f.match(/\s*[-+]/)[0].trim();if(j==l+1){var e={type:"ul",listStyleType:c?"decimal":b[j%b.length],children:[]};return m(e,function(){h(l+1)}),void h(l)}if(j==l)return m({type:"li",children:[]},function(){l(f.replace(/\s*[-+]\s*/,""))}),i=i.slice(f.length),void h(l);if(j<l)return}var d=o.children[o.children.length-1];m(d,function(){l(f)},{isPush:!1}),i=i.slice(f.length),h(l)}}var i=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";i="".concat(i,"\n");var b=["disc","circle","square"];h()}function h(){if(!/^\n{1,2}$/.test(P)&&P){if(/Reg.br/.test(P)){var b=P.match(v.br),e=_slicedToArray(b,1),f=e[0];return o.children.push({type:"br"}),c(f),void h()}if(v.head.test(P)){var g=P.match(v.head)||[],i=_slicedToArray(g,3),l=i[0],p=i[1],W=i[2],D={type:"h".concat(p.length),children:[]};return m(D,function(){j(W)}),c(l),void h()}if(v.queto.test(P)){var E=P.match(v.queto),Q=_slicedToArray(E,2),k=Q[0],R=Q[1],w=q(R,"[","]"),S=_slicedToArray(w,2),T=S[0],s=S[1],t={type:"queto",tag:T,children:[]},u=r(s.replace(/^\s*\n/,""));return t.children=u.children,o.children.push(t),o.children.push({type:"br"}),c(k),void h()}if(v.code.test(P)){var x=P.match(v.code),y=_slicedToArray(x,2),U=y[0],z=y[1],A=a(z,"\n").map(function(b){return b.trim()}),B=_slicedToArray(A,2),C=B[0],V=B[1];return o.children.push({type:"code",language:C,value:V}),c(U),void h()}if(v.ul.test(P)){var X=P.match(v.ul),F=_slicedToArray(X,2),G=F[0],H=F[1];return m(o,function(){d(H)},{isPush:!1}),o.children.push({type:"br"}),c(G),void h()}if(P.match(/.+\n/)&&/\|.+\|/.test(P.match(/.+\n/)[0].trim())&&n(P))return void h();if(v.hr.test(P)){var I=P.match(v.hr)||[],J=_slicedToArray(I,1),K=J[0];return void 0!==K&&o.children.push({type:"hr",children:[]}),c(K),void h()}if(v.text.test(P)){var L=P.match(v.text)||[""],M=_slicedToArray(L,1),N=M[0];return j(N),c(N),void h()}throw new Error("cannot handle str:".concat(P))}}var P=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";P+="\n\n";var o={children:[],type:"root"};return h(),o}function s(c,a){return a&&a.parentElement&&a.parentElement.insertBefore(c,a),c}function c(g){var f=1<arguments.length&&void 0!==arguments[1]?arguments[1]:document.body;if(g){var h=g.nextNode;switch(g.type){case"del":{var i=g.prevNode.__htmlNode;i.parentElement||console.log("delete error::",g),i.parentElement.removeChild(i);break}case"add":{t(h,f,{beforeAppend:function(a){var b=f.childNodes[g.moveTo];if(b)return s(a,b),!0}});break}case"replace":{var a=g.prevNode.__htmlNode,j=document.createDocumentFragment();t(h,j),a.parentElement.replaceChild(j,a);break}case"move":{var k=g.moveTo,p=g.prevNode,b=p.__htmlNode.parentElement;b.childNodes[k]!==p.__htmlNode&&(b.childNodes[k]?s(p.__htmlNode,b.childNodes[k]):b.appendChild(p.__htmlNode));break}case"update":{var d=g.propsChange,l=g.children,e=g.prevNode,m=g.nextNode,n=e.__htmlNode;m.__htmlNode=n,e.__update&&(m.__update=e.__update),d.forEach(function(c){var d=c.key;switch(c.type){case"change":case"add":{var a=m[d];if(e.__update){e.__update(d,m);break}if(n instanceof Text){n.data=a;break}n.setAttribute(d,a);break}case"del":{n.removeAttribute(d);break}}}),l.forEach(function(a){return c(a,n)});break}default:console.error("canot handle type",g,g.type);}}}function g(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"";return"text"===a.type&&(b+=a.value||""),a.children&&a.children.forEach(function(a){b+=g(a)}),b}function t(b,c){var f,a=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};switch(b.type){case"audio":case"video":{if(/^<iframe(\s*.+)*><\/iframe>$/.test(b.src.trim())){f=document.createElement("div"),f.className="audio",f.innerHTML=b.src.replace("http://","//");var i=f.querySelector("iframe");i.style.cssText+=";max-width: 100%; max-height: 60vw; overflow: hidden;"}else f=document.createElement(b.type),f.src=b.src,f.alt=b.alt,f.controls="true";break}case"img":{var e=b.src.match(/\.(\d+)x(\d+)\./);if(e){var j=e.slice(1,3),k=_slicedToArray(j,2),m=k[0],d=k[1],n=b.src;f=document.createElement("div"),f.style.cssText=";position: relative; max-width: ".concat(m,"px; overflow: hidden; background: rgb(219, 221, 215);"),f.innerHTML="<div style=\"padding-top: ".concat(100*(d/m),"%;\">\n <img ").concat(LY.lazyLoad.caches.includes(n)?"src=\"".concat(n,"\" data-img-cache=\"true\""):"","\n class=\"lazy-load-img img-loading\"\n data-lazy-img=\"").concat(b.src,"\"\n style=\"position: absolute; width: 100%; height: 100%; top: 0;\" />\n </div>");break}else{f=document.createElement(b.type),f.src=b.src,f.alt=b.alt;break}}case"text":{var o=b.value;f=document.createTextNode(o);break}case"br":{f=document.createElement(b.type);break}case"a":{f=document.createElement(b.type),f.href=b.href,f.target="_blank";break}case"code":{f=document.createElement("pre");var p=document.createElement("code");b.__update=function(c,a){switch(c){case"language":{p.className=["highlight",a[c]||""].join(" ");break}case"value":{p.textContent=a[c];break}default:}},b.__update("language",b),b.__update("value",b),f.appendChild(p);break}case"inlineCode":{f=document.createElement("code"),f.className="inlineCode";break}case"h1":{f=document.createElement(b.type);var q=document.createElement("a"),s=g(b);q.href="#".concat(s),q.id=s,f.appendChild(q);break}case"ul":{f=document.createElement(b.type),b.__update=function(a,b){f.style.cssText+=";list-style-type:".concat(b[a],";")},b.__update("listStyleType",b);break}default:f=document.createElement(b.type),b.indent&&(f.style.cssText+=";padding-left: 2em;"),"table"==b.type&&f.setAttribute("border","1");}b.tag&&f.setAttribute("tag",b.tag),b.children&&b.children.forEach(function(b){return t(b,f)});var r=a.beforeAppend&&a.beforeAppend(f);return r||c.appendChild(f),b.__htmlNode=f,f}function u(b){function e(b){"text"==b.type&&(c+=b.value||""),"img"==b.type&&g.push(b.src),b.children&&b.children.forEach(e)}var c="",g=[];return e(b),{text:c,imgs:g}}function h(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",a=d[e];if(!a){var f=r(e);a=_objectSpread({root:f},u(f)),d[e]=a}return a}function b(f){return new Promise(function(a){if(k[f])return a();var b=function(){k[f]=!0,a()};if(f.endsWith(".js")){var c=document.createElement("script");c.onload=b,c.src=f,document.head.appendChild(c)}else if(f.endsWith(".css")){var d=document.createElement("link");d.onload=b,d.type="text/css",d.rel="stylesheet",d.charset="utf-8",d.href=f,document.head.appendChild(d)}})}function i(d,a){Promise.all(a.asset.map(b)).then(function(){window.hljs&&d&&(window.hljs.configure({tabReplace:4}),_toConsumableArray(d.querySelectorAll("code.highlight")).forEach(function(b){window.hljs.highlightBlock(b)}))})}function j(b){return _objectSpread({asset:[]},b)}var v={get hr(){return /(^-{3,}[^\n]+\n?)/},get code(){return /^`{3}(((?!```)[\s\S])*)`{3}/},get queto(){return /^>(((?!\n\n)[\s\S])*)\n\n/},get head(){return /^\s*(#{1,6})([^\n]*)\n?/},get ul(){return /^([-+]\s+((?!\n\n)[\s\S])*)\n\n/},get url(){return /^\[([^\]]+)\]\(([^)]+)\)/},get inlineCode(){return /^`([^`]*)`/},get br(){return /^\n/},get text(){return /^[^\n]*\n?/},get blod(){return /^\*{3}(((?!\*{3}).)*)\*{3}/},get italic(){return /^\*{2}(((?!\*{2}).)*)\*{2}/},get video(){return /^!{3}\[([^\]]*)\]\(([^)]+)\)/},get audio(){return /^!{2}\[([^\]]*)\]\(([^)]+)\)/},get img(){return /^!\[([^\]]*)\]\(([^)]+)\)/}},d={},k={},e=function(){function e(a,c){_classCallCheck(this,e),this.dom=a,this.config=c,this.prevRoot=null}return _createClass(e,[{key:"update",value:function(f){this.dom.classList.add("markdown");var a=h(f),d=n(this.prevRoot,a.root);this.prevRoot=a.root,c(d,this.dom);var b=j(this.config);b.useHighlight&&i(this.dom,b)}}]),e}();return m.Markdown=e,m.parser=r,m.trans=t,m.codeHighlight=i,m.getParseResult=h,m.markdown=function(d,c,b){d.innerHTML="",d.classList.add("markdown");var f=h(c);t(f.root,d),b=j(b),i(d,b)},m.markdownInfo=function(d){var a=h(d),e=a.root,b=_objectWithoutProperties(a,["root"]);return b},m}({}); |
{ | ||
"name": "lys-markdown-parser", | ||
"version": "3.6.1", | ||
"version": "3.7.0", | ||
"description": "js markdown parser", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import nodeType from './../nodeType.js'; | ||
import { parser } from './../tokenizer.js'; | ||
import { join, getRealLen, padEnd } from './strHelper.js'; | ||
import { parser } from './../parser/index.js'; | ||
/** @typedef {import("./../../@type/index").ASTNode} ASTNode */ | ||
function fmtTable(/** @type {ASTNode} */ node) { | ||
// 对于table的格式化 | ||
// 寻找每一列中最宽的那一个,按照这种逻辑进行格式化 | ||
// 根据index快速对元素进行左右行交换 | ||
const [thead, tbody] = node.children; | ||
const allRows = [ | ||
...thead.children, // tr list | ||
...tbody.children, | ||
]; | ||
const maxLen = Math.max(...allRows.map((i) => i.children.length)); | ||
const charLen = []; | ||
for (let index = 0; index < maxLen; index++) { | ||
const maxChar = Math.max( | ||
...allRows.map((item) => { | ||
return getRealLen(item.children[index]?.raw.text); | ||
}) | ||
); | ||
charLen.push(maxChar); | ||
// 一种table格式化对的方案,通过canvas计算字符宽度,进而对字符串进行补齐 | ||
// why? 因为不同字体对字符的渲染宽度很不一样,从而导致一般意义的判断(区分字符是英文、中文进行补齐)的操作满足不了需求 | ||
// 从这个角度来讲,可以理解为什么有些人更喜欢等宽字体,等宽字体的渲染宽度符合一般人的直觉预期 | ||
// 使用canvas计算字体宽度,从根本上解决了对齐的计算问题, | ||
// 需要注意不用网站编辑的字体不一致,会导致在A站上对齐,在B站上不行的问题。(此问题无解) | ||
// chrome devtool 的字体是 12px monospace | ||
/** | ||
* table文本格式化,自动对齐 | ||
* @param {string} str [table文本] | ||
* @param {string} font [展示table文本的元素css font] | ||
* @returns {string} | ||
*/ | ||
export function formatTable(str, font = '12px monospace') { | ||
var canvas = document.createElement('canvas'); | ||
var ctx = canvas.getContext('2d'); | ||
ctx.font = font; | ||
function getWidth(str) { | ||
return ctx.measureText(str).width; | ||
} | ||
const allList = [ | ||
thead.children[0].children.map((item, index) => { | ||
return padEnd(format(item), charLen[index], ' '); | ||
}), | ||
charLen.map((i) => '-'.repeat(i)), | ||
...tbody.children.map((item) => { | ||
return item.children.map((it, index) => { | ||
return padEnd(format(it), charLen[index], ' '); | ||
}); | ||
}), | ||
].map((i) => join(i)); | ||
// 以行首为缩进基准 | ||
const ident = str.match(/^\s*/)[0]; | ||
// table第二行使用【-】进行补齐,其他使用空格进行补齐 | ||
const [spaceChar, splitChar] = [' ', '-']; | ||
const [spaceW, splitW] = [getWidth(spaceChar), getWidth(splitChar)]; | ||
const column = []; | ||
return allList.join('\n'); | ||
return str | ||
.split('\n') | ||
.map((ele) => { | ||
return ele | ||
.trim() | ||
.replace(/^\||\|$/, '') | ||
.split('|') | ||
.map((i) => i.trim()) | ||
.map((i, index) => { | ||
if (/^-+$/.test(i)) { | ||
i = '--'; | ||
} | ||
const width = getWidth(i); | ||
column[index] = Math.max(column[index] || 0, width); | ||
return { i, width }; | ||
}); | ||
}) | ||
.map((row) => { | ||
return ( | ||
ident + | ||
row | ||
.map((ele, index, arr) => { | ||
const { i, width } = ele; | ||
const paddingW = column[index] - width; | ||
const [padchar, num, perWidth] = /^-+$/.test(i.trim()) | ||
? [splitChar, Math.round(paddingW / splitW), splitW] | ||
: [ | ||
spaceChar, | ||
Math.round(paddingW / spaceW), | ||
spaceW, | ||
]; | ||
// 如果当前宽度没有被补齐,则同一行下一个字符串的宽度需要被缩短 | ||
if (arr[index + 1]) { | ||
arr[index + 1].width -= paddingW - perWidth * num; | ||
} | ||
return i + padchar.repeat(num); | ||
}) | ||
.join(' | ') | ||
); | ||
}) | ||
.join('\n'); | ||
} | ||
function fmtList1(str) { | ||
return str | ||
.split(/\n/g) | ||
.map((i) => { | ||
return i.replace(/^\s+/, ($0) => { | ||
return ' '.repeat($0.length - ($0.length % 4)); | ||
}); | ||
}) | ||
.join('\n'); | ||
} | ||
/** | ||
@@ -57,4 +102,7 @@ * 因为js是单线程的,因此可以借助这个特性在这里使用类似全局变量 | ||
function fmtList(pnode) { | ||
const tokensStr = pnode.tokens.map((i) => i.raw).join(''); | ||
const startSpace = tokensStr.match(/^\s*/)[0]; | ||
const endSpace = tokensStr.match(/\s*$/)[0]; | ||
const LIST_STYLE_CHAR = pnode.listStyleType === 'decimal' ? '+' : '-'; | ||
ident += 1; | ||
const LIST_STYLE_CHAR = pnode.listStyleType === 'decimal' ? '+' : '-'; | ||
@@ -94,6 +142,3 @@ // 需要有堆栈的概念,去处理缩进问题 | ||
ident -= 1; | ||
if (pnode.__root) { | ||
result += `\n`; | ||
} | ||
return result; | ||
return startSpace + result + endSpace; | ||
} | ||
@@ -104,5 +149,6 @@ | ||
* @param {ASTNode} pnode | ||
* @param {HTMLElement} container | ||
* @returns {string} | ||
*/ | ||
function format(pnode) { | ||
function format(pnode, container) { | ||
return pnode.children | ||
@@ -115,6 +161,7 @@ .map((node) => { | ||
case nodeType.table: { | ||
return fmtTable(node); | ||
const tstr = node.tokens.map((i) => i.raw).join(''); | ||
return formatTable(tstr, getComputedStyle(container).font); | ||
} | ||
case nodeType.ul: | ||
return fmtList(node); | ||
return fmtList1(node.tokens.map((i) => i.raw).join('')); | ||
case nodeType.h1: | ||
@@ -128,3 +175,3 @@ case nodeType.h2: | ||
default: | ||
return node.raw.text; | ||
return node.tokens.map((i) => i.raw).join(''); | ||
} | ||
@@ -142,3 +189,3 @@ }) | ||
return ( | ||
`#` + ' '.repeat(pnode.__headLen) + format(pnode).trimLeft() + '\n' | ||
'#' + ' '.repeat(pnode.__headLen) + format(pnode).trimStart() + '\n' | ||
); | ||
@@ -155,5 +202,5 @@ } | ||
*/ | ||
export default function formater(str) { | ||
export function formater(str, dom) { | ||
const result = parser(str); | ||
return format(result); | ||
return format(result, dom); | ||
} |
import { diffNode } from './diff.js'; | ||
import { parser } from './tokenizer.js'; | ||
import { parser } from './parser/index.js'; | ||
import patch from './patch.js'; | ||
import trans from './trans.js'; | ||
import { getParserNodeInfo } from './helper.js'; | ||
import { formater } from './formater/index.js'; | ||
@@ -112,2 +113,3 @@ const cache = {}; | ||
Markdown, | ||
formater, | ||
parser, | ||
@@ -114,0 +116,0 @@ trans, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
323217
35
7437