@mdgate/mobi 0.5.2 → 0.5.4

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.
Files changed (2) hide show
  1. package/dist/index.js +2 -2
  2. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import{ConvertError as E}from"@mdgate/core";import{fileExtension as En}from"@mdgate/utils";import{parseXml as fn}from"@mdgate/containers";import{ConvertError as a}from"@mdgate/core";import{documentToMarkdown as K,emptyDocument as V,heading as Y,plain as P}from"@mdgate/document";import{Stylesheet as ln,toBlocks as un}from"@mdgate/html";import{decode as gn,isAbsoluteUri as q,trim as O}from"@mdgate/utils";function p(n,t){return(n[t]??0)<<8|(n[t+1]??0)}function h(n,t){return((n[t]??0)<<24|(n[t+1]??0)<<16|(n[t+2]??0)<<8|(n[t+3]??0))>>>0}function _(n,t,r){if(t<0||r<=0||t+r>n.length)return"";let e="";for(let o=0;o<r;o+=1)e+=String.fromCharCode(n[t+o]);return e}function k(n,t,r){if(t+r.length>n.length)return!1;for(let e=0;e<r.length;e+=1)if(n[t+e]!==r.charCodeAt(e))return!1;return!0}function H(n,t){let r=new Uint8Array(t),e=0;for(let o of n)r.set(o,e),e+=o.length;return r}import{ConvertError as s}from"@mdgate/core";var I=1,L=2,N=17480;function Z(n,t,r,e){let o=e<0?Number.MAX_SAFE_INTEGER:e;if(t===I)return n;if(t===L)return nn(n,o);if(t===N){if(r===void 0)throw s.malformed("HUFF/CDIC dictionary is missing");return r.unpack(n,o)}throw s.unsupported(`compression ${t}`)}function nn(n,t){let r=new Uint8Array(Math.min(Math.max(n.length*2,64),t)),e=0,o=0;while(o<n.length){let f=n[o];if(o+=1,f>=1&&f<=8){let l=Math.min(f,n.length-o);r=M(r,e,l,t),r.set(n.subarray(o,o+l),e),e+=l,o+=l;continue}if(f<128){r=M(r,e,1,t),r[e]=f,e+=1;continue}if(f>=192){r=M(r,e,2,t),r[e]=32,r[e+1]=f^128,e+=2;continue}if(o>=n.length)break;let i=f<<8|n[o];o+=1;let u=i>>3&2047,g=(i&7)+3;if(u===0||u>e)throw s.malformed("invalid PalmDOC backreference");r=M(r,e,g,t);for(let l=0;l<g;l+=1)r[e+l]=r[e-u+l];e+=g}return r.subarray(0,e)}class D{dict1;minCode;maxCode;dictionary;constructor(n){if(n.length===0)throw s.malformed("HUFF/CDIC dictionary is missing");let t=tn(n[0]);this.dict1=t.dict1,this.minCode=t.minCode,this.maxCode=t.maxCode,this.dictionary=[];for(let r=1;r<n.length;r+=1)rn(n[r],this.dictionary);if(this.dictionary.length===0)throw s.malformed("HUFF/CDIC dictionary is empty")}unpack(n,t){return C(n,this.dict1,this.minCode,this.maxCode,this.dictionary,t)}}function tn(n){if(n.length<16||!k(n,0,"HUFF")||h(n,4)!==24)throw s.malformed("invalid HUFF header");let t=h(n,8),r=h(n,12);if(t+1024>n.length||r+256>n.length)throw s.malformed("truncated HUFF tables");let e=[];for(let i=0;i<256;i+=1){let u=h(n,t+i*4),g=u&31;if(g===0)throw s.malformed("invalid HUFF dictionary");e.push({codeLen:g,term:(u&128)!==0,maxCode:W(u>>>8,32-g)})}let o=[0],f=[0];for(let i=0;i<32;i+=1){let u=h(n,r+i*8),g=h(n,r+i*8+4),l=32-(i+1);o.push(en(u,l)),f.push(W(g,l))}return{dict1:e,minCode:o,maxCode:f}}function rn(n,t){if(n.length<16||!k(n,0,"CDIC")||h(n,4)!==16)throw s.malformed("invalid CDIC header");let r=h(n,8),e=h(n,12);if(e>16)throw s.malformed("invalid CDIC bit width");let o=Math.min(1<<e,Math.max(0,r-t.length));if(16+o*2>n.length)throw s.malformed("truncated CDIC index");for(let f=0;f<o;f+=1){let u=16+p(n,16+f*2);if(u+2>n.length)throw s.malformed("truncated CDIC phrase");let g=p(n,u),l=g&32767,x=u+2;if(x+l>n.length)throw s.malformed("truncated CDIC phrase");t.push({data:n.subarray(x,x+l),unpacked:(g&32768)!==0})}}function C(n,t,r,e,o,f){let i=n.length*8,u=0,g=$(n,0),l=32,x=[],d=0;for(;;){if(l<=0)u+=4,g=$(n,u),l+=32;let m=Number(g>>BigInt(l)&0xffffffffn)>>>0,{codeLen:c,term:U,maxCode:F}=t[m>>>24];if(!U){while(c<r.length&&m<r[c])c+=1;if(c>=e.length)throw s.malformed("invalid HUFF code");F=e[c]}if(l-=c,i-=c,i<0)break;let X=32-c,v=X>=32?0:F-m>>>X,A=o[v];if(A===void 0)throw s.malformed("invalid HUFF phrase");if(!A.unpacked)A.data=C(A.data,t,r,e,o,f),A.unpacked=!0;if(x.push(A.data),d+=A.data.length,d>f)throw s.malformed("decompressed text exceeds the remaining book length")}return H(x,d)}function $(n,t){let r=0n;for(let e=0;e<8;e+=1)r=r<<8n|BigInt(n[t+e]??0);return r}function en(n,t){if(t<=0)return n>>>0;if(t>=32)return 0;return Number(BigInt(n)<<BigInt(t)&0xffffffffn)>>>0}function W(n,t){if(t<=0)return n>>>0;if(t>=32)return 4294967295;return Number((BigInt(n)+1n<<BigInt(t))-1n&0xffffffffn)>>>0}function M(n,t,r,e){if(t+r>e)throw s.malformed("decompressed text exceeds the remaining book length");if(t+r<=n.length)return n;let o=n.length===0?64:n.length;while(o<t+r)o*=2;let f=new Uint8Array(Math.min(o,e));return f.set(n.subarray(0,t)),f}function G(n,t){let r=n,e=t>>>1;while(e!==0){if((e&1)!==0){let o=on(r);if(o<=0||o>r.length)break;r=r.subarray(0,r.length-o)}e>>>=1}if((t&1)!==0&&r.length>0){let o=(r[r.length-1]&3)+1;if(o<=r.length)r=r.subarray(0,r.length-o)}return r}function on(n){let t=0,r=Math.min(n.length,4);for(let e=0;e<r;e+=1){let o=n[n.length-1-e];if((o&128)!==0)t=0;t=t<<7|o&127}return t}var B=78,w=4294967295,hn=new TextEncoder,an={linkTarget(n){if(n.length===0)return;if(n.startsWith("#")){let t=n.slice(1);return t.length>0?{type:"anchor",id:t}:void 0}if(q(n))return{type:"external",url:n};return{type:"relative",url:n}},imageSource(n){if(n.length===0)return;if(q(n))return{type:"external",url:n};return},anchorId(n){return n}};function z(n){if(Ln(n)||Dn(n)||Rn(n))return!1;if(n.length>=68){let t=_(n,60,8);if(t==="BOOKMOBI"||t==="TEXtREAd")return!0}return In(n)}function y(n){let t=cn(n),r=Un(t.text);if(r.length>0){let o=V();if(t.title!==void 0)o.blocks.push(Y(1,[P(t.title)]));for(let f of r)o.blocks.push(...pn(f));if(o.blocks.length===0)throw a.malformed("empty text");return K(Mn(o,t.title))}let e=V();if(t.title!==void 0)e.blocks.push(Y(1,[P(t.title)]));for(let o of On(t.text))e.blocks.push({type:"paragraph",inlines:[P(o)]});if(e.blocks.length===0)throw a.malformed("empty text");return K(e)}function cn(n){let t=sn(n);if(t.records.length===0)throw a.missingPart("text");let r=t.records[0],e=J(r);if(e.encryption!==0)throw a.encrypted();let o=Q(r);if(o!==void 0&&j(o))throw a.encrypted();let f=o!==void 0?xn(r,o):void 0,i=b(o?.encoding??1252),u=Nn(r,t.name,o,f,i),g={records:t.records,header:r,palm:e,mobi:o},l=f?.kf8Boundary;if(l!==void 0&&l+1<t.records.length){let d=t.records[l];if(k(d,0,"BOUNDARY")){let m=t.records[l+1],c=J(m);if(c.encryption!==0)throw a.encrypted();let U=Q(m);if(U!==void 0&&j(U))throw a.encrypted();g={records:t.records,header:m,palm:c,mobi:U}}}let x=mn(g.records,g.palm,g.mobi,i);return{title:u,text:x}}function sn(n){if(n.length<B+8)throw a.malformed("truncated Palm database");let t=p(n,76);if(t===0)throw a.malformed("Palm database has no records");if(n.length<B+t*8)throw a.malformed("truncated record list");let r=[];for(let o=0;o<t;o+=1)r.push(h(n,B+o*8));let e=[];for(let o=0;o<t;o+=1){let f=r[o],i=o+1<t?r[o+1]:n.length;if(f>n.length||i<f)throw a.malformed("invalid record offset");e.push(n.subarray(f,Math.min(i,n.length)))}return{name:Pn(n),records:e}}function J(n){if(n.length<16)throw a.malformed("truncated PalmDOC header");let t=p(n,0);if(t!==I&&t!==L&&t!==N)throw a.unsupported(`compression ${t}`);return{compression:t,textLength:h(n,4),recordCount:p(n,8),encryption:p(n,12)}}function Q(n){if(n.length<24||!k(n,16,"MOBI"))return;let t=h(n,20);if(t<8)throw a.malformed("truncated MOBI header");let r=(f,i)=>n.length>=f+4?h(n,f):i,e=t>=228&&n.length>=244?h(n,240):0,o=t>=192&&n.length>=194?p(n,192):1;return{headerLength:t,encoding:n.length>=32?h(n,28):1252,firstNonBook:r(80,w),fullNameOffset:r(84,0),fullNameLength:r(88,0),firstImage:r(108,w),huffOffset:r(112,0),huffCount:r(116,0),exthFlags:r(128,0),drmOffset:r(168,w),drmCount:r(172,0),firstContent:o===0?1:o,extraFlags:e}}function xn(n,t){if((t.exthFlags&64)===0)return;let r=16+t.headerLength;if(r+12>n.length||!k(n,r,"EXTH"))return;let e=h(n,r+4),o=h(n,r+8);if(o>1e4)throw a.malformed("truncated EXTH header");let f=r+12,i=Math.min(n.length,r+e),u,g;for(let l=0;l<o&&f+8<=i;l+=1){let x=h(n,f),d=h(n,f+4);if(d<8||f+d>n.length)break;let m=n.subarray(f+8,f+d);if(x===503){let c=O(T(m,b(t.encoding)));if(c.length>0)u=c}if(x===121&&m.length>=4)g=h(m,0);f+=d}return{title:u,kf8Boundary:g}}function mn(n,t,r,e){let o=r!==void 0&&r.firstContent>0?r.firstContent:1,f=o+Math.max(t.recordCount,1)-1;if(r!==void 0&&r.firstNonBook!==0&&r.firstNonBook!==w)f=Math.min(f,r.firstNonBook-1);if(r!==void 0&&r.firstImage!==0&&r.firstImage!==w)f=Math.min(f,r.firstImage-1);if(o>=n.length||f<o)throw a.missingPart("text");let i=t.textLength>0?t.textLength:Number.MAX_SAFE_INTEGER,u=dn(n,t,r),g=[],l=0;for(let d=o;d<=f&&d<n.length;d+=1){let m=n[d];if(m.length===0)continue;if(r!==void 0&&r.extraFlags!==0)m=G(m,r.extraFlags);let c=Z(m,t.compression,u,i-l);if(g.push(c),l+=c.length,t.textLength>0&&l>=t.textLength)break}if(l===0)throw a.missingPart("text");let x=H(g,l);if(t.textLength>0&&x.length>t.textLength)x=x.subarray(0,t.textLength);return Tn(T(x,e))}function dn(n,t,r){if(t.compression!==N)return;if(r===void 0||r.huffCount===0||r.huffOffset>=n.length)throw a.malformed("HUFF/CDIC dictionary is missing");let e=Math.min(n.length,r.huffOffset+r.huffCount);return new D(n.slice(r.huffOffset,e))}function pn(n){let t=Bn(n);try{let e=fn(hn.encode(kn(t))),o=An(e),f=wn(e),i=un(o,f,an);if(i.length>0)return i}catch(e){if(e instanceof a&&e.isFatal())throw e}let r=Hn(n);return r.length===0?[]:[{type:"paragraph",inlines:[P(r)]}]}function kn(n){return n.replace(/<(br|hr|img|meta|link|mbp:pagebreak)(\s[^>/]*)?\s*\/?>/gi,"<$1$2/>")}function An(n){if(n.local==="body")return n;let t=n.local==="html"?n:R(n,"html");if(t!==void 0)return R(t,"body")??t;return R(n,"body")??n}function R(n,t){for(let r of n.childElems())if(r.local===t)return r;return}function wn(n){let t=new ln;for(let r of n.descendantElems())if(r.local==="style")t.add(r.text());return t}function Un(n){let t=[],r=/<html\b[\s\S]*?<\/html>/gi,e=r.exec(n);while(e!==null)t.push(e[0]),e=r.exec(n);if(t.length>0)return t;if(/<[a-zA-Z][^>]*>/.test(n))return[n];return[]}function Bn(n){if(/<html[\s>]/i.test(n))return n;return`<html><body>${n}</body></html>`}function On(n){let t=n.replace(/\r\n/g,`
1
+ import{ConvertError as S}from"@mdgate/core";import{fileExtension as Dn}from"@mdgate/utils";import{ConvertError as a}from"@mdgate/core";import{documentToMarkdown as K,emptyDocument as Y,heading as q,plain as P}from"@mdgate/document";import{parseHtml as fn,Stylesheet as ln,toBlocks as un}from"@mdgate/html";import{decode as gn,isAbsoluteUri as J,trim as H}from"@mdgate/utils";function p(n,t){return(n[t]??0)<<8|(n[t+1]??0)}function h(n,t){return((n[t]??0)<<24|(n[t+1]??0)<<16|(n[t+2]??0)<<8|(n[t+3]??0))>>>0}function _(n,t,r){if(t<0||r<=0||t+r>n.length)return"";let e="";for(let o=0;o<r;o+=1)e+=String.fromCharCode(n[t+o]);return e}function k(n,t,r){if(t+r.length>n.length)return!1;for(let e=0;e<r.length;e+=1)if(n[t+e]!==r.charCodeAt(e))return!1;return!0}function O(n,t){let r=new Uint8Array(t),e=0;for(let o of n)r.set(o,e),e+=o.length;return r}import{ConvertError as s}from"@mdgate/core";var I=1,L=2,N=17480;function $(n,t,r,e){let o=e<0?Number.MAX_SAFE_INTEGER:e;if(t===I)return n;if(t===L)return nn(n,o);if(t===N){if(r===void 0)throw s.malformed("HUFF/CDIC dictionary is missing");return r.unpack(n,o)}throw s.unsupported(`compression ${t}`)}function nn(n,t){let r=new Uint8Array(Math.min(Math.max(n.length*2,64),t)),e=0,o=0;while(o<n.length){let f=n[o];if(o+=1,f>=1&&f<=8){let l=Math.min(f,n.length-o);r=M(r,e,l,t),r.set(n.subarray(o,o+l),e),e+=l,o+=l;continue}if(f<128){r=M(r,e,1,t),r[e]=f,e+=1;continue}if(f>=192){r=M(r,e,2,t),r[e]=32,r[e+1]=f^128,e+=2;continue}if(o>=n.length)break;let i=f<<8|n[o];o+=1;let u=i>>3&2047,g=(i&7)+3;if(u===0||u>e)throw s.malformed("invalid PalmDOC backreference");r=M(r,e,g,t);for(let l=0;l<g;l+=1)r[e+l]=r[e-u+l];e+=g}return r.subarray(0,e)}class D{dict1;minCode;maxCode;dictionary;constructor(n){if(n.length===0)throw s.malformed("HUFF/CDIC dictionary is missing");let t=tn(n[0]);this.dict1=t.dict1,this.minCode=t.minCode,this.maxCode=t.maxCode,this.dictionary=[];for(let r=1;r<n.length;r+=1)rn(n[r],this.dictionary);if(this.dictionary.length===0)throw s.malformed("HUFF/CDIC dictionary is empty")}unpack(n,t){return C(n,this.dict1,this.minCode,this.maxCode,this.dictionary,t)}}function tn(n){if(n.length<16||!k(n,0,"HUFF")||h(n,4)!==24)throw s.malformed("invalid HUFF header");let t=h(n,8),r=h(n,12);if(t+1024>n.length||r+256>n.length)throw s.malformed("truncated HUFF tables");let e=[];for(let i=0;i<256;i+=1){let u=h(n,t+i*4),g=u&31;if(g===0)throw s.malformed("invalid HUFF dictionary");e.push({codeLen:g,term:(u&128)!==0,maxCode:Z(u>>>8,32-g)})}let o=[0],f=[0];for(let i=0;i<32;i+=1){let u=h(n,r+i*8),g=h(n,r+i*8+4),l=32-(i+1);o.push(en(u,l)),f.push(Z(g,l))}return{dict1:e,minCode:o,maxCode:f}}function rn(n,t){if(n.length<16||!k(n,0,"CDIC")||h(n,4)!==16)throw s.malformed("invalid CDIC header");let r=h(n,8),e=h(n,12);if(e>16)throw s.malformed("invalid CDIC bit width");let o=Math.min(1<<e,Math.max(0,r-t.length));if(16+o*2>n.length)throw s.malformed("truncated CDIC index");for(let f=0;f<o;f+=1){let u=16+p(n,16+f*2);if(u+2>n.length)throw s.malformed("truncated CDIC phrase");let g=p(n,u),l=g&32767,x=u+2;if(x+l>n.length)throw s.malformed("truncated CDIC phrase");t.push({data:n.subarray(x,x+l),unpacked:(g&32768)!==0})}}function C(n,t,r,e,o,f){let i=n.length*8,u=0,g=W(n,0),l=32,x=[],d=0;for(;;){if(l<=0)u+=4,g=W(n,u),l+=32;let m=Number(g>>BigInt(l)&0xffffffffn)>>>0,{codeLen:c,term:U,maxCode:F}=t[m>>>24];if(!U){while(c<r.length&&m<r[c])c+=1;if(c>=e.length)throw s.malformed("invalid HUFF code");F=e[c]}if(l-=c,i-=c,i<0)break;let X=32-c,v=X>=32?0:F-m>>>X,A=o[v];if(A===void 0)throw s.malformed("invalid HUFF phrase");if(!A.unpacked)A.data=C(A.data,t,r,e,o,f),A.unpacked=!0;if(x.push(A.data),d+=A.data.length,d>f)throw s.malformed("decompressed text exceeds the remaining book length")}return O(x,d)}function W(n,t){let r=0n;for(let e=0;e<8;e+=1)r=r<<8n|BigInt(n[t+e]??0);return r}function en(n,t){if(t<=0)return n>>>0;if(t>=32)return 0;return Number(BigInt(n)<<BigInt(t)&0xffffffffn)>>>0}function Z(n,t){if(t<=0)return n>>>0;if(t>=32)return 4294967295;return Number((BigInt(n)+1n<<BigInt(t))-1n&0xffffffffn)>>>0}function M(n,t,r,e){if(t+r>e)throw s.malformed("decompressed text exceeds the remaining book length");if(t+r<=n.length)return n;let o=n.length===0?64:n.length;while(o<t+r)o*=2;let f=new Uint8Array(Math.min(o,e));return f.set(n.subarray(0,t)),f}function G(n,t){let r=n,e=t>>>1;while(e!==0){if((e&1)!==0){let o=on(r);if(o<=0||o>r.length)break;r=r.subarray(0,r.length-o)}e>>>=1}if((t&1)!==0&&r.length>0){let o=(r[r.length-1]&3)+1;if(o<=r.length)r=r.subarray(0,r.length-o)}return r}function on(n){let t=0,r=Math.min(n.length,4);for(let e=0;e<r;e+=1){let o=n[n.length-1-e];if((o&128)!==0)t=0;t=t<<7|o&127}return t}var B=78,w=4294967295,hn={linkTarget(n){if(n.length===0)return;if(n.startsWith("#")){let t=n.slice(1);return t.length>0?{type:"anchor",id:t}:void 0}if(J(n))return{type:"external",url:n};return{type:"relative",url:n}},imageSource(n){if(n.length===0)return;if(J(n))return{type:"external",url:n};return},anchorId(n){return n}};function z(n){if(Tn(n)||In(n)||Ln(n))return!1;if(n.length>=68){let t=_(n,60,8);if(t==="BOOKMOBI"||t==="TEXtREAd")return!0}return Pn(n)}function y(n){let t=an(n),r=An(t.text);if(r.length>0){let o=Y();if(t.title!==void 0)o.blocks.push(q(1,[P(t.title)]));for(let f of r)o.blocks.push(...dn(f));if(o.blocks.length===0)throw a.malformed("empty text");return K(Hn(o,t.title))}let e=Y();if(t.title!==void 0)e.blocks.push(q(1,[P(t.title)]));for(let o of Un(t.text))e.blocks.push({type:"paragraph",inlines:[P(o)]});if(e.blocks.length===0)throw a.malformed("empty text");return K(e)}function an(n){let t=cn(n);if(t.records.length===0)throw a.missingPart("text");let r=t.records[0],e=Q(r);if(e.encryption!==0)throw a.encrypted();let o=V(r);if(o!==void 0&&j(o))throw a.encrypted();let f=o!==void 0?sn(r,o):void 0,i=b(o?.encoding??1252),u=On(r,t.name,o,f,i),g={records:t.records,header:r,palm:e,mobi:o},l=f?.kf8Boundary;if(l!==void 0&&l+1<t.records.length){let d=t.records[l];if(k(d,0,"BOUNDARY")){let m=t.records[l+1],c=Q(m);if(c.encryption!==0)throw a.encrypted();let U=V(m);if(U!==void 0&&j(U))throw a.encrypted();g={records:t.records,header:m,palm:c,mobi:U}}}let x=xn(g.records,g.palm,g.mobi,i);return{title:u,text:x}}function cn(n){if(n.length<B+8)throw a.malformed("truncated Palm database");let t=p(n,76);if(t===0)throw a.malformed("Palm database has no records");if(n.length<B+t*8)throw a.malformed("truncated record list");let r=[];for(let o=0;o<t;o+=1)r.push(h(n,B+o*8));let e=[];for(let o=0;o<t;o+=1){let f=r[o],i=o+1<t?r[o+1]:n.length;if(f>n.length||i<f)throw a.malformed("invalid record offset");e.push(n.subarray(f,Math.min(i,n.length)))}return{name:Mn(n),records:e}}function Q(n){if(n.length<16)throw a.malformed("truncated PalmDOC header");let t=p(n,0);if(t!==I&&t!==L&&t!==N)throw a.unsupported(`compression ${t}`);return{compression:t,textLength:h(n,4),recordCount:p(n,8),encryption:p(n,12)}}function V(n){if(n.length<24||!k(n,16,"MOBI"))return;let t=h(n,20);if(t<8)throw a.malformed("truncated MOBI header");let r=(f,i)=>n.length>=f+4?h(n,f):i,e=t>=228&&n.length>=244?h(n,240):0,o=t>=192&&n.length>=194?p(n,192):1;return{headerLength:t,encoding:n.length>=32?h(n,28):1252,firstNonBook:r(80,w),fullNameOffset:r(84,0),fullNameLength:r(88,0),firstImage:r(108,w),huffOffset:r(112,0),huffCount:r(116,0),exthFlags:r(128,0),drmOffset:r(168,w),drmCount:r(172,0),firstContent:o===0?1:o,extraFlags:e}}function sn(n,t){if((t.exthFlags&64)===0)return;let r=16+t.headerLength;if(r+12>n.length||!k(n,r,"EXTH"))return;let e=h(n,r+4),o=h(n,r+8);if(o>1e4)throw a.malformed("truncated EXTH header");let f=r+12,i=Math.min(n.length,r+e),u,g;for(let l=0;l<o&&f+8<=i;l+=1){let x=h(n,f),d=h(n,f+4);if(d<8||f+d>n.length)break;let m=n.subarray(f+8,f+d);if(x===503){let c=H(T(m,b(t.encoding)));if(c.length>0)u=c}if(x===121&&m.length>=4)g=h(m,0);f+=d}return{title:u,kf8Boundary:g}}function xn(n,t,r,e){let o=r!==void 0&&r.firstContent>0?r.firstContent:1,f=o+Math.max(t.recordCount,1)-1;if(r!==void 0&&r.firstNonBook!==0&&r.firstNonBook!==w)f=Math.min(f,r.firstNonBook-1);if(r!==void 0&&r.firstImage!==0&&r.firstImage!==w)f=Math.min(f,r.firstImage-1);if(o>=n.length||f<o)throw a.missingPart("text");let i=t.textLength>0?t.textLength:Number.MAX_SAFE_INTEGER,u=mn(n,t,r),g=[],l=0;for(let d=o;d<=f&&d<n.length;d+=1){let m=n[d];if(m.length===0)continue;if(r!==void 0&&r.extraFlags!==0)m=G(m,r.extraFlags);let c=$(m,t.compression,u,i-l);if(g.push(c),l+=c.length,t.textLength>0&&l>=t.textLength)break}if(l===0)throw a.missingPart("text");let x=O(g,l);if(t.textLength>0&&x.length>t.textLength)x=x.subarray(0,t.textLength);return Nn(T(x,e))}function mn(n,t,r){if(t.compression!==N)return;if(r===void 0||r.huffCount===0||r.huffOffset>=n.length)throw a.malformed("HUFF/CDIC dictionary is missing");let e=Math.min(n.length,r.huffOffset+r.huffCount);return new D(n.slice(r.huffOffset,e))}function dn(n){let t=wn(n);try{let e=fn(t),o=pn(e),f=kn(e),i=un(o,f,hn);if(i.length>0)return i}catch(e){if(e instanceof a&&e.isFatal())throw e}let r=Bn(n);return r.length===0?[]:[{type:"paragraph",inlines:[P(r)]}]}function pn(n){if(n.local==="body")return n;let t=n.local==="html"?n:R(n,"html");if(t!==void 0)return R(t,"body")??t;return R(n,"body")??n}function R(n,t){for(let r of n.childElems())if(r.local===t)return r;return}function kn(n){let t=new ln;for(let r of n.descendantElems())if(r.local==="style")t.add(r.text());return t}function An(n){let t=[],r=/<html\b[\s\S]*?<\/html>/gi,e=r.exec(n);while(e!==null)t.push(e[0]),e=r.exec(n);if(t.length>0)return t;if(/<[a-zA-Z][^>]*>/.test(n))return[n];return[]}function wn(n){if(/<html[\s>]/i.test(n))return n;return`<html><body>${n}</body></html>`}function Un(n){let t=n.replace(/\r\n/g,`
2
2
  `).replace(/\r/g,`
3
- `),r=[];for(let e of t.split(/\n(?:[ \t]*\n)+/)){let o=O(e.replace(/[ \t]*\n[ \t]*/g," "));if(o.length>0)r.push(o)}return r}function Hn(n){return O(n.replace(/<[^>]+>/g," ").replace(/\s+/g," "))}function Mn(n,t){if(t===void 0||n.blocks.length<2)return n;let r=n.blocks[0],e=n.blocks[1];if(r?.type!=="heading"||e?.type!=="heading")return n;let o=r.content[0],f=e.content[0];if(o?.type!=="text"||f?.type!=="text")return n;if(o.text.toLowerCase()!==f.text.toLowerCase())return n;return{...n,blocks:n.blocks.slice(1)}}function Nn(n,t,r,e,o){if(e?.title!==void 0&&e.title.length>0)return e.title;if(r!==void 0&&r.fullNameLength>0&&r.fullNameOffset>0){let f=r.fullNameOffset,i=f+r.fullNameLength;if(i<=n.length){let u=O(T(n.subarray(f,i),o));if(u.length>0)return u}}return t.length>0?t:void 0}function Pn(n){let t=Math.min(32,n.length),r=0;while(r<t&&n[r]!==0)r+=1;if(r===0)return"";return O(T(n.subarray(0,r),"windows-1252"))}function b(n){if(n===65001||n===65000)return"utf-8";if(n===932)return"shift_jis";if(n===950)return"big5";if(n===949)return"euc-kr";if(n===936)return"gbk";return"windows-1252"}function T(n,t){try{return gn(n,t)}catch{return new TextDecoder("utf-8",{fatal:!1}).decode(n)}}function Tn(n){return n.includes("\x00")?n.replace(/\0+/g,""):n}function j(n){if(n.drmOffset===0||n.drmOffset===w)return!1;return n.drmCount!==0&&n.drmCount!==w}function In(n){if(n.length<B+8)return!1;if(p(n,76)<1)return!1;let r=h(n,B);return r+20<=n.length&&k(n,r+16,"MOBI")}function Ln(n){let t=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)t=3;while(t<n.length){let r=n[t];if(r!==9&&r!==10&&r!==13&&r!==32)break;t+=1}return t+5<=n.length&&n[t]===37&&n[t+1]===80&&n[t+2]===68&&n[t+3]===70&&n[t+4]===45}function Dn(n){return n.length>=8&&n[0]===208&&n[1]===207&&n[2]===17&&n[3]===224&&n[4]===161&&n[5]===177&&n[6]===26&&n[7]===225}function Rn(n){return n.length>=4&&n[0]===80&&n[1]===75&&(n[2]===3||n[2]===5||n[2]===7)&&(n[3]===4||n[3]===6||n[3]===8)}var Sn=new Set(["mobi","azw","azw3","prc"]);function S(){return{id:"mobi",sniff(n,t){if(z(n))return 2;if(t?.path!==void 0&&Sn.has(En(t.path)??""))return 1;return 0},convert(n){return Fn(n),{markdown:y(n)}}}}function Fn(n){if(Xn(n))throw E.unsupported("pdf");if(_n(n))throw E.unsupported("ole");if($n(n))throw E.unsupported("zip")}function Xn(n){let t=Wn(n);return Zn(n,"%PDF-",t)}function _n(n){return n.length>=8&&n[0]===208&&n[1]===207&&n[2]===17&&n[3]===224&&n[4]===161&&n[5]===177&&n[6]===26&&n[7]===225}function $n(n){if(n.length<4||n[0]!==80||n[1]!==75)return!1;return n[2]===3&&n[3]===4||n[2]===5&&n[3]===6||n[2]===7&&n[3]===8}function Wn(n){let t=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)t=3;while(t<n.length){let r=n[t];if(r!==9&&r!==10&&r!==13&&r!==32)break;t+=1}return t}function Zn(n,t,r){if(r+t.length>n.length)return!1;for(let e=0;e<t.length;e+=1)if(n[r+e]!==t.charCodeAt(e))return!1;return!0}import{create as Cn}from"@mdgate/core";var Gn=Cn([S()]);function Kn(n,t){return Gn(n,t)}export{Kn as toMarkdown,S as mobi};
3
+ `),r=[];for(let e of t.split(/\n(?:[ \t]*\n)+/)){let o=H(e.replace(/[ \t]*\n[ \t]*/g," "));if(o.length>0)r.push(o)}return r}function Bn(n){return H(n.replace(/<[^>]+>/g," ").replace(/\s+/g," "))}function Hn(n,t){if(t===void 0||n.blocks.length<2)return n;let r=n.blocks[0],e=n.blocks[1];if(r?.type!=="heading"||e?.type!=="heading")return n;let o=r.content[0],f=e.content[0];if(o?.type!=="text"||f?.type!=="text")return n;if(o.text.toLowerCase()!==f.text.toLowerCase())return n;return{...n,blocks:n.blocks.slice(1)}}function On(n,t,r,e,o){if(e?.title!==void 0&&e.title.length>0)return e.title;if(r!==void 0&&r.fullNameLength>0&&r.fullNameOffset>0){let f=r.fullNameOffset,i=f+r.fullNameLength;if(i<=n.length){let u=H(T(n.subarray(f,i),o));if(u.length>0)return u}}return t.length>0?t:void 0}function Mn(n){let t=Math.min(32,n.length),r=0;while(r<t&&n[r]!==0)r+=1;if(r===0)return"";return H(T(n.subarray(0,r),"windows-1252"))}function b(n){if(n===65001||n===65000)return"utf-8";if(n===932)return"shift_jis";if(n===950)return"big5";if(n===949)return"euc-kr";if(n===936)return"gbk";return"windows-1252"}function T(n,t){try{return gn(n,t)}catch{return new TextDecoder("utf-8",{fatal:!1}).decode(n)}}function Nn(n){return n.includes("\x00")?n.replace(/\0+/g,""):n}function j(n){if(n.drmOffset===0||n.drmOffset===w)return!1;return n.drmCount!==0&&n.drmCount!==w}function Pn(n){if(n.length<B+8)return!1;if(p(n,76)<1)return!1;let r=h(n,B);return r+20<=n.length&&k(n,r+16,"MOBI")}function Tn(n){let t=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)t=3;while(t<n.length){let r=n[t];if(r!==9&&r!==10&&r!==13&&r!==32)break;t+=1}return t+5<=n.length&&n[t]===37&&n[t+1]===80&&n[t+2]===68&&n[t+3]===70&&n[t+4]===45}function In(n){return n.length>=8&&n[0]===208&&n[1]===207&&n[2]===17&&n[3]===224&&n[4]===161&&n[5]===177&&n[6]===26&&n[7]===225}function Ln(n){return n.length>=4&&n[0]===80&&n[1]===75&&(n[2]===3||n[2]===5||n[2]===7)&&(n[3]===4||n[3]===6||n[3]===8)}var Rn=new Set(["mobi","azw","azw3","prc"]);function E(){return{id:"mobi",sniff(n,t){if(z(n))return 2;if(t?.path!==void 0&&Rn.has(Dn(t.path)??""))return 1;return 0},convert(n){return Sn(n),{markdown:y(n)}}}}function Sn(n){if(En(n))throw S.unsupported("pdf");if(Fn(n))throw S.unsupported("ole");if(Xn(n))throw S.unsupported("zip")}function En(n){let t=_n(n);return Wn(n,"%PDF-",t)}function Fn(n){return n.length>=8&&n[0]===208&&n[1]===207&&n[2]===17&&n[3]===224&&n[4]===161&&n[5]===177&&n[6]===26&&n[7]===225}function Xn(n){if(n.length<4||n[0]!==80||n[1]!==75)return!1;return n[2]===3&&n[3]===4||n[2]===5&&n[3]===6||n[2]===7&&n[3]===8}function _n(n){let t=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)t=3;while(t<n.length){let r=n[t];if(r!==9&&r!==10&&r!==13&&r!==32)break;t+=1}return t}function Wn(n,t,r){if(r+t.length>n.length)return!1;for(let e=0;e<t.length;e+=1)if(n[r+e]!==t.charCodeAt(e))return!1;return!0}import{create as Zn}from"@mdgate/core";var $n=Zn([E()]);function Cn(n,t){return $n(n,t)}export{Cn as toMarkdown,E as mobi};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/mobi",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "mdgate MOBI / AZW3 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/html": "0.5.2",
29
- "@mdgate/utils": "0.5.2"
25
+ "@mdgate/containers": "0.5.4",
26
+ "@mdgate/core": "0.5.4",
27
+ "@mdgate/document": "0.5.4",
28
+ "@mdgate/html": "0.5.4",
29
+ "@mdgate/utils": "0.5.4"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"