@mdgate/html 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{hasOleMagic as L0}from"@mdgate/containers";import{ConvertError as O}from"@mdgate/core";import{documentToMarkdown as Wf}from"@mdgate/document";import{fileExtension as _f}from"@mdgate/utils";import{decodeFragment as z0,mimeHeader as Xf,mimeTextHtml as J0,mimeTextPlain as Q0,parseMime as Z0,parseXml as $0,walkMimeParts as K0}from"@mdgate/containers";import{ConvertError as F0}from"@mdgate/core";import{emptyDocument as Mf,plain as V0}from"@mdgate/document";import{AssetSink as Df,mediaTypeFor as G0}from"@mdgate/office-common";import{decode as j0,isAbsoluteUri as Hf,trim as Cf}from"@mdgate/utils";import{Element as m}from"@mdgate/containers";var l=new Set(["area","base","basefont","bgsound","br","col","embed","frame","hr","img","input","keygen","link","meta","param","source","track","wbr"]),Pf=new Set(["script","style","textarea","title","noscript"]),Rf=new Set(["address","article","aside","blockquote","div","dl","fieldset","footer","form","h1","h2","h3","h4","h5","h6","header","hr","main","nav","ol","p","pre","section","table","ul"]);function t(f){let U=[],z=0;while(z<f.length){if(f.charCodeAt(z)!==60){z+=1;continue}if(f.startsWith("<!--",z)){let Q=f.indexOf("-->",z+4);if(Q<0)return!1;z=Q+3;continue}if(f.startsWith("<![CDATA[",z)){let Q=f.indexOf("]]>",z+9);if(Q<0)return!1;z=Q+3;continue}if(f.startsWith("<?",z)){let Q=f.indexOf("?>",z+2);if(Q<0)return!1;z=Q+2;continue}if(f.startsWith("<!",z)){let Q=s(f,z);if(Q<0)return!1;z=Q;continue}if(f.startsWith("</",z)){z+=2;let Q=B(f,z);if(Q.length===0)return!1;if(z+=Q.length,z=D(f,z),f[z]!==">")return!1;z+=1;let K=U.pop();if(K===void 0||K!==Q)return!1;continue}z+=1;let J=B(f,z);if(J.length===0)return!1;z+=J.length;let Z=Ef(f,z);if(Z===void 0)return!1;if(z=Z.next,Z.empty)continue;if(l.has(J.toLowerCase()))return!1;U.push(J)}return U.length===0}function R(f){let U=new m(void 0,""),z=[U],J=0;while(J<f.length){if(f.charCodeAt(J)!==60){let $=J,F=f.indexOf("<",J);J=F<0?f.length:F;let G=P(f.slice($,J));if(G.length>0)L(z[z.length-1],G);continue}if(f.startsWith("<!--",J)){let $=f.indexOf("-->",J+4);J=$<0?f.length:$+3;continue}if(f.startsWith("<![CDATA[",J)){let $=f.indexOf("]]>",J+9),F=$<0?f.slice(J+9):f.slice(J+9,$);if(F.length>0)L(z[z.length-1],F);J=$<0?f.length:$+3;continue}if(f.startsWith("<?",J)){let $=f.indexOf("?>",J+2);J=$<0?f.length:$+2;continue}if(f.startsWith("<!",J)){let $=s(f,J);J=$<0?f.length:$;continue}if(f.startsWith("</",J)){J+=2;let $=A(f,J).toLowerCase();if($.length===0){L(z[z.length-1],"</");continue}if(J+=$.length,J=D(f,J),f[J]===">")J+=1;Nf(z,$);continue}if(!i(f.charCodeAt(J+1))){L(z[z.length-1],"<"),J+=1;continue}J+=1;let Z=A(f,J),Q=Z.toLowerCase();J+=Z.length;let K=Tf(f,J);J=K.next,uf(z,Q);let V=new m(void 0,Q,K.attrs);if(z[z.length-1].children.push({type:"elem",elem:V}),K.empty||l.has(Q))continue;if(Pf.has(Q)){let $=xf(f,J,Q),F=$<0?f.slice(J):f.slice(J,$);if(F.length>0)V.children.push({type:"text",text:F});if($<0)J=f.length;else J=f.indexOf(">",$),J=J<0?f.length:J+1;continue}z.push(V)}return U}function uf(f,U){let z=f[f.length-1];if(z===void 0||f.length<2)return;let J=z.local;if(U==="li"&&J==="li"){f.pop();return}if((U==="dt"||U==="dd")&&(J==="dt"||J==="dd")){f.pop();return}if((U==="td"||U==="th")&&(J==="td"||J==="th")){f.pop();return}if(U==="tr"){if(J==="td"||J==="th")f.pop();if(f.length>=2&&f[f.length-1].local==="tr")f.pop();return}if(Rf.has(U)&&J==="p")f.pop()}function Nf(f,U){for(let z=f.length-1;z>=1;z-=1)if(f[z].local===U){f.length=z;return}}function L(f,U){let z=f.children,J=z[z.length-1];if(J?.type==="text")J.text+=U;else z.push({type:"text",text:U})}function B(f,U){let z=U;while(U<f.length&&a(f.charCodeAt(U)))U+=1;return f.slice(z,U)}function A(f,U){let z=U;while(U<f.length&&vf(f.charCodeAt(U)))U+=1;return f.slice(z,U)}function i(f){return f>=65&&f<=90||f>=97&&f<=122||f===58||f===95}function a(f){return i(f)||f>=48&&f<=57||f===45||f===46}function vf(f){return a(f)}function Ef(f,U){for(;;){U=D(f,U);let z=f[U];if(z===void 0)return;if(z===">")return{next:U+1,empty:!1};if(z==="/"&&f[U+1]===">")return{next:U+2,empty:!0};let J=B(f,U);if(J.length===0)return;if(U+=J.length,U=D(f,U),f[U]!=="=")return;U+=1,U=D(f,U);let Z=f.charCodeAt(U);if(Z!==34&&Z!==39)return;U+=1;let Q=f.indexOf(String.fromCharCode(Z),U);if(Q<0)return;U=Q+1}}function Tf(f,U){let z=[];for(;;){U=D(f,U);let J=f[U];if(J===void 0)return{next:U,empty:!1,attrs:z};if(J===">")return{next:U+1,empty:!1,attrs:z};if(J==="/"&&f[U+1]===">")return{next:U+2,empty:!0,attrs:z};let Z=A(f,U);if(Z.length===0){U+=1;continue}U+=Z.length,U=D(f,U);let Q="";if(f[U]==="="){U+=1,U=D(f,U);let K=f.charCodeAt(U);if(K===34||K===39){U+=1;let V=f.indexOf(String.fromCharCode(K),U),$=V<0?f.slice(U):f.slice(U,V);U=V<0?f.length:V+1,Q=P($)}else{let V=U;while(U<f.length){let $=f.charCodeAt(U);if($===62||$===47||$===32||$===9||$===10||$===13)break;U+=1}Q=P(f.slice(V,U))}}z.push({ns:void 0,local:Z.toLowerCase(),value:Q})}}function xf(f,U,z){let J=`</${z}`,Z=U;while(Z<f.length){let Q=hf(f,J,Z);if(Q<0)return-1;let K=Q+J.length,V=f.charCodeAt(K);if(V===62||V===32||V===9||V===10||V===13||Number.isNaN(V))return Q;Z=Q+1}return-1}function hf(f,U,z){let J=U.length,Z=f.length-J;for(let Q=z;Q<=Z;Q+=1){let K=!0;for(let V=0;V<J;V+=1){let $=f.charCodeAt(Q+V),F=U.charCodeAt(V);if($===F)continue;let G=$>=65&&$<=90?$+32:$,q=F>=65&&F<=90?F+32:F;if(G!==q){K=!1;break}}if(K)return Q}return-1}function s(f,U){U+=2;let z=0;while(U<f.length){let J=f[U];if(J==='"'||J==="'"){let Z=J;U+=1;while(U<f.length&&f[U]!==Z)U+=1;if(U<f.length)U+=1;continue}if(J==="<")z+=1;else if(J===">"){if(z===0)return U+1;z-=1}U+=1}return-1}function D(f,U){while(U<f.length){let z=f.charCodeAt(U);if(z!==32&&z!==9&&z!==10&&z!==13)break;U+=1}return U}function P(f){let U="",z=0;while(z<f.length){let J=f.indexOf("&",z);if(J<0){U+=f.slice(z);break}U+=f.slice(z,J);let Z=f.indexOf(";",J+1);if(Z<0||Z-J>32){U+="&",z=J+1;continue}let Q=kf(f.slice(J+1,Z));if(Q!==void 0)U+=Q,z=Z+1;else U+="&",z=J+1}return U}function kf(f){if(f.startsWith("#")){let z=f.slice(1),J=z.startsWith("x")||z.startsWith("X"),Z=J?z.slice(1):z,Q=Number.parseInt(Z,J?16:10);if(!Number.isFinite(Q))return;try{return String.fromCodePoint(Q)}catch{return}}return{amp:"&",lt:"<",gt:">",apos:"'",quot:'"',nbsp:" ",shy:"",mdash:"—",ndash:"–",lsquo:"‘",rsquo:"’",ldquo:"“",rdquo:"”",hellip:"…",copy:"©",reg:"®",trade:"™",deg:"°",middot:"·",bull:"•",sect:"§",para:"¶",laquo:"«",raquo:"»",times:"×",divide:"÷",plusmn:"±",frac12:"½",frac14:"¼",eacute:"é",egrave:"è",agrave:"à",ccedil:"ç",uuml:"ü",ouml:"ö",auml:"ä",szlig:"ß",aring:"å",oslash:"ø",aelig:"æ",euro:"€",pound:"£",yen:"¥",cent:"¢"}[f]}import{cellSpanning as of,GridBuilder as df,inlinesAreEmpty as x,inlinesToPlainText as e,plain as yf,resolveHeaderRows as pf}from"@mdgate/document";import{deltasEqual as rf,emptyDelta as Ff,mergeDelta as Vf,rebaseEmphasis as nf,resolveDelta as ff}from"@mdgate/office-common";import{cleanText as Uf,collapseWs as zf,trim as Jf}from"@mdgate/utils";function h(f,U,z){let J=new k(U,z,!0);return J.walkChildren(f,Ff()),J.finish()}var Gf={bold:void 0,italic:void 0,strike:void 0,code:void 0},u={delta:Gf,hidden:void 0};function v(){return{delta:Ff(),hidden:void 0}}function Qf(f,U){return{delta:Vf(f.delta,U.delta),hidden:U.hidden??f.hidden}}function Y(f){return rf(f.delta,Gf)&&f.hidden===void 0}var Zf=1e5,jf=1e6,cf=[],bf=[];class I{rules=[];get isEmpty(){return this.rules.length===0}addFrom(f){let U=f.rules;for(let z=0;z<U.length;z+=1)this.rules.push(U[z])}add(f){if(f.length===0)return;let U=lf(f);if(U.length===0)return;for(let z of U.split("}")){let J=z.indexOf("{");if(J<0)continue;let Z=z.slice(J+1);if(!mf(Z))continue;let Q=wf(Z);if(Y(Q.normal)&&Y(Q.important))continue;let K=z.slice(0,J);for(let V of K.split(",")){let $=V.trim();if($.length===0||$.includes(" ")||$.includes(":")||$.includes("["))continue;let F=$.indexOf("."),G,q;if(F>=0){let X=$.slice(0,F);G=X.length===0?void 0:X.toLowerCase(),q=$.slice(F+1)}else G=$.toLowerCase();let H=(q!==void 0?10:0)+(G!==void 0?1:0);if(!Y(Q.normal))this.rules.push({tag:G,className:q,priority:H,props:Q.normal});if(!Y(Q.important))this.rules.push({tag:G,className:q,priority:jf+H,props:Q.important})}}}matchingRules(f,U){let z=this.rules;if(z.length===0)return cf;let J=[];for(let Z=0;Z<z.length;Z+=1){let Q=z[Z];if(Q.tag!==void 0&&Q.tag!==f)continue;if(Q.className!==void 0&&!U.includes(Q.className))continue;J.push([Q.priority,Q.props])}return J}}function mf(f){return f.includes("display")||f.includes("font-weight")||f.includes("font-style")||f.includes("text-decoration")}function lf(f){let U=f.indexOf("/*");if(U<0)return f;let z=[],J=0,Z=U;while(Z>=0){if(Z>J)z.push(f.slice(J,Z));let Q=f.indexOf("*/",Z+2);if(Q<0)return z.join("");J=Q+2,Z=f.indexOf("/*",J)}if(J<f.length)z.push(f.slice(J));return z.join("")}function wf(f){let U={normal:v(),important:v()};for(let z of f.split(";")){let J=z.indexOf(":");if(J<0)continue;let Z=z.slice(0,J).trim().toLowerCase(),Q=z.slice(J+1).trim().toLowerCase(),K=!1,V=Q.indexOf("!");if(V>=0){if(Q.slice(V+1).trim()==="important")K=!0;Q=Q.slice(0,V).trimEnd()}let $=K?U.important:U.normal;switch(Z){case"font-weight":{let F=E(Q);$.delta.bold=Q==="bold"||Q==="bolder"||F!==void 0&&F>=600;break}case"font-style":$.delta.italic=Q==="italic"||Q==="oblique";break;case"text-decoration":case"text-decoration-line":if(Q.includes("line-through"))$.delta.strike=!0;else if(Q==="none")$.delta.strike=!1;break;case"display":$.hidden=Q==="none";break;default:break}}return U}function E(f){if(!/^\d+$/.test(f))return;let U=Number(f);if(!Number.isSafeInteger(U)||U<0||U>4294967295)return;return U}class k{blocks=[];inlines=[];css;ctx;startBoundary;constructor(f,U,z){this.css=f,this.ctx=U,this.startBoundary=z}finish(){return this.flushParagraph(),this.blocks}subBlocks(f,U){return this.subBlocksAt(f,U,!0)}subBlocksAt(f,U,z){let J=new k(this.css,this.ctx,z);return J.walkChildren(f,U),J.finish()}elementProps(f){let U=f.attrAny("style");if(this.css.isEmpty&&U===void 0)return u;let z=f.attrAny("class"),J=z!==void 0&&z.length>0?z.split(/\s+/).filter((K)=>K.length>0):bf,Z=this.css.matchingRules(f.local,J);if(U!==void 0){let K=wf(U);Z.push([Zf,K.normal]),Z.push([jf+Zf,K.important])}if(Z.length===0)return u;if(Z.length===1)return Qf(u,Z[0][1]);Z.sort((K,V)=>K[0]-V[0]);let Q=v();for(let K=0;K<Z.length;K+=1)Q=Qf(Q,Z[K][1]);return Q}pushAnchor(f){let U=f.attrAny("id");if(U!==void 0&&U.length>0)this.inlines.push({type:"anchor",id:this.ctx.anchorId(U)});if(f.local==="a"){let z=f.attrAny("name");if(z!==void 0&&z.length>0)this.inlines.push({type:"anchor",id:this.ctx.anchorId(z)})}}walkChildren(f,U){for(let z of f.children)if(z.type==="text")this.pushText(z.text,U);else this.walkElem(z.elem,U)}pushText(f,U){let z=zf(Uf(f));if(z.length===0)return;let J=z;if(T(this.inlines,this.startBoundary))J=U0(z," ");if(J.length===0)return;this.inlines.push({type:"text",text:J,style:ff(U)})}walkElem(f,U){let z=this.elementProps(f);if(z.hidden===!0)return;U=Vf(tf(f,U),z.delta);let J=f.local;if(J==="h1"||J==="h2"||J==="h3"||J==="h4"||J==="h5"||J==="h6"){this.flushParagraph();let Z=Number.parseInt(J.slice(1),10)||1,Q=this.inlineChildren(f,U);nf(Q,ff(U));let K=f.attrAny("id"),V=K!==void 0?this.ctx.anchorId(K):void 0;if(!x(Q))this.blocks.push({type:"heading",level:Z,anchor:V,content:Q});else{let $=[];if(V!==void 0)$.push({type:"anchor",id:V});for(let F of Q)if(F.type==="anchor")$.push(F);if($.length>0)this.blocks.push({type:"paragraph",inlines:$})}return}if(J==="p"){this.flushParagraph(),this.pushAnchor(f);let Z=this.inlines.splice(0,this.inlines.length);if(Z.push(...this.inlineChildren(f,U)),N(Z))this.blocks.push({type:"paragraph",inlines:Z});return}if(J==="ul"||J==="ol"){this.flushParagraph(),this.blocks.push(...this.parseList(f,U));return}if(J==="table"){this.flushParagraph();let Z=f.childElems().find((K)=>K.local==="caption");if(Z!==void 0){let K=this.inlineChildren(Z,U);if(N(K))this.blocks.push({type:"paragraph",inlines:K})}let Q=this.parseTable(f,U);if(Q!==void 0)this.blocks.push(Q);return}if(J==="blockquote"){this.flushParagraph();let Z=this.subBlocks(f,U);if(Z.length>0)this.blocks.push({type:"blockQuote",blocks:Z});return}if(J==="pre"){this.flushParagraph();let Z=f.text();if(Jf(Z).length>0)this.blocks.push({type:"codeBlock",lang:void 0,text:Z});return}if(J==="hr"){this.flushParagraph(),this.blocks.push({type:"rule"});return}if(qf(J)){if(this.pushAnchor(f),sf(f))this.flushParagraph(),this.walkChildren(f,U),this.flushParagraph();else this.walkChildren(f,U);return}if(J==="script"||J==="style"||J==="head"||J==="template"||J==="noscript")return;this.walkInline(f,U)}walkInline(f,U){this.pushAnchor(f);let z=f.local;if(z==="br"){this.inlines.push({type:"lineBreak"});return}if(z==="img"||z==="image"){let J=Uf(f.attrAny("alt")??""),Z=f.attrAny("src")??f.attrAny("href")??"",Q=this.ctx.imageSource(Z);if(Q!==void 0||Jf(J).length>0)this.inlines.push({type:"image",alt:J,source:Q??{type:"unavailable"}});return}if(z==="a"){let J=f.attrAny("href"),Z=J!==void 0?this.ctx.linkTarget(J):void 0,Q=this.inlineChildrenAt(f,U,T(this.inlines,this.startBoundary));if(Z!==void 0)this.inlines.push({type:"link",content:Q,target:Z});else this.inlines.push(...Q);return}this.walkChildren(f,U)}inlineChildren(f,U){return this.inlineChildrenAt(f,U,!0)}inlineChildrenAt(f,U,z){let J=this.subBlocksAt(f,U,z);if(J.length===1&&J[0].type==="paragraph")return J[0].inlines;let Z=[];for(let Q=0;Q<J.length;Q+=1){if(Q>0)Z.push({type:"lineBreak"});let K=J[Q];if(K.type==="paragraph")Z.push(...K.inlines);else if(K.type==="heading")Z.push(...K.content);else Z.push(yf(zf(g(K))))}return Z}parseList(f,U){let z=f.local==="ol",J=f.childElems().filter((w)=>w.local==="li");if(J.length===0)return[];if(!z)return[{type:"list",list:{marker:"bullet",start:1,items:J.map((j)=>({blocks:this.subBlocks(j,U),checked:void 0,markerLabel:void 0}))}}];let Z=f.attrAny("type"),Q="decimal";if(Z==="a")Q="lowerAlpha";else if(Z==="A")Q="upperAlpha";else if(Z==="i")Q="lowerRoman";else if(Z==="I")Q="upperRoman";let K=f.attrAny("reversed")!==void 0,V=f.attrAny("start"),F=(V!==void 0?$f(V):void 0)??(K?J.length:1),G=[];for(let w of J){let j=w.attrAny("value"),M=j!==void 0?$f(j):void 0;if(M!==void 0)F=M;G.push(F),F=K?F-1:F+1}if(G.some((w)=>w<1)){let w=J.map((j,M)=>({blocks:this.subBlocks(j,U),checked:void 0,markerLabel:`${G[M]}.`}));return[{type:"list",list:{marker:Q,start:1,items:w}}]}let q=[],H,X=0;for(let w=0;w<J.length;w+=1){let j=G[w],M={blocks:this.subBlocks(J[w],U),checked:void 0,markerLabel:void 0};if(!(H!==void 0&&X+1===j)){if(H!==void 0)q.push({type:"list",list:H});H={marker:Q,start:j,items:[]}}H.items.push(M),X=j}if(H!==void 0)q.push({type:"list",list:H});return q}parseTable(f,U){let z=[],J=0,Z=!1;for(let F of f.childElems())if(F.local==="thead"||F.local==="tbody"||F.local==="tfoot"){if(Z)Z=!1,J+=1;let G=F.local==="thead";for(let q of F.childElems())if(q.local==="tr")z.push([q,G,J]);J+=1}else if(F.local==="tr")Z=!0,z.push([F,!1,J]);if(z.length===0)return;let Q=new Map;for(let F=0;F<z.length;F+=1)Q.set(z[F][2],F);let K=new df,V=0;for(let F=0;F<z.length;F+=1){let[G,q,H]=z[F];K.nextRow();let X=!0,w=!1;for(let j of G.childElems()){if(j.local!=="td"&&j.local!=="th")continue;if(w=!0,j.local!=="th")X=!1;let M=Kf(E(j.attrAny("colspan")??"")??1,1,1000),C,b=j.attrAny("rowspan"),S=b!==void 0?E(b):void 0;if(S===0)C=(Q.get(H)??F)-F+1;else if(S!==void 0)C=Kf(S,1,65534);else C=1;K.place(of(this.subBlocks(j,U),M,C))}if(F===V&&(q||X&&w))V+=1}let $=K.finish("data");if($.grid.length===0)return;return $.headerRows=pf($,V),{type:"table",table:$}}flushParagraph(){if(this.inlines.length>0){let f=this.inlines.splice(0,this.inlines.length);if(N(f))this.blocks.push({type:"paragraph",inlines:f})}this.startBoundary=!0}}function tf(f,U){let z={...U};switch(f.local){case"b":case"strong":z.bold=!0;break;case"i":case"em":case"cite":case"dfn":case"var":z.italic=!0;break;case"s":case"del":case"strike":z.strike=!0;break;case"code":case"kbd":case"samp":case"tt":z.code=!0;break;default:break}return z}function g(f){switch(f.type){case"paragraph":return e(f.inlines);case"heading":return e(f.content);case"list":return f.list.items.flatMap((U)=>U.blocks.map(g)).join(" ");case"blockQuote":return f.blocks.map(g).join(" ");case"codeBlock":return f.text;case"table":return f.table.grid.flatMap((U)=>U.flatMap((z)=>z.type==="origin"?[z.cell.blocks.map(g).join(" ")]:[])).join(" ");case"rule":return""}}function qf(f){return f==="div"||f==="section"||f==="article"||f==="aside"||f==="main"||f==="nav"||f==="header"||f==="footer"||f==="figure"||f==="figcaption"||f==="center"||f==="details"||f==="summary"||f==="li"||f==="dl"||f==="dt"||f==="dd"||f==="body"}function af(f){return qf(f)||f==="p"||f==="ul"||f==="ol"||f==="table"||f==="blockquote"||f==="pre"||f==="hr"||f==="h1"||f==="h2"||f==="h3"||f==="h4"||f==="h5"||f==="h6"}function sf(f){return f.childElems().some((U)=>af(U.local))}function N(f){return!x(f)||f.some((U)=>U.type==="anchor")}function T(f,U){for(let z=f.length-1;z>=0;z-=1){let J=f[z];if(J.type==="anchor")continue;if(J.type==="text"){if(J.text.length===0)continue;return f0(ef(J.text))}if(J.type==="lineBreak")return!0;if(J.type==="link"){if(x(J.content))continue;return T(J.content,!1)}return!1}return U}function ef(f){let U=f.length;if(U===0)return 0;let z=f.charCodeAt(U-1);if(z>=56320&&z<=57343&&U>=2){let J=f.charCodeAt(U-2);if(J>=55296&&J<=56319)return(J-55296<<10)+(z-56320)+65536}return z}function f0(f){return f>=9&&f<=13||f===32||f===133||f===160||f===5760||f>=8192&&f<=8202||f===8232||f===8233||f===8239||f===8287||f===12288}function U0(f,U){let z=0;while(z<f.length&&f[z]===U)z+=1;return f.slice(z)}function $f(f){let U=f.trim();if(!/^-?\d+$/.test(U))return;let z=Number(U);return Number.isFinite(z)?z:void 0}function Kf(f,U,z){return Math.min(z,Math.max(U,f))}function Lf(f){let U=w0(f),z=new Df;return If(U,new d(void 0,z),z)}function Yf(f){let U=Z0(f),z=J0(U)??Q0(U);if(z===void 0)throw F0.malformed("no text/html or text/plain part");let J=new Df,Z=X0(U),Q=D0(z);if(z.contentType==="text/plain"){let V=Mf(),$=Cf(Q);if($.length>0)V.blocks.push({type:"paragraph",inlines:[V0($)]});return V.assets=J.assets,V}let K=gf(Q,new TextEncoder().encode(Q));return If(K,new d(Z,J),J)}function w0(f){return gf(C0(f),f)}function gf(f,U){if(t(f))try{return $0(U)}catch{}return R(f)}function If(f,U,z){Of(f);let J=q0(f),Z=H0(f),Q=Mf();return Q.blocks.push(...h(Z,J,U)),Q.assets=z.assets,Q}function q0(f){let U=new I;for(let z of f.descendantElems())if(z.local==="style")U.add(z.text());return U}function H0(f){if(f.local==="body")return f;let U=f.local==="html"?f:o(f,"html");if(U!==void 0){let J=o(U,"body");if(J!==void 0)return J;return U}return o(f,"body")??f}function o(f,U){return f.childElems().find((z)=>z.local===U)}function Of(f){f.local=f.local.toLowerCase();let U=f.attrs;for(let z=0;z<U.length;z+=1)U[z].local=U[z].local.toLowerCase();for(let z of f.childElems())Of(z)}class d{cids;assets;constructor(f,U){this.cids=f;this.assets=U}linkTarget(f){if(f.length===0)return;if(f.startsWith("#")){let U=z0(f.slice(1));return U.length>0?{type:"anchor",id:U}:void 0}if(Hf(f))return{type:"external",url:f};return{type:"relative",url:f}}imageSource(f){if(f.length===0)return;let U=this.resolveCid(f);if(U!==void 0)return U;if(Hf(f))return{type:"external",url:f};return}anchorId(f){return f}resolveCid(f){if(this.cids===void 0)return;if(f.length<4||f.slice(0,4).toLowerCase()!=="cid:")return;let U=f.slice(4);try{U=decodeURIComponent(U)}catch{}if(U.startsWith("<")&&U.endsWith(">")&&U.length>=2)U=U.slice(1,-1);let z=this.cids.get(U.toLowerCase());if(z===void 0)return;let J=z.filename??U,Z=z.contentType.length>0?z.contentType:G0(J);return{type:"asset",id:this.assets.add(Z,J,z.bytes)}}}function X0(f){let U=new Map;for(let z of K0(f)){let J=Xf(z,"content-id");if(J===void 0)continue;let Z=M0(J);if(Z.length>0)U.set(Z.toLowerCase(),z)}return U}function M0(f){let U=Cf(f);if(U.startsWith("<")&&U.endsWith(">")&&U.length>=2)U=U.slice(1,-1);return U}function D0(f){let U=Xf(f,"content-type"),z="utf-8";if(U!==void 0){let J=/charset\s*=\s*("?)([^";\s]+)\1/i.exec(U);if(J!==null)z=J[2]}try{return j0(f.bytes,z)}catch{return new TextDecoder("utf-8",{fatal:!1}).decode(f.bytes)}}function C0(f){if(f.length>=2&&f[0]===255&&f[1]===254)return new TextDecoder("utf-16le").decode(f);if(f.length>=2&&f[0]===254&&f[1]===255)return new TextDecoder("utf-16be").decode(f);if(f.length>=3&&f[0]===239&&f[1]===187&&f[2]===191)return new TextDecoder("utf-8").decode(f.subarray(3));return new TextDecoder("utf-8").decode(f)}var Y0=new Set(["htm","html","html4","html5","xhtml","mhtml","mht"]),Sf=new Set(["mhtml","mht"]);function n(){return{id:"html",sniff(f,U){let z=U?.path!==void 0?_f(U.path):void 0;if(Bf(f)||z!==void 0&&Sf.has(z))return 3;if(W0(f))return 0;if(Af(f)&&!y(f))return 3;if(O0(f)&&!y(f))return 2;if(z!==void 0&&Y0.has(z))return 1;return 0},convert(f,U){g0(f);let z=U?.path!==void 0?_f(U.path):void 0;if(z!==void 0&&Sf.has(z)||Bf(f))return{markdown:Wf(Yf(f))};if(y(f))throw O.unsupported("OpenDocument");return{markdown:Wf(Lf(f))}}}}function g0(f){if(I0(f))throw O.unsupported("pdf");if(L0(f))throw O.unsupported("ole");if(f.length>=4&&f[0]===80&&f[1]===75&&f[2]===3&&f[3]===4)throw O.unsupported("zip")}function I0(f){let U=c(f);return _0(f,"%PDF-",U)}function O0(f){if(Af(f))return!0;return _(f.subarray(0,Math.min(f.length,4096))).includes("http://www.w3.org/1999/xhtml")}function Af(f){let U=c(f);if(W(f,"<!doctype html",U)&&!r(f[U+14]))return!0;return W(f,"<html",U)&&!r(f[U+5])}function W0(f){let U=c(f);if(W(f,"<svg",U)&&!r(f[U+4]))return!0;if(!W(f,"<?xml",U))return!1;let z=_(f.subarray(0,Math.min(f.length,8192)));return/<svg\b/i.test(z)}function Bf(f){return _(f.subarray(0,Math.min(f.length,4096))).toLowerCase().includes("content-location:")}function y(f){let U=_(f.subarray(0,Math.min(f.length,8192))),z=p(U,0);if(U.startsWith("<?xml",z)){let Z=U.indexOf("?>",z);if(Z<0)return!1;z=p(U,Z+2)}while(U.startsWith("<!--",z)){let Z=U.indexOf("-->",z+4);if(Z<0)return!1;z=p(U,Z+3)}let J=U.slice(z,z+2048);return/<office:document\b/.test(J)}function c(f){let U=0;if(f.length>=3&&f[0]===239&&f[1]===187&&f[2]===191)U=3;while(U<f.length){let z=f[U];if(z!==9&&z!==10&&z!==13&&z!==32)break;U+=1}return U}function p(f,U){while(U<f.length){let z=f.charCodeAt(U);if(z!==9&&z!==10&&z!==13&&z!==32)break;U+=1}return U}function _0(f,U,z){if(z+U.length>f.length)return!1;for(let J=0;J<U.length;J+=1)if(f[z+J]!==U.charCodeAt(J))return!1;return!0}function W(f,U,z){if(z+U.length>f.length)return!1;for(let J=0;J<U.length;J+=1){let Z=f[z+J],Q=U.charCodeAt(J);if(Z===Q)continue;let K=Z>=65&&Z<=90?Z+32:Z,V=Q>=65&&Q<=90?Q+32:Q;if(K!==V)return!1}return!0}function r(f){if(f===void 0)return!1;return f>=48&&f<=57||f>=65&&f<=90||f>=97&&f<=122||f===45||f===95||f===58}function _(f){return new TextDecoder("utf-8",{fatal:!1}).decode(f)}import{create as S0}from"@mdgate/core";var B0=S0([n()]);function A0(f,U){return B0(f,U)}export{A0 as toMarkdown,h as toBlocks,R as parseHtml,n as html,I as Stylesheet};
|
|
1
|
+
import{hasOleMagic as Y0}from"@mdgate/containers";import{ConvertError as O}from"@mdgate/core";import{documentToMarkdown as Wf}from"@mdgate/document";import{fileExtension as _f}from"@mdgate/utils";import{decodeFragment as J0,mimeHeader as Xf,mimeTextHtml as Q0,mimeTextPlain as $0,parseMime as K0,parseXml as F0,walkMimeParts as V0}from"@mdgate/containers";import{ConvertError as Z0}from"@mdgate/core";import{emptyDocument as Mf,plain as G0}from"@mdgate/document";import{AssetSink as Df,mediaTypeFor as j0}from"@mdgate/office-common";import{decode as w0,isAbsoluteUri as Hf,trim as Cf}from"@mdgate/utils";import{Element as m}from"@mdgate/containers";var l=new Set(["area","base","basefont","bgsound","br","col","embed","frame","hr","img","input","keygen","link","meta","param","source","track","wbr"]),Rf=new Set(["script","style","textarea","title","noscript"]),uf=new Set(["address","article","aside","blockquote","div","dl","fieldset","footer","form","h1","h2","h3","h4","h5","h6","header","hr","main","nav","ol","p","pre","section","table","ul"]);function t(f){let U=[],z=0;while(z<f.length){if(f.charCodeAt(z)!==60){z+=1;continue}if(f.startsWith("<!--",z)){let Q=f.indexOf("-->",z+4);if(Q<0)return!1;z=Q+3;continue}if(f.startsWith("<![CDATA[",z)){let Q=f.indexOf("]]>",z+9);if(Q<0)return!1;z=Q+3;continue}if(f.startsWith("<?",z)){let Q=f.indexOf("?>",z+2);if(Q<0)return!1;z=Q+2;continue}if(f.startsWith("<!",z)){let Q=s(f,z);if(Q<0)return!1;z=Q;continue}if(f.startsWith("</",z)){z+=2;let Q=B(f,z);if(Q.length===0)return!1;if(z+=Q.length,z=D(f,z),f[z]!==">")return!1;z+=1;let F=U.pop();if(F===void 0||F!==Q)return!1;continue}z+=1;let J=B(f,z);if(J.length===0)return!1;z+=J.length;let $=Tf(f,z);if($===void 0)return!1;if(z=$.next,$.empty)continue;if(l.has(J.toLowerCase()))return!1;U.push(J)}return U.length===0}function R(f){let U=new m(void 0,""),z=[U],J=0;while(J<f.length){if(f.charCodeAt(J)!==60){let K=J,V=f.indexOf("<",J);J=V<0?f.length:V;let G=P(f.slice(K,J));if(G.length>0)L(z[z.length-1],G);continue}if(f.startsWith("<!--",J)){let K=f.indexOf("-->",J+4);J=K<0?f.length:K+3;continue}if(f.startsWith("<![CDATA[",J)){let K=f.indexOf("]]>",J+9),V=K<0?f.slice(J+9):f.slice(J+9,K);if(V.length>0)L(z[z.length-1],V);J=K<0?f.length:K+3;continue}if(f.startsWith("<?",J)){let K=f.indexOf("?>",J+2);J=K<0?f.length:K+2;continue}if(f.startsWith("<!",J)){let K=s(f,J);J=K<0?f.length:K;continue}if(f.startsWith("</",J)){J+=2;let K=A(f,J).toLowerCase();if(K.length===0){L(z[z.length-1],"</");continue}if(J+=K.length,J=D(f,J),f[J]===">")J+=1;vf(z,K);continue}if(!i(f.charCodeAt(J+1))){L(z[z.length-1],"<"),J+=1;continue}J+=1;let $=A(f,J),Q=$.toLowerCase();J+=$.length;let F=xf(f,J);J=F.next,Nf(z,Q);let Z=new m(void 0,Q,F.attrs);if(z[z.length-1].children.push({type:"elem",elem:Z}),F.empty||l.has(Q))continue;if(Rf.has(Q)){let K=hf(f,J,Q),V=K<0?f.slice(J):f.slice(J,K);if(V.length>0)Z.children.push({type:"text",text:V});if(K<0)J=f.length;else J=f.indexOf(">",K),J=J<0?f.length:J+1;continue}z.push(Z)}return U}function Nf(f,U){let z=f[f.length-1];if(z===void 0||f.length<2)return;let J=z.local;if(U==="li"&&J==="li"){f.pop();return}if((U==="dt"||U==="dd")&&(J==="dt"||J==="dd")){f.pop();return}if((U==="td"||U==="th")&&(J==="td"||J==="th")){f.pop();return}if(U==="tr"){if(J==="td"||J==="th")f.pop();if(f.length>=2&&f[f.length-1].local==="tr")f.pop();return}if(uf.has(U)&&J==="p")f.pop()}function vf(f,U){for(let z=f.length-1;z>=1;z-=1)if(f[z].local===U){f.length=z;return}}function L(f,U){let z=f.children,J=z[z.length-1];if(J?.type==="text")J.text+=U;else z.push({type:"text",text:U})}function B(f,U){let z=U;while(U<f.length&&a(f.charCodeAt(U)))U+=1;return f.slice(z,U)}function A(f,U){let z=U;while(U<f.length&&Ef(f.charCodeAt(U)))U+=1;return f.slice(z,U)}function i(f){return f>=65&&f<=90||f>=97&&f<=122||f===58||f===95}function a(f){return i(f)||f>=48&&f<=57||f===45||f===46}function Ef(f){return a(f)}function Tf(f,U){for(;;){U=D(f,U);let z=f[U];if(z===void 0)return;if(z===">")return{next:U+1,empty:!1};if(z==="/"&&f[U+1]===">")return{next:U+2,empty:!0};let J=B(f,U);if(J.length===0)return;if(U+=J.length,U=D(f,U),f[U]!=="=")return;U+=1,U=D(f,U);let $=f.charCodeAt(U);if($!==34&&$!==39)return;U+=1;let Q=f.indexOf(String.fromCharCode($),U);if(Q<0)return;U=Q+1}}function xf(f,U){let z=[];for(;;){U=D(f,U);let J=f[U];if(J===void 0)return{next:U,empty:!1,attrs:z};if(J===">")return{next:U+1,empty:!1,attrs:z};if(J==="/"&&f[U+1]===">")return{next:U+2,empty:!0,attrs:z};let $=A(f,U);if($.length===0){U+=1;continue}U+=$.length,U=D(f,U);let Q="";if(f[U]==="="){U+=1,U=D(f,U);let F=f.charCodeAt(U);if(F===34||F===39){U+=1;let Z=f.indexOf(String.fromCharCode(F),U),K=Z<0?f.slice(U):f.slice(U,Z);U=Z<0?f.length:Z+1,Q=P(K)}else{let Z=U;while(U<f.length){let K=f.charCodeAt(U);if(K===62||K===47||K===32||K===9||K===10||K===13)break;U+=1}Q=P(f.slice(Z,U))}}z.push({ns:void 0,local:$.toLowerCase(),value:Q})}}function hf(f,U,z){let J=`</${z}`,$=U;while($<f.length){let Q=kf(f,J,$);if(Q<0)return-1;let F=Q+J.length,Z=f.charCodeAt(F);if(Z===62||Z===32||Z===9||Z===10||Z===13||Number.isNaN(Z))return Q;$=Q+1}return-1}function kf(f,U,z){let J=U.length,$=f.length-J;for(let Q=z;Q<=$;Q+=1){let F=!0;for(let Z=0;Z<J;Z+=1){let K=f.charCodeAt(Q+Z),V=U.charCodeAt(Z);if(K===V)continue;let G=K>=65&&K<=90?K+32:K,q=V>=65&&V<=90?V+32:V;if(G!==q){F=!1;break}}if(F)return Q}return-1}function s(f,U){U+=2;let z=0;while(U<f.length){let J=f[U];if(J==='"'||J==="'"){let $=J;U+=1;while(U<f.length&&f[U]!==$)U+=1;if(U<f.length)U+=1;continue}if(J==="<")z+=1;else if(J===">"){if(z===0)return U+1;z-=1}U+=1}return-1}function D(f,U){while(U<f.length){let z=f.charCodeAt(U);if(z!==32&&z!==9&&z!==10&&z!==13)break;U+=1}return U}function P(f){let U="",z=0;while(z<f.length){let J=f.indexOf("&",z);if(J<0){U+=f.slice(z);break}U+=f.slice(z,J);let $=f.indexOf(";",J+1);if($<0||$-J>32){U+="&",z=J+1;continue}let Q=of(f.slice(J+1,$));if(Q!==void 0)U+=Q,z=$+1;else U+="&",z=J+1}return U}function of(f){if(f.startsWith("#")){let z=f.slice(1),J=z.startsWith("x")||z.startsWith("X"),$=J?z.slice(1):z,Q=Number.parseInt($,J?16:10);if(!Number.isFinite(Q))return;try{return String.fromCodePoint(Q)}catch{return}}return{amp:"&",lt:"<",gt:">",apos:"'",quot:'"',nbsp:" ",shy:"",mdash:"—",ndash:"–",lsquo:"‘",rsquo:"’",ldquo:"“",rdquo:"”",hellip:"…",copy:"©",reg:"®",trade:"™",deg:"°",middot:"·",bull:"•",sect:"§",para:"¶",laquo:"«",raquo:"»",times:"×",divide:"÷",plusmn:"±",frac12:"½",frac14:"¼",eacute:"é",egrave:"è",agrave:"à",ccedil:"ç",uuml:"ü",ouml:"ö",auml:"ä",szlig:"ß",aring:"å",oslash:"ø",aelig:"æ",euro:"€",pound:"£",yen:"¥",cent:"¢"}[f]}import{cellSpanning as df,GridBuilder as yf,inlinesAreEmpty as x,inlinesToPlainText as e,plain as pf,resolveHeaderRows as rf}from"@mdgate/document";import{deltasEqual as nf,emptyDelta as Vf,mergeDelta as Zf,rebaseEmphasis as cf,resolveDelta as ff}from"@mdgate/office-common";import{cleanText as Uf,collapseWs as zf,trim as Jf}from"@mdgate/utils";function h(f,U,z){let J=new k(U,z,!0);return J.walkChildren(f,Vf()),J.finish()}var Gf={bold:void 0,italic:void 0,strike:void 0,code:void 0},u={delta:Gf,hidden:void 0};function v(){return{delta:Vf(),hidden:void 0}}function Qf(f,U){return{delta:Zf(f.delta,U.delta),hidden:U.hidden??f.hidden}}function Y(f){return nf(f.delta,Gf)&&f.hidden===void 0}var $f=1e5,jf=1e6,bf=[],mf=[];class I{rules=[];get isEmpty(){return this.rules.length===0}addFrom(f){let U=f.rules;for(let z=0;z<U.length;z+=1)this.rules.push(U[z])}add(f){if(f.length===0)return;let U=tf(f);if(U.length===0)return;for(let z of U.split("}")){let J=z.indexOf("{");if(J<0)continue;let $=z.slice(J+1);if(!lf($))continue;let Q=wf($);if(Y(Q.normal)&&Y(Q.important))continue;let F=z.slice(0,J);for(let Z of F.split(",")){let K=Z.trim();if(K.length===0||K.includes(" ")||K.includes(":")||K.includes("["))continue;let V=K.indexOf("."),G,q;if(V>=0){let X=K.slice(0,V);G=X.length===0?void 0:X.toLowerCase(),q=K.slice(V+1)}else G=K.toLowerCase();let H=(q!==void 0?10:0)+(G!==void 0?1:0);if(!Y(Q.normal))this.rules.push({tag:G,className:q,priority:H,props:Q.normal});if(!Y(Q.important))this.rules.push({tag:G,className:q,priority:jf+H,props:Q.important})}}}matchingRules(f,U){let z=this.rules;if(z.length===0)return bf;let J=[];for(let $=0;$<z.length;$+=1){let Q=z[$];if(Q.tag!==void 0&&Q.tag!==f)continue;if(Q.className!==void 0&&!U.includes(Q.className))continue;J.push([Q.priority,Q.props])}return J}}function lf(f){return f.includes("display")||f.includes("font-weight")||f.includes("font-style")||f.includes("text-decoration")}function tf(f){let U=f.indexOf("/*");if(U<0)return f;let z=[],J=0,$=U;while($>=0){if($>J)z.push(f.slice(J,$));let Q=f.indexOf("*/",$+2);if(Q<0)return z.join("");J=Q+2,$=f.indexOf("/*",J)}if(J<f.length)z.push(f.slice(J));return z.join("")}function wf(f){let U={normal:v(),important:v()};for(let z of f.split(";")){let J=z.indexOf(":");if(J<0)continue;let $=z.slice(0,J).trim().toLowerCase(),Q=z.slice(J+1).trim().toLowerCase(),F=!1,Z=Q.indexOf("!");if(Z>=0){if(Q.slice(Z+1).trim()==="important")F=!0;Q=Q.slice(0,Z).trimEnd()}let K=F?U.important:U.normal;switch($){case"font-weight":{let V=E(Q);K.delta.bold=Q==="bold"||Q==="bolder"||V!==void 0&&V>=600;break}case"font-style":K.delta.italic=Q==="italic"||Q==="oblique";break;case"text-decoration":case"text-decoration-line":if(Q.includes("line-through"))K.delta.strike=!0;else if(Q==="none")K.delta.strike=!1;break;case"display":K.hidden=Q==="none";break;default:break}}return U}function E(f){if(!/^\d+$/.test(f))return;let U=Number(f);if(!Number.isSafeInteger(U)||U<0||U>4294967295)return;return U}class k{blocks=[];inlines=[];css;ctx;startBoundary;constructor(f,U,z){this.css=f,this.ctx=U,this.startBoundary=z}finish(){return this.flushParagraph(),this.blocks}subBlocks(f,U){return this.subBlocksAt(f,U,!0)}subBlocksAt(f,U,z){let J=new k(this.css,this.ctx,z);return J.walkChildren(f,U),J.finish()}elementProps(f){let U=f.attrAny("style");if(this.css.isEmpty&&U===void 0)return u;let z=f.attrAny("class"),J=z!==void 0&&z.length>0?z.split(/\s+/).filter((F)=>F.length>0):mf,$=this.css.matchingRules(f.local,J);if(U!==void 0){let F=wf(U);$.push([$f,F.normal]),$.push([jf+$f,F.important])}if($.length===0)return u;if($.length===1)return Qf(u,$[0][1]);$.sort((F,Z)=>F[0]-Z[0]);let Q=v();for(let F=0;F<$.length;F+=1)Q=Qf(Q,$[F][1]);return Q}pushAnchor(f){let U=f.attrAny("id");if(U!==void 0&&U.length>0)this.inlines.push({type:"anchor",id:this.ctx.anchorId(U)});if(f.local==="a"){let z=f.attrAny("name");if(z!==void 0&&z.length>0)this.inlines.push({type:"anchor",id:this.ctx.anchorId(z)})}}walkChildren(f,U){for(let z of f.children)if(z.type==="text")this.pushText(z.text,U);else this.walkElem(z.elem,U)}pushText(f,U){let z=zf(Uf(f));if(z.length===0)return;let J=z;if(T(this.inlines,this.startBoundary))J=z0(z," ");if(J.length===0)return;this.inlines.push({type:"text",text:J,style:ff(U)})}walkElem(f,U){let z=this.elementProps(f);if(z.hidden===!0)return;U=Zf(af(f,U),z.delta);let J=f.local;if(J==="h1"||J==="h2"||J==="h3"||J==="h4"||J==="h5"||J==="h6"){this.flushParagraph();let $=Number.parseInt(J.slice(1),10)||1,Q=this.inlineChildren(f,U);cf(Q,ff(U));let F=f.attrAny("id"),Z=F!==void 0?this.ctx.anchorId(F):void 0;if(!x(Q))this.blocks.push({type:"heading",level:$,anchor:Z,content:Q});else{let K=[];if(Z!==void 0)K.push({type:"anchor",id:Z});for(let V of Q)if(V.type==="anchor")K.push(V);if(K.length>0)this.blocks.push({type:"paragraph",inlines:K})}return}if(J==="p"){this.flushParagraph(),this.pushAnchor(f);let $=this.inlines.splice(0,this.inlines.length);if($.push(...this.inlineChildren(f,U)),N($))this.blocks.push({type:"paragraph",inlines:$});return}if(J==="ul"||J==="ol"){this.flushParagraph(),this.blocks.push(...this.parseList(f,U));return}if(J==="table"){this.flushParagraph();let $=f.childElems().find((F)=>F.local==="caption");if($!==void 0){let F=this.inlineChildren($,U);if(N(F))this.blocks.push({type:"paragraph",inlines:F})}let Q=this.parseTable(f,U);if(Q!==void 0)this.blocks.push(Q);return}if(J==="blockquote"){this.flushParagraph();let $=this.subBlocks(f,U);if($.length>0)this.blocks.push({type:"blockQuote",blocks:$});return}if(J==="pre"){this.flushParagraph();let $=f.text();if(Jf($).length>0)this.blocks.push({type:"codeBlock",lang:void 0,text:$});return}if(J==="hr"){this.flushParagraph(),this.blocks.push({type:"rule"});return}if(qf(J)){if(this.pushAnchor(f),ef(f))this.flushParagraph(),this.walkChildren(f,U),this.flushParagraph();else this.walkChildren(f,U);return}if(J==="script"||J==="style"||J==="head"||J==="template"||J==="noscript")return;this.walkInline(f,U)}walkInline(f,U){this.pushAnchor(f);let z=f.local;if(z==="br"){this.inlines.push({type:"lineBreak"});return}if(z==="img"||z==="image"){let J=Uf(f.attrAny("alt")??""),$=f.attrAny("src")??f.attrAny("href")??"",Q=this.ctx.imageSource($);if(Q!==void 0||Jf(J).length>0)this.inlines.push({type:"image",alt:J,source:Q??{type:"unavailable"}});return}if(z==="a"){let J=f.attrAny("href"),$=J!==void 0?this.ctx.linkTarget(J):void 0,Q=this.inlineChildrenAt(f,U,T(this.inlines,this.startBoundary));if($!==void 0)this.inlines.push({type:"link",content:Q,target:$});else this.inlines.push(...Q);return}this.walkChildren(f,U)}inlineChildren(f,U){return this.inlineChildrenAt(f,U,!0)}inlineChildrenAt(f,U,z){let J=this.subBlocksAt(f,U,z);if(J.length===1&&J[0].type==="paragraph")return J[0].inlines;let $=[];for(let Q=0;Q<J.length;Q+=1){if(Q>0)$.push({type:"lineBreak"});let F=J[Q];if(F.type==="paragraph")$.push(...F.inlines);else if(F.type==="heading")$.push(...F.content);else $.push(pf(zf(g(F))))}return $}parseList(f,U){let z=f.local==="ol",J=f.childElems().filter((w)=>w.local==="li");if(J.length===0)return[];if(!z)return[{type:"list",list:{marker:"bullet",start:1,items:J.map((j)=>({blocks:this.subBlocks(j,U),checked:void 0,markerLabel:void 0}))}}];let $=f.attrAny("type"),Q="decimal";if($==="a")Q="lowerAlpha";else if($==="A")Q="upperAlpha";else if($==="i")Q="lowerRoman";else if($==="I")Q="upperRoman";let F=f.attrAny("reversed")!==void 0,Z=f.attrAny("start"),V=(Z!==void 0?Kf(Z):void 0)??(F?J.length:1),G=[];for(let w of J){let j=w.attrAny("value"),M=j!==void 0?Kf(j):void 0;if(M!==void 0)V=M;G.push(V),V=F?V-1:V+1}if(G.some((w)=>w<1)){let w=J.map((j,M)=>({blocks:this.subBlocks(j,U),checked:void 0,markerLabel:`${G[M]}.`}));return[{type:"list",list:{marker:Q,start:1,items:w}}]}let q=[],H,X=0;for(let w=0;w<J.length;w+=1){let j=G[w],M={blocks:this.subBlocks(J[w],U),checked:void 0,markerLabel:void 0};if(!(H!==void 0&&X+1===j)){if(H!==void 0)q.push({type:"list",list:H});H={marker:Q,start:j,items:[]}}H.items.push(M),X=j}if(H!==void 0)q.push({type:"list",list:H});return q}parseTable(f,U){let z=[],J=0,$=!1;for(let V of f.childElems())if(V.local==="thead"||V.local==="tbody"||V.local==="tfoot"){if($)$=!1,J+=1;let G=V.local==="thead";for(let q of V.childElems())if(q.local==="tr")z.push([q,G,J]);J+=1}else if(V.local==="tr")$=!0,z.push([V,!1,J]);if(z.length===0)return;let Q=new Map;for(let V=0;V<z.length;V+=1)Q.set(z[V][2],V);let F=new yf,Z=0;for(let V=0;V<z.length;V+=1){let[G,q,H]=z[V];F.nextRow();let X=!0,w=!1;for(let j of G.childElems()){if(j.local!=="td"&&j.local!=="th")continue;if(w=!0,j.local!=="th")X=!1;let M=Ff(E(j.attrAny("colspan")??"")??1,1,1000),C,b=j.attrAny("rowspan"),S=b!==void 0?E(b):void 0;if(S===0)C=(Q.get(H)??V)-V+1;else if(S!==void 0)C=Ff(S,1,65534);else C=1;F.place(df(this.subBlocks(j,U),M,C))}if(V===Z&&(q||X&&w))Z+=1}let K=F.finish("data");if(K.grid.length===0)return;return K.headerRows=rf(K,Z),{type:"table",table:K}}flushParagraph(){if(this.inlines.length>0){let f=this.inlines.splice(0,this.inlines.length);if(N(f))this.blocks.push({type:"paragraph",inlines:f})}this.startBoundary=!0}}function af(f,U){let z={...U};switch(f.local){case"b":case"strong":z.bold=!0;break;case"i":case"em":case"cite":case"dfn":case"var":z.italic=!0;break;case"s":case"del":case"strike":z.strike=!0;break;case"code":case"kbd":case"samp":case"tt":z.code=!0;break;default:break}return z}function g(f){switch(f.type){case"paragraph":return e(f.inlines);case"heading":return e(f.content);case"list":return f.list.items.flatMap((U)=>U.blocks.map(g)).join(" ");case"blockQuote":return f.blocks.map(g).join(" ");case"codeBlock":return f.text;case"table":return f.table.grid.flatMap((U)=>U.flatMap((z)=>z.type==="origin"?[z.cell.blocks.map(g).join(" ")]:[])).join(" ");case"rule":return""}}function qf(f){return f==="div"||f==="section"||f==="article"||f==="aside"||f==="main"||f==="nav"||f==="header"||f==="footer"||f==="figure"||f==="figcaption"||f==="center"||f==="details"||f==="summary"||f==="li"||f==="dl"||f==="dt"||f==="dd"||f==="body"}function sf(f){return qf(f)||f==="p"||f==="ul"||f==="ol"||f==="table"||f==="blockquote"||f==="pre"||f==="hr"||f==="h1"||f==="h2"||f==="h3"||f==="h4"||f==="h5"||f==="h6"}function ef(f){return f.childElems().some((U)=>sf(U.local))}function N(f){return!x(f)||f.some((U)=>U.type==="anchor")}function T(f,U){for(let z=f.length-1;z>=0;z-=1){let J=f[z];if(J.type==="anchor")continue;if(J.type==="text"){if(J.text.length===0)continue;return U0(f0(J.text))}if(J.type==="lineBreak")return!0;if(J.type==="link"){if(x(J.content))continue;return T(J.content,!1)}return!1}return U}function f0(f){let U=f.length;if(U===0)return 0;let z=f.charCodeAt(U-1);if(z>=56320&&z<=57343&&U>=2){let J=f.charCodeAt(U-2);if(J>=55296&&J<=56319)return(J-55296<<10)+(z-56320)+65536}return z}function U0(f){return f>=9&&f<=13||f===32||f===133||f===160||f===5760||f>=8192&&f<=8202||f===8232||f===8233||f===8239||f===8287||f===12288}function z0(f,U){let z=0;while(z<f.length&&f[z]===U)z+=1;return f.slice(z)}function Kf(f){let U=f.trim();if(!/^-?\d+$/.test(U))return;let z=Number(U);return Number.isFinite(z)?z:void 0}function Ff(f,U,z){return Math.min(z,Math.max(U,f))}function Lf(f){let U=q0(f),z=new Df;return If(U,new d(void 0,z),z)}function Yf(f){let U=K0(f),z=Q0(U)??$0(U);if(z===void 0)throw Z0.malformed("no text/html or text/plain part");let J=new Df,$=M0(U),Q=C0(z);if(z.contentType==="text/plain"){let Z=Mf(),K=Cf(Q);if(K.length>0)Z.blocks.push({type:"paragraph",inlines:[G0(K)]});return Z.assets=J.assets,Z}let F=gf(Q,new TextEncoder().encode(Q));return If(F,new d($,J),J)}function q0(f){return gf(L0(f),f)}function gf(f,U){if(t(f))try{return F0(U)}catch{}return R(f)}function If(f,U,z){Of(f);let J=H0(f),$=X0(f),Q=Mf();return Q.blocks.push(...h($,J,U)),Q.assets=z.assets,Q}function H0(f){let U=new I;for(let z of f.descendantElems())if(z.local==="style")U.add(z.text());return U}function X0(f){if(f.local==="body")return f;let U=f.local==="html"?f:o(f,"html");if(U!==void 0){let J=o(U,"body");if(J!==void 0)return J;return U}return o(f,"body")??f}function o(f,U){return f.childElems().find((z)=>z.local===U)}function Of(f){f.local=f.local.toLowerCase();let U=f.attrs;for(let z=0;z<U.length;z+=1)U[z].local=U[z].local.toLowerCase();for(let z of f.childElems())Of(z)}class d{cids;assets;constructor(f,U){this.cids=f;this.assets=U}linkTarget(f){if(f.length===0)return;if(f.startsWith("#")){let U=J0(f.slice(1));return U.length>0?{type:"anchor",id:U}:void 0}if(Hf(f))return{type:"external",url:f};return{type:"relative",url:f}}imageSource(f){if(f.length===0)return;let U=this.resolveCid(f);if(U!==void 0)return U;if(Hf(f))return{type:"external",url:f};return}anchorId(f){return f}resolveCid(f){if(this.cids===void 0)return;if(f.length<4||f.slice(0,4).toLowerCase()!=="cid:")return;let U=f.slice(4);try{U=decodeURIComponent(U)}catch{}if(U.startsWith("<")&&U.endsWith(">")&&U.length>=2)U=U.slice(1,-1);let z=this.cids.get(U.toLowerCase());if(z===void 0)return;let J=z.filename??U,$=z.contentType.length>0?z.contentType:j0(J);return{type:"asset",id:this.assets.add($,J,z.bytes)}}}function M0(f){let U=new Map;for(let z of V0(f)){let J=Xf(z,"content-id");if(J===void 0)continue;let $=D0(J);if($.length>0)U.set($.toLowerCase(),z)}return U}function D0(f){let U=Cf(f);if(U.startsWith("<")&&U.endsWith(">")&&U.length>=2)U=U.slice(1,-1);return U}function C0(f){let U=Xf(f,"content-type"),z="utf-8";if(U!==void 0){let J=/charset\s*=\s*("?)([^";\s]+)\1/i.exec(U);if(J!==null)z=J[2]}try{return w0(f.bytes,z)}catch{return new TextDecoder("utf-8",{fatal:!1}).decode(f.bytes)}}function L0(f){if(f.length>=2&&f[0]===255&&f[1]===254)return new TextDecoder("utf-16le").decode(f);if(f.length>=2&&f[0]===254&&f[1]===255)return new TextDecoder("utf-16be").decode(f);if(f.length>=3&&f[0]===239&&f[1]===187&&f[2]===191)return new TextDecoder("utf-8").decode(f.subarray(3));return new TextDecoder("utf-8").decode(f)}var g0=new Set(["htm","html","html4","html5","xhtml","mhtml","mht"]),Sf=new Set(["mhtml","mht"]);function n(){return{id:"html",sniff(f,U){if(Af(f))return 0;let z=U?.path!==void 0?_f(U.path):void 0;if(Bf(f)||z!==void 0&&Sf.has(z))return 3;if(_0(f))return 0;if(Pf(f)&&!y(f))return 3;if(W0(f)&&!y(f))return 2;if(z!==void 0&&g0.has(z))return 1;return 0},convert(f,U){I0(f);let z=U?.path!==void 0?_f(U.path):void 0;if(z!==void 0&&Sf.has(z)||Bf(f))return{markdown:Wf(Yf(f))};if(y(f))throw O.unsupported("OpenDocument");return{markdown:Wf(Lf(f))}}}}function I0(f){if(O0(f))throw O.unsupported("pdf");if(Y0(f))throw O.unsupported("ole");if(Af(f))throw O.unsupported("zip")}function Af(f){return f.length>=4&&f[0]===80&&f[1]===75&&f[2]===3&&f[3]===4}function O0(f){let U=c(f);return S0(f,"%PDF-",U)}function W0(f){if(Pf(f))return!0;return _(f.subarray(0,Math.min(f.length,4096))).includes("http://www.w3.org/1999/xhtml")}function Pf(f){let U=c(f);if(W(f,"<!doctype html",U)&&!r(f[U+14]))return!0;return W(f,"<html",U)&&!r(f[U+5])}function _0(f){let U=c(f);if(W(f,"<svg",U)&&!r(f[U+4]))return!0;if(!W(f,"<?xml",U))return!1;let z=_(f.subarray(0,Math.min(f.length,8192)));return/<svg\b/i.test(z)}function Bf(f){return _(f.subarray(0,Math.min(f.length,4096))).toLowerCase().includes("content-location:")}function y(f){let U=_(f.subarray(0,Math.min(f.length,8192))),z=p(U,0);if(U.startsWith("<?xml",z)){let $=U.indexOf("?>",z);if($<0)return!1;z=p(U,$+2)}while(U.startsWith("<!--",z)){let $=U.indexOf("-->",z+4);if($<0)return!1;z=p(U,$+3)}let J=U.slice(z,z+2048);return/<office:document\b/.test(J)}function c(f){let U=0;if(f.length>=3&&f[0]===239&&f[1]===187&&f[2]===191)U=3;while(U<f.length){let z=f[U];if(z!==9&&z!==10&&z!==13&&z!==32)break;U+=1}return U}function p(f,U){while(U<f.length){let z=f.charCodeAt(U);if(z!==9&&z!==10&&z!==13&&z!==32)break;U+=1}return U}function S0(f,U,z){if(z+U.length>f.length)return!1;for(let J=0;J<U.length;J+=1)if(f[z+J]!==U.charCodeAt(J))return!1;return!0}function W(f,U,z){if(z+U.length>f.length)return!1;for(let J=0;J<U.length;J+=1){let $=f[z+J],Q=U.charCodeAt(J);if($===Q)continue;let F=$>=65&&$<=90?$+32:$,Z=Q>=65&&Q<=90?Q+32:Q;if(F!==Z)return!1}return!0}function r(f){if(f===void 0)return!1;return f>=48&&f<=57||f>=65&&f<=90||f>=97&&f<=122||f===45||f===95||f===58}function _(f){return new TextDecoder("utf-8",{fatal:!1}).decode(f)}import{create as B0}from"@mdgate/core";var A0=B0([n()]);function P0(f,U){return A0(f,U)}export{P0 as toMarkdown,h as toBlocks,R as parseHtml,n as html,I as Stylesheet};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgate/html",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "mdgate HTML, XHTML, and MHTML converter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"prepublishOnly": "bun run build"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@mdgate/containers": "0.6.
|
|
26
|
-
"@mdgate/core": "0.6.
|
|
27
|
-
"@mdgate/document": "0.6.
|
|
28
|
-
"@mdgate/office-common": "0.6.
|
|
29
|
-
"@mdgate/utils": "0.6.
|
|
25
|
+
"@mdgate/containers": "0.6.2",
|
|
26
|
+
"@mdgate/core": "0.6.2",
|
|
27
|
+
"@mdgate/document": "0.6.2",
|
|
28
|
+
"@mdgate/office-common": "0.6.2",
|
|
29
|
+
"@mdgate/utils": "0.6.2"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|