@mdgate/html 0.4.1 → 0.5.1
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 D0}from"@mdgate/containers";import{ConvertError as I}from"@mdgate/core";import{documentToMarkdown as Of}from"@mdgate/document";import{fileExtension as Sf}from"@mdgate/utils";import{decodeFragment as J0,mimeHeader as Hf,mimeTextHtml as Q0,mimeTextPlain as Z0,parseMime as $0,parseXml as F0,walkMimeParts as K0}from"@mdgate/containers";import{ConvertError as Lf}from"@mdgate/core";import{emptyDocument as gf,plain as V0}from"@mdgate/document";import{AssetSink as Cf,mediaTypeFor as G0}from"@mdgate/office-common";import{decode as j0,isAbsoluteUri as qf,trim as Yf}from"@mdgate/utils";import{Element as p,MAX_XML_DEPTH as r,MAX_XML_NODES as n}from"@mdgate/containers";import{ConvertError as b}from"@mdgate/core";var c=new Set(["area","base","basefont","bgsound","br","col","embed","frame","hr","img","input","keygen","link","meta","param","source","track","wbr"]),Af=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 m(f){let U=[],z=0;while(z<f.length){if(f.charCodeAt(z)!==60){z+=1;continue}if(f.startsWith("<!--",z)){let Z=f.indexOf("-->",z+4);if(Z<0)return!1;z=Z+3;continue}if(f.startsWith("<![CDATA[",z)){let Z=f.indexOf("]]>",z+9);if(Z<0)return!1;z=Z+3;continue}if(f.startsWith("<?",z)){let Z=f.indexOf("?>",z+2);if(Z<0)return!1;z=Z+2;continue}if(f.startsWith("<!",z)){let Z=a(f,z);if(Z<0)return!1;z=Z;continue}if(f.startsWith("</",z)){z+=2;let Z=S(f,z);if(Z.length===0)return!1;if(z+=Z.length,z=C(f,z),f[z]!==">")return!1;z+=1;let $=U.pop();if($===void 0||$!==Z)return!1;continue}z+=1;let J=S(f,z);if(J.length===0)return!1;z+=J.length;let Q=xf(f,z);if(Q===void 0)return!1;if(z=Q.next,Q.empty)continue;if(c.has(J.toLowerCase()))return!1;U.push(J)}return U.length===0}function l(f){let U=new p(void 0,""),z=[U],J=0,Q=0,Z=()=>{if(J+=1,J>n)throw b.resourceLimit("max_xml_nodes",`part exceeds ${n} xml nodes`)};while(Q<f.length){if(f.charCodeAt(Q)!==60){let G=Q,j=f.indexOf("<",Q);Q=j<0?f.length:j;let H=R(f.slice(G,Q));if(H.length>0)Z(),D(z[z.length-1],H);continue}if(f.startsWith("<!--",Q)){let G=f.indexOf("-->",Q+4);Q=G<0?f.length:G+3;continue}if(f.startsWith("<![CDATA[",Q)){let G=f.indexOf("]]>",Q+9),j=G<0?f.slice(Q+9):f.slice(Q+9,G);if(j.length>0)Z(),D(z[z.length-1],j);Q=G<0?f.length:G+3;continue}if(f.startsWith("<?",Q)){let G=f.indexOf("?>",Q+2);Q=G<0?f.length:G+2;continue}if(f.startsWith("<!",Q)){let G=a(f,Q);Q=G<0?f.length:G;continue}if(f.startsWith("</",Q)){Q+=2;let G=B(f,Q).toLowerCase();if(G.length===0){Z(),D(z[z.length-1],"</");continue}if(Q+=G.length,Q=C(f,Q),f[Q]===">")Q+=1;vf(z,G);continue}if(!t(f.charCodeAt(Q+1))){Z(),D(z[z.length-1],"<"),Q+=1;continue}Q+=1;let $=B(f,Q),F=$.toLowerCase();Q+=$.length;let K=Ef(f,Q);Q=K.next,Nf(z,F),Z();let V=new p(void 0,F,K.attrs);if(z[z.length-1].children.push({type:"elem",elem:V}),K.empty||c.has(F))continue;if(Af.has(F)){let G=hf(f,Q,F),j=G<0?f.slice(Q):f.slice(Q,G);if(j.length>0)Z(),V.children.push({type:"text",text:j});if(G<0)Q=f.length;else Q=f.indexOf(">",G),Q=Q<0?f.length:Q+1;continue}if(z.length>=r)throw b.resourceLimit("max_xml_depth",`element nesting exceeds ${r}`);z.push(V)}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 D(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 S(f,U){let z=U;while(U<f.length&&i(f.charCodeAt(U)))U+=1;return f.slice(z,U)}function B(f,U){let z=U;while(U<f.length&&Tf(f.charCodeAt(U)))U+=1;return f.slice(z,U)}function t(f){return f>=65&&f<=90||f>=97&&f<=122||f===58||f===95}function i(f){return t(f)||f>=48&&f<=57||f===45||f===46}function Tf(f){return i(f)}function xf(f,U){for(;;){U=C(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=S(f,U);if(J.length===0)return;if(U+=J.length,U=C(f,U),f[U]!=="=")return;U+=1,U=C(f,U);let Q=f.charCodeAt(U);if(Q!==34&&Q!==39)return;U+=1;let Z=f.indexOf(String.fromCharCode(Q),U);if(Z<0)return;U=Z+1}}function Ef(f,U){let z=[];for(;;){U=C(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 Q=B(f,U);if(Q.length===0){U+=1;continue}U+=Q.length,U=C(f,U);let Z="";if(f[U]==="="){U+=1,U=C(f,U);let $=f.charCodeAt(U);if($===34||$===39){U+=1;let F=f.indexOf(String.fromCharCode($),U),K=F<0?f.slice(U):f.slice(U,F);U=F<0?f.length:F+1,Z=R(K)}else{let F=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}Z=R(f.slice(F,U))}}z.push({ns:void 0,local:Q.toLowerCase(),value:Z})}}function hf(f,U,z){let J=`</${z}`,Q=U;while(Q<f.length){let Z=kf(f,J,Q);if(Z<0)return-1;let $=Z+J.length,F=f.charCodeAt($);if(F===62||F===32||F===9||F===10||F===13||Number.isNaN(F))return Z;Q=Z+1}return-1}function kf(f,U,z){let J=U.length,Q=f.length-J;for(let Z=z;Z<=Q;Z+=1){let $=!0;for(let F=0;F<J;F+=1){let K=f.charCodeAt(Z+F),V=U.charCodeAt(F);if(K===V)continue;let G=K>=65&&K<=90?K+32:K,j=V>=65&&V<=90?V+32:V;if(G!==j){$=!1;break}}if($)return Z}return-1}function a(f,U){U+=2;let z=0;while(U<f.length){let J=f[U];if(J==='"'||J==="'"){let Q=J;U+=1;while(U<f.length&&f[U]!==Q)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 C(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 R(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 Q=f.indexOf(";",J+1);if(Q<0||Q-J>32){U+="&",z=J+1;continue}let Z=yf(f.slice(J+1,Q));if(Z!==void 0)U+=Z,z=Q+1;else U+="&",z=J+1}return U}function yf(f){if(f.startsWith("#")){let z=f.slice(1),J=z.startsWith("x")||z.startsWith("X"),Q=J?z.slice(1):z,Z=Number.parseInt(Q,J?16:10);if(!Number.isFinite(Z))return;try{return String.fromCodePoint(Z)}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 v,inlinesToPlainText as s,plain as pf,resolveHeaderRows as rf}from"@mdgate/document";import{deltasEqual as nf,emptyDelta as Ff,mergeDelta as Kf,rebaseEmphasis as bf,resolveDelta as e}from"@mdgate/office-common";import{cleanText as ff,collapseWs as Uf,trim as zf}from"@mdgate/utils";function T(f,U,z){let J=new x(U,z,!0);return J.walkChildren(f,Ff()),J.finish()}var Vf={bold:void 0,italic:void 0,strike:void 0,code:void 0},_={delta:Vf,hidden:void 0};function A(){return{delta:Ff(),hidden:void 0}}function Jf(f,U){return{delta:Kf(f.delta,U.delta),hidden:U.hidden??f.hidden}}function M(f){return nf(f.delta,Vf)&&f.hidden===void 0}var Qf=1e5,Gf=1e6,cf=[],mf=[];class X{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 Q=z.slice(J+1);if(!lf(Q))continue;let Z=jf(Q);if(M(Z.normal)&&M(Z.important))continue;let $=z.slice(0,J);for(let F of $.split(",")){let K=F.trim();if(K.length===0||K.includes(" ")||K.includes(":")||K.includes("["))continue;let V=K.indexOf("."),G,j;if(V>=0){let L=K.slice(0,V);G=L.length===0?void 0:L.toLowerCase(),j=K.slice(V+1)}else G=K.toLowerCase();let H=(j!==void 0?10:0)+(G!==void 0?1:0);if(!M(Z.normal))this.rules.push({tag:G,className:j,priority:H,props:Z.normal});if(!M(Z.important))this.rules.push({tag:G,className:j,priority:Gf+H,props:Z.important})}}}matchingRules(f,U){let z=this.rules;if(z.length===0)return cf;let J=[];for(let Q=0;Q<z.length;Q+=1){let Z=z[Q];if(Z.tag!==void 0&&Z.tag!==f)continue;if(Z.className!==void 0&&!U.includes(Z.className))continue;J.push([Z.priority,Z.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,Q=U;while(Q>=0){if(Q>J)z.push(f.slice(J,Q));let Z=f.indexOf("*/",Q+2);if(Z<0)return z.join("");J=Z+2,Q=f.indexOf("/*",J)}if(J<f.length)z.push(f.slice(J));return z.join("")}function jf(f){let U={normal:A(),important:A()};for(let z of f.split(";")){let J=z.indexOf(":");if(J<0)continue;let Q=z.slice(0,J).trim().toLowerCase(),Z=z.slice(J+1).trim().toLowerCase(),$=!1,F=Z.indexOf("!");if(F>=0){if(Z.slice(F+1).trim()==="important")$=!0;Z=Z.slice(0,F).trimEnd()}let K=$?U.important:U.normal;switch(Q){case"font-weight":{let V=u(Z);K.delta.bold=Z==="bold"||Z==="bolder"||V!==void 0&&V>=600;break}case"font-style":K.delta.italic=Z==="italic"||Z==="oblique";break;case"text-decoration":case"text-decoration-line":if(Z.includes("line-through"))K.delta.strike=!0;else if(Z==="none")K.delta.strike=!1;break;case"display":K.hidden=Z==="none";break;default:break}}return U}function u(f){if(!/^\d+$/.test(f))return;let U=Number(f);if(!Number.isSafeInteger(U)||U<0||U>4294967295)return;return U}class x{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 x(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 _;let z=f.attrAny("class"),J=z!==void 0&&z.length>0?z.split(/\s+/).filter(($)=>$.length>0):mf,Q=this.css.matchingRules(f.local,J);if(U!==void 0){let $=jf(U);Q.push([Qf,$.normal]),Q.push([Gf+Qf,$.important])}if(Q.length===0)return _;if(Q.length===1)return Jf(_,Q[0][1]);Q.sort(($,F)=>$[0]-F[0]);let Z=A();for(let $=0;$<Q.length;$+=1)Z=Jf(Z,Q[$][1]);return Z}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=Uf(ff(f));if(z.length===0)return;let J=z;if(N(this.inlines,this.startBoundary))J=z0(z," ");if(J.length===0)return;this.inlines.push({type:"text",text:J,style:e(U)})}walkElem(f,U){let z=this.elementProps(f);if(z.hidden===!0)return;U=Kf(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 Q=Number.parseInt(J.slice(1),10)||1,Z=this.inlineChildren(f,U);bf(Z,e(U));let $=f.attrAny("id"),F=$!==void 0?this.ctx.anchorId($):void 0;if(!v(Z))this.blocks.push({type:"heading",level:Q,anchor:F,content:Z});else{let K=[];if(F!==void 0)K.push({type:"anchor",id:F});for(let V of Z)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 Q=this.inlines.splice(0,this.inlines.length);if(Q.push(...this.inlineChildren(f,U)),P(Q))this.blocks.push({type:"paragraph",inlines:Q});return}if(J==="ul"||J==="ol"){this.flushParagraph(),this.blocks.push(...this.parseList(f,U));return}if(J==="table"){this.flushParagraph();let Q=f.childElems().find(($)=>$.local==="caption");if(Q!==void 0){let $=this.inlineChildren(Q,U);if(P($))this.blocks.push({type:"paragraph",inlines:$})}let Z=this.parseTable(f,U);if(Z!==void 0)this.blocks.push(Z);return}if(J==="blockquote"){this.flushParagraph();let Q=this.subBlocks(f,U);if(Q.length>0)this.blocks.push({type:"blockQuote",blocks:Q});return}if(J==="pre"){this.flushParagraph();let Q=f.text();if(zf(Q).length>0)this.blocks.push({type:"codeBlock",lang:void 0,text:Q});return}if(J==="hr"){this.flushParagraph(),this.blocks.push({type:"rule"});return}if(wf(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=ff(f.attrAny("alt")??""),Q=f.attrAny("src")??f.attrAny("href")??"",Z=this.ctx.imageSource(Q);if(Z!==void 0||zf(J).length>0)this.inlines.push({type:"image",alt:J,source:Z??{type:"unavailable"}});return}if(z==="a"){let J=f.attrAny("href"),Q=J!==void 0?this.ctx.linkTarget(J):void 0,Z=this.inlineChildrenAt(f,U,N(this.inlines,this.startBoundary));if(Q!==void 0)this.inlines.push({type:"link",content:Z,target:Q});else this.inlines.push(...Z);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 Q=[];for(let Z=0;Z<J.length;Z+=1){if(Z>0)Q.push({type:"lineBreak"});let $=J[Z];if($.type==="paragraph")Q.push(...$.inlines);else if($.type==="heading")Q.push(...$.content);else Q.push(pf(Uf(W($))))}return Q}parseList(f,U){let z=f.local==="ol",J=f.childElems().filter((q)=>q.local==="li");if(J.length===0)return[];if(!z)return[{type:"list",list:{marker:"bullet",start:1,items:J.map((w)=>({blocks:this.subBlocks(w,U),checked:void 0,markerLabel:void 0}))}}];let Q=f.attrAny("type"),Z="decimal";if(Q==="a")Z="lowerAlpha";else if(Q==="A")Z="upperAlpha";else if(Q==="i")Z="lowerRoman";else if(Q==="I")Z="upperRoman";let $=f.attrAny("reversed")!==void 0,F=f.attrAny("start"),V=(F!==void 0?Zf(F):void 0)??($?J.length:1),G=[];for(let q of J){let w=q.attrAny("value"),g=w!==void 0?Zf(w):void 0;if(g!==void 0)V=g;G.push(V),V=$?V-1:V+1}if(G.some((q)=>q<1)){let q=J.map((w,g)=>({blocks:this.subBlocks(w,U),checked:void 0,markerLabel:`${G[g]}.`}));return[{type:"list",list:{marker:Z,start:1,items:q}}]}let j=[],H,L=0;for(let q=0;q<J.length;q+=1){let w=G[q],g={blocks:this.subBlocks(J[q],U),checked:void 0,markerLabel:void 0};if(!(H!==void 0&&L+1===w)){if(H!==void 0)j.push({type:"list",list:H});H={marker:Z,start:w,items:[]}}H.items.push(g),L=w}if(H!==void 0)j.push({type:"list",list:H});return j}parseTable(f,U){let z=[],J=0,Q=!1;for(let V of f.childElems())if(V.local==="thead"||V.local==="tbody"||V.local==="tfoot"){if(Q)Q=!1,J+=1;let G=V.local==="thead";for(let j of V.childElems())if(j.local==="tr")z.push([j,G,J]);J+=1}else if(V.local==="tr")Q=!0,z.push([V,!1,J]);if(z.length===0)return;let Z=new Map;for(let V=0;V<z.length;V+=1)Z.set(z[V][2],V);let $=new df,F=0;for(let V=0;V<z.length;V+=1){let[G,j,H]=z[V];$.nextRow();let L=!0,q=!1;for(let w of G.childElems()){if(w.local!=="td"&&w.local!=="th")continue;if(q=!0,w.local!=="th")L=!1;let g=$f(u(w.attrAny("colspan")??"")??1,1,1000),Y,d=w.attrAny("rowspan"),O=d!==void 0?u(d):void 0;if(O===0)Y=(Z.get(H)??V)-V+1;else if(O!==void 0)Y=$f(O,1,65534);else Y=1;$.place(of(this.subBlocks(w,U),g,Y))}if(V===F&&(j||L&&q))F+=1}let K=$.finish("data");if(K.grid.length===0)return;return K.headerRows=rf(K,F),{type:"table",table:K}}flushParagraph(){if(this.inlines.length>0){let f=this.inlines.splice(0,this.inlines.length);if(P(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 W(f){switch(f.type){case"paragraph":return s(f.inlines);case"heading":return s(f.content);case"list":return f.list.items.flatMap((U)=>U.blocks.map(W)).join(" ");case"blockQuote":return f.blocks.map(W).join(" ");case"codeBlock":return f.text;case"table":return f.table.grid.flatMap((U)=>U.flatMap((z)=>z.type==="origin"?[z.cell.blocks.map(W).join(" ")]:[])).join(" ");case"rule":return""}}function wf(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 wf(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 P(f){return!v(f)||f.some((U)=>U.type==="anchor")}function N(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(v(J.content))continue;return N(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 Zf(f){let U=f.trim();if(!/^-?\d+$/.test(U))return;let z=Number(U);return Number.isFinite(z)?z:void 0}function $f(f,U,z){return Math.min(z,Math.max(U,f))}function Df(f){let U=w0(f),z=new Cf;return Xf(U,new h(void 0,z),z)}function Mf(f){let U=$0(f),z=Q0(U)??Z0(U);if(z===void 0)throw Lf.malformed("no text/html or text/plain part");let J=new Cf,Q=L0(U),Z=C0(z);if(z.contentType==="text/plain"){let F=gf(),K=Yf(Z);if(K.length>0)F.blocks.push({type:"paragraph",inlines:[V0(K)]});return F.assets=J.assets,F}let $=Wf(Z,new TextEncoder().encode(Z));return Xf($,new h(Q,J),J)}function w0(f){return Wf(Y0(f),f)}function Wf(f,U){if(m(f))try{return F0(U)}catch(z){if(z instanceof Lf&&z.code==="resourceLimit")throw z}return l(f)}function Xf(f,U,z){If(f);let J=q0(f),Q=H0(f),Z=gf();return Z.blocks.push(...T(Q,J,U)),Z.assets=z.assets,Z}function q0(f){let U=new X;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:E(f,"html");if(U!==void 0){let J=E(U,"body");if(J!==void 0)return J;return U}return E(f,"body")??f}function E(f,U){return f.childElems().find((z)=>z.local===U)}function If(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())If(z)}class h{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(qf(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(qf(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,Q=z.contentType.length>0?z.contentType:G0(J);return{type:"asset",id:this.assets.add(Q,J,z.bytes)}}}function L0(f){let U=new Map;for(let z of K0(f)){let J=Hf(z,"content-id");if(J===void 0)continue;let Q=g0(J);if(Q.length>0)U.set(Q.toLowerCase(),z)}return U}function g0(f){let U=Yf(f);if(U.startsWith("<")&&U.endsWith(">")&&U.length>=2)U=U.slice(1,-1);return U}function C0(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 Y0(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 M0=new Set(["htm","html","xhtml","mhtml","mht"]),W0=new Set(["mhtml","mht"]);function y(){return{id:"html",sniff(f,U){if(O0(f)&&!Bf(f))return 2;if(U?.path!==void 0&&M0.has(Sf(U.path)??""))return 1;return 0},convert(f,U){X0(f);let z=U?.path!==void 0?Sf(U.path):void 0;if(z!==void 0&&W0.has(z)||S0(f))return{markdown:Of(Mf(f))};if(Bf(f))throw I.unsupported("OpenDocument");return{markdown:Of(Df(f))}}}}function X0(f){if(I0(f))throw I.unsupported("pdf");if(D0(f))throw I.unsupported("ole");if(f.length>=4&&f[0]===80&&f[1]===75&&f[2]===3&&f[3]===4)throw I.unsupported("zip")}function I0(f){let U=Pf(f);return B0(f,"%PDF-",U)}function O0(f){let U=Pf(f);if(Rf(f,"<!doctype html",U)&&!_f(f[U+14]))return!0;if(Rf(f,"<html",U)&&!_f(f[U+5]))return!0;return o(f.subarray(0,Math.min(f.length,4096))).includes("http://www.w3.org/1999/xhtml")}function S0(f){let U=o(f.subarray(0,Math.min(f.length,512))).toLowerCase();return U.includes("multipart/related")||U.includes("content-location:")}function Bf(f){let U=o(f.subarray(0,Math.min(f.length,8192))),z=k(U,0);if(U.startsWith("<?xml",z)){let Q=U.indexOf("?>",z);if(Q<0)return!1;z=k(U,Q+2)}while(U.startsWith("<!--",z)){let Q=U.indexOf("-->",z+4);if(Q<0)return!1;z=k(U,Q+3)}let J=U.slice(z,z+2048);return/<office:document\b/.test(J)}function Pf(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 k(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 B0(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 Rf(f,U,z){if(z+U.length>f.length)return!1;for(let J=0;J<U.length;J+=1){let Q=f[z+J],Z=U.charCodeAt(J);if(Q===Z)continue;let $=Q>=65&&Q<=90?Q+32:Q,F=Z>=65&&Z<=90?Z+32:Z;if($!==F)return!1}return!0}function _f(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 o(f){return new TextDecoder("utf-8",{fatal:!1}).decode(f)}import{create as R0}from"@mdgate/core";var _0=R0([y()]);function P0(f,U){return _0(f,U)}export{P0 as toMarkdown,T as toBlocks,y as html,X as Stylesheet};
|
|
1
|
+
import{hasOleMagic as X0}from"@mdgate/containers";import{ConvertError as O}from"@mdgate/core";import{documentToMarkdown as Df}from"@mdgate/document";import{fileExtension as Yf}from"@mdgate/utils";import{decodeFragment as ef,mimeHeader as jf,mimeTextHtml as f0,mimeTextPlain as U0,parseMime as z0,parseXml as J0,walkMimeParts as Q0}from"@mdgate/containers";import{ConvertError as Z0}from"@mdgate/core";import{emptyDocument as Hf,plain as $0}from"@mdgate/document";import{AssetSink as Mf,mediaTypeFor as F0}from"@mdgate/office-common";import{decode as K0,isAbsoluteUri as Gf,trim as wf}from"@mdgate/utils";import{Element as p}from"@mdgate/containers";var r=new Set(["area","base","basefont","bgsound","br","col","embed","frame","hr","img","input","keygen","link","meta","param","source","track","wbr"]),_f=new Set(["script","style","textarea","title","noscript"]),Bf=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 n(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=l(f,z);if(Q<0)return!1;z=Q;continue}if(f.startsWith("</",z)){z+=2;let Q=I(f,z);if(Q.length===0)return!1;if(z+=Q.length,z=q(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=I(f,z);if(J.length===0)return!1;z+=J.length;let Z=uf(f,z);if(Z===void 0)return!1;if(z=Z.next,Z.empty)continue;if(r.has(J.toLowerCase()))return!1;U.push(J)}return U.length===0}function b(f){let U=new p(void 0,""),z=[U],J=0;while(J<f.length){if(f.charCodeAt(J)!==60){let $=J,K=f.indexOf("<",J);J=K<0?f.length:K;let G=B(f.slice($,J));if(G.length>0)g(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),K=$<0?f.slice(J+9):f.slice(J+9,$);if(K.length>0)g(z[z.length-1],K);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 $=l(f,J);J=$<0?f.length:$;continue}if(f.startsWith("</",J)){J+=2;let $=_(f,J).toLowerCase();if($.length===0){g(z[z.length-1],"</");continue}if(J+=$.length,J=q(f,J),f[J]===">")J+=1;Pf(z,$);continue}if(!c(f.charCodeAt(J+1))){g(z[z.length-1],"<"),J+=1;continue}J+=1;let Z=_(f,J),Q=Z.toLowerCase();J+=Z.length;let F=Nf(f,J);J=F.next,Af(z,Q);let V=new p(void 0,Q,F.attrs);if(z[z.length-1].children.push({type:"elem",elem:V}),F.empty||r.has(Q))continue;if(_f.has(Q)){let $=vf(f,J,Q),K=$<0?f.slice(J):f.slice(J,$);if(K.length>0)V.children.push({type:"text",text:K});if($<0)J=f.length;else J=f.indexOf(">",$),J=J<0?f.length:J+1;continue}z.push(V)}return U}function Af(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(Bf.has(U)&&J==="p")f.pop()}function Pf(f,U){for(let z=f.length-1;z>=1;z-=1)if(f[z].local===U){f.length=z;return}}function g(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 I(f,U){let z=U;while(U<f.length&&m(f.charCodeAt(U)))U+=1;return f.slice(z,U)}function _(f,U){let z=U;while(U<f.length&&Rf(f.charCodeAt(U)))U+=1;return f.slice(z,U)}function c(f){return f>=65&&f<=90||f>=97&&f<=122||f===58||f===95}function m(f){return c(f)||f>=48&&f<=57||f===45||f===46}function Rf(f){return m(f)}function uf(f,U){for(;;){U=q(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=I(f,U);if(J.length===0)return;if(U+=J.length,U=q(f,U),f[U]!=="=")return;U+=1,U=q(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 Nf(f,U){let z=[];for(;;){U=q(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=_(f,U);if(Z.length===0){U+=1;continue}U+=Z.length,U=q(f,U);let Q="";if(f[U]==="="){U+=1,U=q(f,U);let F=f.charCodeAt(U);if(F===34||F===39){U+=1;let V=f.indexOf(String.fromCharCode(F),U),$=V<0?f.slice(U):f.slice(U,V);U=V<0?f.length:V+1,Q=B($)}else{let V=U;while(U<f.length){let $=f.charCodeAt(U);if($===62||$===47||$===32||$===9||$===10||$===13)break;U+=1}Q=B(f.slice(V,U))}}z.push({ns:void 0,local:Z.toLowerCase(),value:Q})}}function vf(f,U,z){let J=`</${z}`,Z=U;while(Z<f.length){let Q=Ef(f,J,Z);if(Q<0)return-1;let F=Q+J.length,V=f.charCodeAt(F);if(V===62||V===32||V===9||V===10||V===13||Number.isNaN(V))return Q;Z=Q+1}return-1}function Ef(f,U,z){let J=U.length,Z=f.length-J;for(let Q=z;Q<=Z;Q+=1){let F=!0;for(let V=0;V<J;V+=1){let $=f.charCodeAt(Q+V),K=U.charCodeAt(V);if($===K)continue;let G=$>=65&&$<=90?$+32:$,M=K>=65&&K<=90?K+32:K;if(G!==M){F=!1;break}}if(F)return Q}return-1}function l(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 q(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 B(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=Tf(f.slice(J+1,Z));if(Q!==void 0)U+=Q,z=Z+1;else U+="&",z=J+1}return U}function Tf(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 xf,GridBuilder as hf,inlinesAreEmpty as v,inlinesToPlainText as t,plain as kf,resolveHeaderRows as of}from"@mdgate/document";import{deltasEqual as yf,emptyDelta as Qf,mergeDelta as Zf,rebaseEmphasis as df,resolveDelta as i}from"@mdgate/office-common";import{cleanText as a,collapseWs as s,trim as e}from"@mdgate/utils";function E(f,U,z){let J=new T(U,z,!0);return J.walkChildren(f,Qf()),J.finish()}var $f={bold:void 0,italic:void 0,strike:void 0,code:void 0},A={delta:$f,hidden:void 0};function R(){return{delta:Qf(),hidden:void 0}}function ff(f,U){return{delta:Zf(f.delta,U.delta),hidden:U.hidden??f.hidden}}function D(f){return yf(f.delta,$f)&&f.hidden===void 0}var Uf=1e5,Ff=1e6,pf=[],rf=[];class W{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=bf(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(!nf(Z))continue;let Q=Kf(Z);if(D(Q.normal)&&D(Q.important))continue;let F=z.slice(0,J);for(let V of F.split(",")){let $=V.trim();if($.length===0||$.includes(" ")||$.includes(":")||$.includes("["))continue;let K=$.indexOf("."),G,M;if(K>=0){let L=$.slice(0,K);G=L.length===0?void 0:L.toLowerCase(),M=$.slice(K+1)}else G=$.toLowerCase();let w=(M!==void 0?10:0)+(G!==void 0?1:0);if(!D(Q.normal))this.rules.push({tag:G,className:M,priority:w,props:Q.normal});if(!D(Q.important))this.rules.push({tag:G,className:M,priority:Ff+w,props:Q.important})}}}matchingRules(f,U){let z=this.rules;if(z.length===0)return pf;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 nf(f){return f.includes("display")||f.includes("font-weight")||f.includes("font-style")||f.includes("text-decoration")}function bf(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 Kf(f){let U={normal:R(),important:R()};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(),F=!1,V=Q.indexOf("!");if(V>=0){if(Q.slice(V+1).trim()==="important")F=!0;Q=Q.slice(0,V).trimEnd()}let $=F?U.important:U.normal;switch(Z){case"font-weight":{let K=u(Q);$.delta.bold=Q==="bold"||Q==="bolder"||K!==void 0&&K>=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 u(f){if(!/^\d+$/.test(f))return;let U=Number(f);if(!Number.isSafeInteger(U)||U<0||U>4294967295)return;return U}class T{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 T(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 A;let z=f.attrAny("class"),J=z!==void 0&&z.length>0?z.split(/\s+/).filter((F)=>F.length>0):rf,Z=this.css.matchingRules(f.local,J);if(U!==void 0){let F=Kf(U);Z.push([Uf,F.normal]),Z.push([Ff+Uf,F.important])}if(Z.length===0)return A;if(Z.length===1)return ff(A,Z[0][1]);Z.sort((F,V)=>F[0]-V[0]);let Q=R();for(let F=0;F<Z.length;F+=1)Q=ff(Q,Z[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=s(a(f));if(z.length===0)return;let J=z;if(N(this.inlines,this.startBoundary))J=sf(z," ");if(J.length===0)return;this.inlines.push({type:"text",text:J,style:i(U)})}walkElem(f,U){let z=this.elementProps(f);if(z.hidden===!0)return;U=Zf(cf(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);df(Q,i(U));let F=f.attrAny("id"),V=F!==void 0?this.ctx.anchorId(F):void 0;if(!v(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 K of Q)if(K.type==="anchor")$.push(K);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)),P(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((F)=>F.local==="caption");if(Z!==void 0){let F=this.inlineChildren(Z,U);if(P(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 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(e(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(Vf(J)){if(this.pushAnchor(f),lf(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=a(f.attrAny("alt")??""),Z=f.attrAny("src")??f.attrAny("href")??"",Q=this.ctx.imageSource(Z);if(Q!==void 0||e(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,N(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 F=J[Q];if(F.type==="paragraph")Z.push(...F.inlines);else if(F.type==="heading")Z.push(...F.content);else Z.push(kf(s(Y(F))))}return Z}parseList(f,U){let z=f.local==="ol",J=f.childElems().filter((H)=>H.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 F=f.attrAny("reversed")!==void 0,V=f.attrAny("start"),K=(V!==void 0?zf(V):void 0)??(F?J.length:1),G=[];for(let H of J){let j=H.attrAny("value"),X=j!==void 0?zf(j):void 0;if(X!==void 0)K=X;G.push(K),K=F?K-1:K+1}if(G.some((H)=>H<1)){let H=J.map((j,X)=>({blocks:this.subBlocks(j,U),checked:void 0,markerLabel:`${G[X]}.`}));return[{type:"list",list:{marker:Q,start:1,items:H}}]}let M=[],w,L=0;for(let H=0;H<J.length;H+=1){let j=G[H],X={blocks:this.subBlocks(J[H],U),checked:void 0,markerLabel:void 0};if(!(w!==void 0&&L+1===j)){if(w!==void 0)M.push({type:"list",list:w});w={marker:Q,start:j,items:[]}}w.items.push(X),L=j}if(w!==void 0)M.push({type:"list",list:w});return M}parseTable(f,U){let z=[],J=0,Z=!1;for(let K of f.childElems())if(K.local==="thead"||K.local==="tbody"||K.local==="tfoot"){if(Z)Z=!1,J+=1;let G=K.local==="thead";for(let M of K.childElems())if(M.local==="tr")z.push([M,G,J]);J+=1}else if(K.local==="tr")Z=!0,z.push([K,!1,J]);if(z.length===0)return;let Q=new Map;for(let K=0;K<z.length;K+=1)Q.set(z[K][2],K);let F=new hf,V=0;for(let K=0;K<z.length;K+=1){let[G,M,w]=z[K];F.nextRow();let L=!0,H=!1;for(let j of G.childElems()){if(j.local!=="td"&&j.local!=="th")continue;if(H=!0,j.local!=="th")L=!1;let X=Jf(u(j.attrAny("colspan")??"")??1,1,1000),C,d=j.attrAny("rowspan"),S=d!==void 0?u(d):void 0;if(S===0)C=(Q.get(w)??K)-K+1;else if(S!==void 0)C=Jf(S,1,65534);else C=1;F.place(xf(this.subBlocks(j,U),X,C))}if(K===V&&(M||L&&H))V+=1}let $=F.finish("data");if($.grid.length===0)return;return $.headerRows=of($,V),{type:"table",table:$}}flushParagraph(){if(this.inlines.length>0){let f=this.inlines.splice(0,this.inlines.length);if(P(f))this.blocks.push({type:"paragraph",inlines:f})}this.startBoundary=!0}}function cf(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 Y(f){switch(f.type){case"paragraph":return t(f.inlines);case"heading":return t(f.content);case"list":return f.list.items.flatMap((U)=>U.blocks.map(Y)).join(" ");case"blockQuote":return f.blocks.map(Y).join(" ");case"codeBlock":return f.text;case"table":return f.table.grid.flatMap((U)=>U.flatMap((z)=>z.type==="origin"?[z.cell.blocks.map(Y).join(" ")]:[])).join(" ");case"rule":return""}}function Vf(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 mf(f){return Vf(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 lf(f){return f.childElems().some((U)=>mf(U.local))}function P(f){return!v(f)||f.some((U)=>U.type==="anchor")}function N(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 af(tf(J.text))}if(J.type==="lineBreak")return!0;if(J.type==="link"){if(v(J.content))continue;return N(J.content,!1)}return!1}return U}function tf(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 af(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 sf(f,U){let z=0;while(z<f.length&&f[z]===U)z+=1;return f.slice(z)}function zf(f){let U=f.trim();if(!/^-?\d+$/.test(U))return;let z=Number(U);return Number.isFinite(z)?z:void 0}function Jf(f,U,z){return Math.min(z,Math.max(U,f))}function Lf(f){let U=V0(f),z=new Mf;return Cf(U,new h(void 0,z),z)}function Xf(f){let U=z0(f),z=f0(U)??U0(U);if(z===void 0)throw Z0.malformed("no text/html or text/plain part");let J=new Mf,Z=H0(U),Q=w0(z);if(z.contentType==="text/plain"){let V=Hf(),$=wf(Q);if($.length>0)V.blocks.push({type:"paragraph",inlines:[$0($)]});return V.assets=J.assets,V}let F=qf(Q,new TextEncoder().encode(Q));return Cf(F,new h(Z,J),J)}function V0(f){return qf(L0(f),f)}function qf(f,U){if(n(f))try{return J0(U)}catch{}return b(f)}function Cf(f,U,z){gf(f);let J=G0(f),Z=j0(f),Q=Hf();return Q.blocks.push(...E(Z,J,U)),Q.assets=z.assets,Q}function G0(f){let U=new W;for(let z of f.descendantElems())if(z.local==="style")U.add(z.text());return U}function j0(f){if(f.local==="body")return f;let U=f.local==="html"?f:x(f,"html");if(U!==void 0){let J=x(U,"body");if(J!==void 0)return J;return U}return x(f,"body")??f}function x(f,U){return f.childElems().find((z)=>z.local===U)}function gf(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())gf(z)}class h{cids;assets;constructor(f,U){this.cids=f;this.assets=U}linkTarget(f){if(f.length===0)return;if(f.startsWith("#")){let U=ef(f.slice(1));return U.length>0?{type:"anchor",id:U}:void 0}if(Gf(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(Gf(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:F0(J);return{type:"asset",id:this.assets.add(Z,J,z.bytes)}}}function H0(f){let U=new Map;for(let z of Q0(f)){let J=jf(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=wf(f);if(U.startsWith("<")&&U.endsWith(">")&&U.length>=2)U=U.slice(1,-1);return U}function w0(f){let U=jf(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 K0(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 q0=new Set(["htm","html","xhtml","mhtml","mht"]),C0=new Set(["mhtml","mht"]);function o(){return{id:"html",sniff(f,U){if(Y0(f)&&!Wf(f))return 2;if(U?.path!==void 0&&q0.has(Yf(U.path)??""))return 1;return 0},convert(f,U){g0(f);let z=U?.path!==void 0?Yf(U.path):void 0;if(z!==void 0&&C0.has(z)||W0(f))return{markdown:Df(Xf(f))};if(Wf(f))throw O.unsupported("OpenDocument");return{markdown:Df(Lf(f))}}}}function g0(f){if(D0(f))throw O.unsupported("pdf");if(X0(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 D0(f){let U=If(f);return O0(f,"%PDF-",U)}function Y0(f){let U=If(f);if(Of(f,"<!doctype html",U)&&!Sf(f[U+14]))return!0;if(Of(f,"<html",U)&&!Sf(f[U+5]))return!0;return y(f.subarray(0,Math.min(f.length,4096))).includes("http://www.w3.org/1999/xhtml")}function W0(f){let U=y(f.subarray(0,Math.min(f.length,512))).toLowerCase();return U.includes("multipart/related")||U.includes("content-location:")}function Wf(f){let U=y(f.subarray(0,Math.min(f.length,8192))),z=k(U,0);if(U.startsWith("<?xml",z)){let Z=U.indexOf("?>",z);if(Z<0)return!1;z=k(U,Z+2)}while(U.startsWith("<!--",z)){let Z=U.indexOf("-->",z+4);if(Z<0)return!1;z=k(U,Z+3)}let J=U.slice(z,z+2048);return/<office:document\b/.test(J)}function If(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 k(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 O0(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 Of(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 F=Z>=65&&Z<=90?Z+32:Z,V=Q>=65&&Q<=90?Q+32:Q;if(F!==V)return!1}return!0}function Sf(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 y(f){return new TextDecoder("utf-8",{fatal:!1}).decode(f)}import{create as S0}from"@mdgate/core";var I0=S0([o()]);function _0(f,U){return I0(f,U)}export{_0 as toMarkdown,E as toBlocks,o as html,W as Stylesheet};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgate/html",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
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.
|
|
26
|
-
"@mdgate/core": "0.
|
|
27
|
-
"@mdgate/document": "0.
|
|
28
|
-
"@mdgate/office-common": "0.
|
|
29
|
-
"@mdgate/utils": "0.
|
|
25
|
+
"@mdgate/containers": "0.5.1",
|
|
26
|
+
"@mdgate/core": "0.5.1",
|
|
27
|
+
"@mdgate/document": "0.5.1",
|
|
28
|
+
"@mdgate/office-common": "0.5.1",
|
|
29
|
+
"@mdgate/utils": "0.5.1"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|