@mdgate/html 0.5.2 → 0.5.3

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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { html } from './html.js';
2
+ export { parseHtml } from './internal/html-parse.js';
2
3
  export type { HtmlCtx, StyleProps } from './internal/walk.js';
3
4
  export { Stylesheet, toBlocks } from './internal/walk.js';
4
5
  export { toMarkdown } from './to-markdown.js';
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 Hf,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 Xf,trim as Cf}from"@mdgate/utils";import{Element as b}from"@mdgate/containers";var m=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 l(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(m.has(J.toLowerCase()))return!1;U.push(J)}return U.length===0}function t(f){let U=new b(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 b(void 0,Q,K.attrs);if(z[z.length-1].children.push({type:"elem",elem:V}),K.empty||m.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 T,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 x(f,U,z){let J=new h(U,z,!0);return J.walkChildren(f,Ff()),J.finish()}var Gf={bold:void 0,italic:void 0,strike:void 0,code:void 0},R={delta:Gf,hidden:void 0};function N(){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 H=$.slice(0,F);G=H.length===0?void 0:H.toLowerCase(),q=$.slice(F+1)}else G=$.toLowerCase();let X=(q!==void 0?10:0)+(G!==void 0?1:0);if(!Y(Q.normal))this.rules.push({tag:G,className:q,priority:X,props:Q.normal});if(!Y(Q.important))this.rules.push({tag:G,className:q,priority:jf+X,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:N(),important:N()};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=v(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 v(f){if(!/^\d+$/.test(f))return;let U=Number(f);if(!Number.isSafeInteger(U)||U<0||U>4294967295)return;return U}class h{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 h(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 R;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 R;if(Z.length===1)return Qf(R,Z[0][1]);Z.sort((K,V)=>K[0]-V[0]);let Q=N();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(E(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(!T(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)),u(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(u(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,E(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=[],X,H=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(!(X!==void 0&&H+1===j)){if(X!==void 0)q.push({type:"list",list:X});X={marker:Q,start:j,items:[]}}X.items.push(M),H=j}if(X!==void 0)q.push({type:"list",list:X});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,X]=z[F];K.nextRow();let H=!0,w=!1;for(let j of G.childElems()){if(j.local!=="td"&&j.local!=="th")continue;if(w=!0,j.local!=="th")H=!1;let M=Kf(v(j.attrAny("colspan")??"")??1,1,1000),C,c=j.attrAny("rowspan"),S=c!==void 0?v(c):void 0;if(S===0)C=(Q.get(X)??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||H&&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(u(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 u(f){return!T(f)||f.some((U)=>U.type==="anchor")}function E(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(T(J.content))continue;return E(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 o(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=H0(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 o(Z,J),J)}function w0(f){return gf(C0(f),f)}function gf(f,U){if(l(f))try{return $0(U)}catch{}return t(f)}function If(f,U,z){Of(f);let J=q0(f),Z=X0(f),Q=Mf();return Q.blocks.push(...x(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 X0(f){if(f.local==="body")return f;let U=f.local==="html"?f:k(f,"html");if(U!==void 0){let J=k(U,"body");if(J!==void 0)return J;return U}return k(f,"body")??f}function k(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 o{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(Xf(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(Xf(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 H0(f){let U=new Map;for(let z of K0(f)){let J=Hf(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=Hf(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","xhtml","mhtml","mht"]),Sf=new Set(["mhtml","mht"]);function r(){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)&&!d(f))return 3;if(O0(f)&&!d(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(d(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=n(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=n(f);if(W(f,"<!doctype html",U)&&!p(f[U+14]))return!0;return W(f,"<html",U)&&!p(f[U+5])}function W0(f){let U=n(f);if(W(f,"<svg",U)&&!p(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 d(f){let U=_(f.subarray(0,Math.min(f.length,8192))),z=y(U,0);if(U.startsWith("<?xml",z)){let Z=U.indexOf("?>",z);if(Z<0)return!1;z=y(U,Z+2)}while(U.startsWith("<!--",z)){let Z=U.indexOf("-->",z+4);if(Z<0)return!1;z=y(U,Z+3)}let J=U.slice(z,z+2048);return/<office:document\b/.test(J)}function n(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 y(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 p(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([r()]);function A0(f,U){return B0(f,U)}export{A0 as toMarkdown,x as toBlocks,r as html,I as Stylesheet};
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","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};
@@ -0,0 +1,5 @@
1
+ import { Element } from '@mdgate/containers';
2
+ /** True when markup is well-formed XML (quoted attrs, matched tags, self-closed voids). */
3
+ export declare function isWellFormedXmlMarkup(s: string): boolean;
4
+ /** Tolerant HTML5-ish parse into the same `Element` tree the EPUB walker expects. */
5
+ export declare function parseHtml(s: string): Element;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/html",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
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.5.2",
26
- "@mdgate/core": "0.5.2",
27
- "@mdgate/document": "0.5.2",
28
- "@mdgate/office-common": "0.5.2",
29
- "@mdgate/utils": "0.5.2"
25
+ "@mdgate/containers": "0.5.3",
26
+ "@mdgate/core": "0.5.3",
27
+ "@mdgate/document": "0.5.3",
28
+ "@mdgate/office-common": "0.5.3",
29
+ "@mdgate/utils": "0.5.3"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"