react-book-reader 1.1.4 → 1.1.6
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/README.md +2 -2
- package/lib/{epub-CiyU4mPz.js → epub-vizLT02-.js} +1 -1
- package/lib/{pdf-BboyxV4m.js → pdf-D5_pBBCO.js} +20 -20
- package/lib/react-book-reader.es.js +1 -1
- package/lib/react-book-reader.umd.js +1 -1
- package/lib/{view-C6hqGjnD.js → view-Ht0IwIBs.js} +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -55,8 +55,8 @@ export default () => (
|
|
|
55
55
|
|
|
56
56
|
## ReactReader props passed to inner BookView
|
|
57
57
|
|
|
58
|
-
| **Name** | **Description** | **Type** |
|
|
59
|
-
| ----------- |
|
|
58
|
+
| **Name** | **Description** | **Type** | **Default** |
|
|
59
|
+
| ----------- | ----------------------------------------- | --------------------------------- | ---------------- |
|
|
60
60
|
| url | book url or File | `string`/`File` | |
|
|
61
61
|
| tocChanged | when the reader has parsed the book you will receive an array of the chapters | `function(toc)` |
|
|
62
62
|
| nextPage | display next page | `function` |
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
const h = async (e,
|
|
1
|
+
const h = async (e, i, t) => {
|
|
2
2
|
const o = t * devicePixelRatio;
|
|
3
|
-
|
|
3
|
+
i.documentElement.style.transform = `scale(${1 / devicePixelRatio})`, i.documentElement.style.transformOrigin = "top left", i.documentElement.style.setProperty("--scale-factor", o);
|
|
4
4
|
const n = e.getViewport({ scale: o }), s = document.createElement("canvas");
|
|
5
5
|
s.height = n.height, s.width = n.width;
|
|
6
6
|
const g = s.getContext("2d");
|
|
7
|
-
await e.render({ canvasContext: g, viewport: n }).promise,
|
|
8
|
-
const c =
|
|
7
|
+
await e.render({ canvasContext: g, viewport: n }).promise, i.querySelector("#canvas").replaceChildren(i.adoptNode(s));
|
|
8
|
+
const c = i.querySelector(".textLayer");
|
|
9
9
|
await new pdfjsLib.TextLayer({
|
|
10
10
|
textContentSource: await e.streamTextContent(),
|
|
11
11
|
container: c,
|
|
@@ -22,7 +22,7 @@ const h = async (e, r, t) => {
|
|
|
22
22
|
});
|
|
23
23
|
const a = document.createElement("div");
|
|
24
24
|
a.className = "endOfContent", c.append(a), c.onpointerdown = () => c.classList.add("selecting"), c.onpointerup = () => c.classList.remove("selecting");
|
|
25
|
-
const d =
|
|
25
|
+
const d = i.querySelector(".annotationLayer");
|
|
26
26
|
await new pdfjsLib.AnnotationLayer({ page: e, viewport: n, div: d }).render({
|
|
27
27
|
annotations: await e.getAnnotations(),
|
|
28
28
|
linkService: {
|
|
@@ -32,9 +32,9 @@ const h = async (e, r, t) => {
|
|
|
32
32
|
addLinkAttributes: (l, p) => l.href = p
|
|
33
33
|
}
|
|
34
34
|
});
|
|
35
|
-
}, u = async (e,
|
|
35
|
+
}, u = async (e, i) => {
|
|
36
36
|
const t = e.getViewport({ scale: 1 });
|
|
37
|
-
if (
|
|
37
|
+
if (i) {
|
|
38
38
|
const s = document.createElement("canvas");
|
|
39
39
|
s.height = t.height, s.width = t.width;
|
|
40
40
|
const g = s.getContext("2d");
|
|
@@ -60,16 +60,16 @@ const h = async (e, r, t) => {
|
|
|
60
60
|
href: JSON.stringify(e.dest),
|
|
61
61
|
subitems: e.items.length ? e.items.map(m) : null
|
|
62
62
|
}), y = async (e) => {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
e.slice(
|
|
66
|
-
|
|
63
|
+
const i = new pdfjsLib.PDFDataRangeTransport(e.size, []);
|
|
64
|
+
i.requestDataRange = (r, a) => {
|
|
65
|
+
e.slice(r, a).arrayBuffer().then((d) => {
|
|
66
|
+
i.onDataRange(r, d);
|
|
67
67
|
});
|
|
68
68
|
};
|
|
69
69
|
const t = await pdfjsLib.getDocument({
|
|
70
|
-
range:
|
|
71
|
-
cMapUrl: pdfjsPath(
|
|
72
|
-
standardFontDataUrl: pdfjsPath(
|
|
70
|
+
range: i,
|
|
71
|
+
// cMapUrl: pdfjsPath('cmaps/'),
|
|
72
|
+
// standardFontDataUrl: pdfjsPath('standard_fonts/'),
|
|
73
73
|
isEvalSupported: !1
|
|
74
74
|
}).promise, o = { rendition: { layout: "pre-paginated" } }, { metadata: n, info: s } = await t.getMetadata() ?? {};
|
|
75
75
|
o.metadata = {
|
|
@@ -87,7 +87,7 @@ const h = async (e, r, t) => {
|
|
|
87
87
|
const g = await t.getOutline();
|
|
88
88
|
o.toc = g?.map(m);
|
|
89
89
|
const c = /* @__PURE__ */ new Map();
|
|
90
|
-
return o.sections = Array.from({ length: t.numPages }).map((
|
|
90
|
+
return o.sections = Array.from({ length: t.numPages }).map((r, a) => ({
|
|
91
91
|
id: a,
|
|
92
92
|
load: async () => {
|
|
93
93
|
const d = c.get(a);
|
|
@@ -96,13 +96,13 @@ const h = async (e, r, t) => {
|
|
|
96
96
|
return c.set(a, l), l;
|
|
97
97
|
},
|
|
98
98
|
size: 1e3
|
|
99
|
-
})), o.isExternal = (
|
|
100
|
-
const a = JSON.parse(
|
|
99
|
+
})), o.isExternal = (r) => /^\w+:/i.test(r), o.resolveHref = async (r) => {
|
|
100
|
+
const a = JSON.parse(r), d = typeof a == "string" ? await t.getDestination(a) : a;
|
|
101
101
|
return { index: await t.getPageIndex(d[0]) };
|
|
102
|
-
}, o.splitTOCHref = async (
|
|
103
|
-
const a = JSON.parse(
|
|
102
|
+
}, o.splitTOCHref = async (r) => {
|
|
103
|
+
const a = JSON.parse(r), d = typeof a == "string" ? await t.getDestination(a) : a;
|
|
104
104
|
return [await t.getPageIndex(d[0]), null];
|
|
105
|
-
}, o.getTOCFragment = (
|
|
105
|
+
}, o.getTOCFragment = (r) => r.documentElement, o.getCover = async () => u(await t.getPage(1), !0), o.destroy = () => t.destroy(), o;
|
|
106
106
|
};
|
|
107
107
|
export {
|
|
108
108
|
y as makePDF
|
|
@@ -1638,7 +1638,7 @@ const eo = ii((e, t) => {
|
|
|
1638
1638
|
}, A = ({ detail: q }) => {
|
|
1639
1639
|
u && u(q);
|
|
1640
1640
|
}, k = () => h.current.next(), H = () => h.current.prev(), L = (q) => h.current.goTo(q);
|
|
1641
|
-
return oi(t, () => ({ prevPage: H, nextPage: k, setLocation: L })), E && import("./view-
|
|
1641
|
+
return oi(t, () => ({ prevPage: H, nextPage: k, setLocation: L })), E && import("./view-Ht0IwIBs.js").then((q) => q.v).then(() => {
|
|
1642
1642
|
S(!1);
|
|
1643
1643
|
}), ui(() => {
|
|
1644
1644
|
E || y();
|
|
@@ -79,7 +79,7 @@ body:not(.notesBodyType) > .title, body:not(.notesBodyType) > .epigraph {
|
|
|
79
79
|
<div id="canvas"></div>
|
|
80
80
|
<div class="textLayer"></div>
|
|
81
81
|
<div class="annotationLayer"></div>
|
|
82
|
-
`],{type:"text/html"})),onZoom:({doc:s,scale:a})=>Zh(n,s,a)}},fc=n=>({label:n.title,href:JSON.stringify(n.dest),subitems:n.items.length?n.items.map(fc):null}),Qh=Object.freeze(Object.defineProperty({__proto__:null,makePDF:async n=>{const e=new pdfjsLib.PDFDataRangeTransport(n.size,[]);e.requestDataRange=(c,l)=>{n.slice(c,l).arrayBuffer().then(u=>{e.onDataRange(c,u)})};const t=await pdfjsLib.getDocument({range:e,cMapUrl:pdfjsPath("cmaps/"),standardFontDataUrl:pdfjsPath("standard_fonts/"),isEvalSupported:!1}).promise,r={rendition:{layout:"pre-paginated"}},{metadata:i,info:s}=await t.getMetadata()??{};r.metadata={title:i?.get("dc:title")??s?.Title,author:i?.get("dc:creator")??s?.Author,contributor:i?.get("dc:contributor"),description:i?.get("dc:description")??s?.Subject,language:i?.get("dc:language"),publisher:i?.get("dc:publisher"),subject:i?.get("dc:subject"),identifier:i?.get("dc:identifier"),source:i?.get("dc:source"),rights:i?.get("dc:rights")};const a=await t.getOutline();r.toc=a?.map(fc);const o=new Map;return r.sections=Array.from({length:t.numPages}).map((c,l)=>({id:l,load:async()=>{const u=o.get(l);if(u)return u;const h=await hc(await t.getPage(l+1));return o.set(l,h),h},size:1e3})),r.isExternal=c=>/^\w+:/i.test(c),r.resolveHref=async c=>{const l=JSON.parse(c),u=typeof l=="string"?await t.getDestination(l):l;return{index:await t.getPageIndex(u[0])}},r.splitTOCHref=async c=>{const l=JSON.parse(c),u=typeof l=="string"?await t.getDestination(l):l;return[await t.getPageIndex(u[0]),null]},r.getTOCFragment=c=>c.documentElement,r.getCover=async()=>hc(await t.getPage(1),!0),r.destroy=()=>t.destroy(),r}},Symbol.toStringTag,{value:"Module"})),et=n=>{if(!n)return"";const e=document.createElement("textarea");return e.innerHTML=n,e.value},qe={XML:"application/xml",XHTML:"application/xhtml+xml",HTML:"text/html",CSS:"text/css",SVG:"image/svg+xml"},ef={name:[0,32,"string"],type:[60,4,"string"],creator:[64,4,"string"],numRecords:[76,2,"uint"]},tf={compression:[0,2,"uint"],numTextRecords:[8,2,"uint"],recordSize:[10,2,"uint"],encryption:[12,2,"uint"]},rf={magic:[16,4,"string"],length:[20,4,"uint"],type:[24,4,"uint"],encoding:[28,4,"uint"],uid:[32,4,"uint"],version:[36,4,"uint"],titleOffset:[84,4,"uint"],titleLength:[88,4,"uint"],localeRegion:[94,1,"uint"],localeLanguage:[95,1,"uint"],resourceStart:[108,4,"uint"],huffcdic:[112,4,"uint"],numHuffcdic:[116,4,"uint"],exthFlag:[128,4,"uint"],trailingFlags:[240,4,"uint"],indx:[244,4,"uint"]},nf={resourceStart:[108,4,"uint"],fdst:[192,4,"uint"],numFdst:[196,4,"uint"],frag:[248,4,"uint"],skel:[252,4,"uint"],guide:[260,4,"uint"]},sf={magic:[0,4,"string"],length:[4,4,"uint"],count:[8,4,"uint"]},dc={magic:[0,4,"string"],length:[4,4,"uint"],type:[8,4,"uint"],idxt:[20,4,"uint"],numRecords:[24,4,"uint"],encoding:[28,4,"uint"],language:[32,4,"uint"],total:[36,4,"uint"],ordt:[40,4,"uint"],ligt:[44,4,"uint"],numLigt:[48,4,"uint"],numCncx:[52,4,"uint"]},af={magic:[0,4,"string"],length:[4,4,"uint"],numControlBytes:[8,4,"uint"]},of={magic:[0,4,"string"],offset1:[8,4,"uint"],offset2:[12,4,"uint"]},cf={magic:[0,4,"string"],length:[4,4,"uint"],numEntries:[8,4,"uint"],codeLength:[12,4,"uint"]},lf={magic:[0,4,"string"],numEntries:[8,4,"uint"]},uf={flags:[8,4,"uint"],dataStart:[12,4,"uint"],keyLength:[16,4,"uint"],keyStart:[20,4,"uint"]},hf={1252:"windows-1252",65001:"utf-8"},pc={100:["creator","string",!0],101:["publisher"],103:["description"],104:["isbn"],105:["subject","string",!0],106:["date"],108:["contributor","string",!0],109:["rights"],110:["subjectCode","string",!0],112:["source","string",!0],113:["asin"],121:["boundary","uint"],122:["fixedLayout"],125:["numResources","uint"],126:["originalResolution"],127:["zeroGutter"],128:["zeroMargin"],129:["coverURI"],132:["regionMagnification"],201:["coverOffset","uint"],202:["thumbnailOffset","uint"],503:["title"],524:["language","string",!0],527:["pageProgressionDirection"]},ff={1:["ar","ar-SA","ar-IQ","ar-EG","ar-LY","ar-DZ","ar-MA","ar-TN","ar-OM","ar-YE","ar-SY","ar-JO","ar-LB","ar-KW","ar-AE","ar-BH","ar-QA"],2:["bg"],3:["ca"],4:["zh","zh-TW","zh-CN","zh-HK","zh-SG"],5:["cs"],6:["da"],7:["de","de-DE","de-CH","de-AT","de-LU","de-LI"],8:["el"],9:["en","en-US","en-GB","en-AU","en-CA","en-NZ","en-IE","en-ZA","en-JM",null,"en-BZ","en-TT","en-ZW","en-PH"],10:["es","es-ES","es-MX",null,"es-GT","es-CR","es-PA","es-DO","es-VE","es-CO","es-PE","es-AR","es-EC","es-CL","es-UY","es-PY","es-BO","es-SV","es-HN","es-NI","es-PR"],11:["fi"],12:["fr","fr-FR","fr-BE","fr-CA","fr-CH","fr-LU","fr-MC"],13:["he"],14:["hu"],15:["is"],16:["it","it-IT","it-CH"],17:["ja"],18:["ko"],19:["nl","nl-NL","nl-BE"],20:["no","nb","nn"],21:["pl"],22:["pt","pt-BR","pt-PT"],23:["rm"],24:["ro"],25:["ru"],26:["hr",null,"sr"],27:["sk"],28:["sq"],29:["sv","sv-SE","sv-FI"],30:["th"],31:["tr"],32:["ur"],33:["id"],34:["uk"],35:["be"],36:["sl"],37:["et"],38:["lv"],39:["lt"],41:["fa"],42:["vi"],43:["hy"],44:["az"],45:["eu"],46:["hsb"],47:["mk"],48:["st"],49:["ts"],50:["tn"],52:["xh"],53:["zu"],54:["af"],55:["ka"],56:["fo"],57:["hi"],58:["mt"],59:["se"],62:["ms"],63:["kk"],65:["sw"],67:["uz",null,"uz-UZ"],68:["tt"],69:["bn"],70:["pa"],71:["gu"],72:["or"],73:["ta"],74:["te"],75:["kn"],76:["ml"],77:["as"],78:["mr"],79:["sa"],82:["cy","cy-GB"],83:["gl","gl-ES"],87:["kok"],97:["ne"],98:["fy"]},pi=(n,e)=>{const t=new n.constructor(n.length+e.length);return t.set(n),t.set(e,n.length),t},gc=(n,e,t)=>{const r=new n.constructor(n.length+e.length+t.length);return r.set(n),r.set(e,n.length),r.set(t,n.length+e.length),r},df=new TextDecoder,Ct=n=>df.decode(n),ue=n=>{if(!n)return;const e=n.byteLength,t=e===4?"getUint32":e===2?"getUint16":"getUint8";return new DataView(n)[t](0)},we=(n,e)=>Object.fromEntries(Array.from(Object.entries(n)).map(([t,[r,i,s]])=>[t,(s==="string"?Ct:ue)(e.slice(r,r+i))])),gi=n=>new TextDecoder(hf[n]),rr=(n,e=0)=>{let t=0,r=0;for(const i of n.subarray(e,e+4))if(t=t<<7|(i&127)>>>0,r++,i&128)break;return{value:t,length:r}},pf=n=>{let e=0;for(const t of n.subarray(-4))t&128&&(e=0),e=e<<7|t&127;return e},mc=n=>{let e=0;for(;n>0;n=n>>1)(n&1)===1&&e++;return e},gf=n=>{let e=0;for(;(n&1)===0;)n=n>>1,e++;return e},mf=n=>{let e=[];for(let t=0;t<n.length;t++){const r=n[t];if(r===0)e.push(0);else if(r<=8)for(const i of n.subarray(t+1,(t+=r)+1))e.push(i);else if(r<=127)e.push(r);else if(r<=191){const i=r<<8|n[t+++1],s=(i&16383)>>>3,a=(i&7)+3;for(let o=0;o<a;o++)e.push(e[e.length-s])}else e.push(32,r^128)}return Uint8Array.from(e)},bf=(n,e)=>{const t=e>>3,r=e+32,i=r>>3;let s=0n;for(let a=t;a<=i;a++)s=s<<8n|BigInt(n[a]??0);return s>>8n-BigInt(r&7)&0xffffffffn},yf=async(n,e)=>{const t=await e(n.huffcdic),{magic:r,offset1:i,offset2:s}=we(of,t);if(r!=="HUFF")throw new Error("Invalid HUFF record");const a=Array.from({length:256},(u,h)=>i+h*4).map(u=>ue(t.slice(u,u+4))).map(u=>[u&128,u&31,u>>>8]),o=[null].concat(Array.from({length:32},(u,h)=>s+h*8).map(u=>[ue(t.slice(u,u+4)),ue(t.slice(u+4,u+8))])),c=[];for(let u=1;u<n.numHuffcdic;u++){const h=await e(n.huffcdic+u),p=we(cf,h);if(p.magic!=="CDIC")throw new Error("Invalid CDIC record");const g=Math.min(1<<p.codeLength,p.numEntries-c.length),f=h.slice(p.length);for(let b=0;b<g;b++){const d=ue(f.slice(b*2,b*2+2)),m=ue(f.slice(d,d+2)),y=m&32767,w=m&32768,S=new Uint8Array(f.slice(d+2,d+2+y));c.push([S,w])}}const l=u=>{let h=new Uint8Array;const p=u.byteLength*8;for(let g=0;g<p;){const f=Number(bf(u,g));let[b,d,m]=a[f>>>24];if(!b){for(;f>>>32-d<o[d][0];)d+=1;m=o[d][1]}if((g+=d)>p)break;const y=m-(f>>>32-d);let[w,S]=c[y];S||(w=l(w),c[y]=[w,!0]),h=pi(h,w)}return h};return l},nr=async(n,e)=>{const t=await e(n),r=we(dc,t);if(r.magic!=="INDX")throw new Error("Invalid INDX record");const i=gi(r.encoding),s=t.slice(r.length),a=we(af,s);if(a.magic!=="TAGX")throw new Error("Invalid TAGX section");const o=(a.length-12)/4,c=Array.from({length:o},(p,g)=>new Uint8Array(s.slice(12+g*4,12+g*4+4))),l={};let u=0;for(let p=0;p<r.numCncx;p++){const g=await e(n+r.numRecords+p+1),f=new Uint8Array(g);for(let b=0;b<f.byteLength;){const d=b,{value:m,length:y}=rr(f,b);b+=y;const w=g.slice(b,b+m);b+=m,l[u+d]=i.decode(w)}u+=65536}const h=[];for(let p=0;p<r.numRecords;p++){const g=await e(n+1+p),f=new Uint8Array(g),b=we(dc,g);if(b.magic!=="INDX")throw new Error("Invalid INDX record");for(let d=0;d<b.numRecords;d++){const m=b.idxt+4+2*d,y=ue(g.slice(m,m+2)),w=ue(g.slice(y,y+1)),S=Ct(g.slice(y+1,y+1+w)),R=[],T=y+1+w;let _=0,x=T+a.numControlBytes;for(const[k,E,O,I]of c){if(I&1){_++;continue}const L=T+_,P=ue(g.slice(L,L+1))&O;if(P===O)if(mc(O)>1){const{value:j,length:F}=rr(f,x);R.push([k,null,j,E]),x+=F}else R.push([k,1,null,E]);else R.push([k,P>>gf(O),null,E])}const A={};for(const[k,E,O,I]of R){const L=[];if(E!=null)for(let P=0;P<E*I;P++){const{value:j,length:F}=rr(f,x);L.push(j),x+=F}else{let P=0;for(;P<O;){const{value:j,length:F}=rr(f,x);L.push(j),x+=F,P+=F}}A[k]=L}h.push({name:S,tagMap:A})}}return{table:h,cncx:l}},vf=async(n,e)=>{const{table:t,cncx:r}=await nr(n,e),i=t.map(({tagMap:a},o)=>({index:o,offset:a[1]?.[0],size:a[2]?.[0],label:r[a[3]]??"",headingLevel:a[4]?.[0],pos:a[6],parent:a[21]?.[0],firstChild:a[22]?.[0],lastChild:a[23]?.[0]})),s=a=>(a.firstChild==null||(a.children=i.filter(o=>o.parent===a.index).map(s)),a);return i.filter(a=>a.headingLevel===0).map(s)},wf=(n,e)=>{const{magic:t,count:r}=we(sf,n);if(t!=="EXTH")throw new Error("Invalid EXTH header");const i=gi(e),s={};let a=12;for(let o=0;o<r;o++){const c=ue(n.slice(a,a+4)),l=ue(n.slice(a+4,a+8));if(c in pc){const[u,h,p]=pc[c],g=n.slice(a+8,a+l),f=h==="uint"?ue(g):i.decode(g);p?(s[u]??=[],s[u].push(f)):s[u]=f}a+=l}return s},xf=async(n,e)=>{const{flags:t,dataStart:r,keyLength:i,keyStart:s}=we(uf,n),a=new Uint8Array(n.slice(r));if(t&2){const c=i===16?1024:1040,l=new Uint8Array(n.slice(s,s+i)),u=Math.min(c,a.length);for(var o=0;o<u;o++)a[o]=a[o]^l[o%l.length]}if(t&1)try{return await e(a)}catch(c){console.warn(c),console.warn("Failed to decompress font")}return a},_f=async n=>Ct(await n.slice(60,68).arrayBuffer())==="BOOKMOBI";class Sf{#e;#r;pdb;async open(e){this.#e=e;const t=we(ef,await e.slice(0,78).arrayBuffer());this.pdb=t;const r=await e.slice(78,78+t.numRecords*8).arrayBuffer();this.#r=Array.from({length:t.numRecords},(i,s)=>ue(r.slice(s*8,s*8+4))).map((i,s,a)=>[i,a[s+1]])}loadRecord(e){const t=this.#r[e];if(!t)throw new RangeError("Record index out of bounds");return this.#e.slice(...t).arrayBuffer()}async loadMagic(e){const t=this.#r[e][0];return Ct(await this.#e.slice(t,t+4).arrayBuffer())}}class Ef extends Sf{#e=0;#r;#t;#n;#i;#s;constructor({unzlib:e}){super(),this.unzlib=e}async open(e){await super.open(e),this.headers=this.#o(await super.loadRecord(0)),this.#r=this.headers.mobi.resourceStart;let t=this.headers.mobi.version>=8;if(!t){const r=this.headers.exth?.boundary;if(r<4294967295)try{this.headers=this.#o(await super.loadRecord(r)),this.#e=r,t=!0}catch(i){console.warn(i),console.warn("Failed to open KF8; falling back to MOBI")}}return await this.#a(),t?new Pf(this).init():new Of(this).init()}#o(e){const t=we(tf,e),r=we(rf,e);if(r.magic!=="MOBI")throw new Error("Missing MOBI header");const{titleOffset:i,titleLength:s,localeLanguage:a,localeRegion:o}=r;r.title=e.slice(i,i+s);const c=ff[a];r.language=c?.[o>>2]??c?.[0];const l=r.exthFlag&64?wf(e.slice(r.length+16),r.encoding):null,u=r.version>=8?we(nf,e):null;return{palmdoc:t,mobi:r,exth:l,kf8:u}}async#a(){const{palmdoc:e,mobi:t}=this.headers;this.#t=gi(t.encoding),this.#n=new TextEncoder;const{compression:r}=e;if(this.#i=r===1?o=>o:r===2?mf:r===17480?await yf(t,this.loadRecord.bind(this)):null,!this.#i)throw new Error("Unknown compression type");const{trailingFlags:i}=t,s=i&1,a=mc(i>>>1);this.#s=o=>{for(let c=0;c<a;c++){const l=pf(o);o=o.subarray(0,-l)}if(s){const c=(o[o.length-1]&3)+1;o=o.subarray(0,-c)}return o}}decode(...e){return this.#t.decode(...e)}encode(...e){return this.#n.encode(...e)}loadRecord(e){return super.loadRecord(this.#e+e)}loadMagic(e){return super.loadMagic(this.#e+e)}loadText(e){return this.loadRecord(e+1).then(t=>new Uint8Array(t)).then(this.#s).then(this.#i)}async loadResource(e){const t=await super.loadRecord(this.#r+e),r=Ct(t.slice(0,4));return r==="FONT"?xf(t,this.unzlib):r==="VIDE"||r==="AUDI"?t.slice(12):t}getNCX(){const e=this.headers.mobi.indx;if(e<4294967295)return vf(e,this.loadRecord.bind(this))}getMetadata(){const{mobi:e,exth:t}=this.headers;return{identifier:e.uid.toString(),title:et(t?.title||this.decode(e.title)),author:t?.creator?.map(et),publisher:et(t?.publisher),language:t?.language??e.language,published:t?.date,description:et(t?.description),subject:t?.subject?.map(et),rights:et(t?.rights),contributor:t?.contributor}}async getCover(){const{exth:e}=this.headers,t=e?.coverOffset<4294967295?e?.coverOffset:e?.thumbnailOffset<4294967295?e?.thumbnailOffset:null;if(t!=null){const r=await this.loadResource(t);return new Blob([r])}}}const bc=/<\s*(?:mbp:)?pagebreak[^>]*>/gi,Tf=/<[^<>]+filepos=['"]{0,1}(\d+)[^<>]*>/gi,Rf=n=>{let e=0;for(;n;){const t=n.parentElement;if(t){const r=t.tagName.toLowerCase();r==="p"?e+=1.5:r==="blockquote"&&(e+=2)}n=t}return e};function kf(n){let t="";for(let r=0;r<n.length;r+=32768)t+=String.fromCharCode.apply(null,n.subarray(r,r+32768));return t}class Of{parser=new DOMParser;serializer=new XMLSerializer;#e=new Map;#r=new Map;#t=new Map;#n;#i=[];#s=qe.HTML;constructor(e){this.mobi=e}async init(){const e=[];for(let s=0;s<this.mobi.headers.palmdoc.numTextRecords;s++){const a=await this.mobi.loadText(s);e.push(a)}const t=e.reduce((s,a)=>s+a.byteLength,0),r=new Uint8Array(t);e.reduce((s,a)=>(r.set(new Uint8Array(a),s),s+a.byteLength),0);const i=kf(r);this.#n=[0].concat(Array.from(i.matchAll(bc),s=>s.index)).map((s,a,o)=>{const c=o[a+1]??r.length;return{book:this,raw:r.subarray(s,c)}}).map((s,a,o)=>(s.start=o[a-1]?.end??0,s.end=s.start+s.raw.byteLength,s)),this.sections=this.#n.map((s,a)=>({id:a,load:()=>this.loadSection(s),createDocument:()=>this.createDocument(s),size:s.end-s.start}));try{this.landmarks=await this.getGuide();const s=this.landmarks.find(({type:a})=>a?.includes("toc"))?.href;if(s){const{index:a}=this.resolveHref(s),o=await this.sections[a].createDocument();let c,l=0,u=0;const h=new Map,p=new Map;this.toc=Array.from(o.querySelectorAll("a[filepos]")).reduce((g,f)=>{const b=Rf(f),d={label:f.innerText?.trim()??"",href:`filepos:${f.getAttribute("filepos")}`},m=b>u?l+1:b===u?l:h.get(b)??Math.max(0,l-1);if(m>l)c?(c.subitems??=[],c.subitems.push(d),p.set(m,c)):g.push(d);else{const y=p.get(m);y?y.subitems.push(d):g.push(d)}return c=d,l=m,u=b,h.set(b,m),g},[])}}catch(s){console.warn(s)}return this.#i=[...new Set(Array.from(i.matchAll(Tf),s=>s[1]))].map(s=>({filepos:s,number:Number(s)})).sort((s,a)=>s.number-a.number),this.metadata=this.mobi.getMetadata(),this.getCover=this.mobi.getCover.bind(this.mobi),this}async getGuide(){const e=await this.createDocument(this.#n[0]);return Array.from(e.getElementsByTagName("reference"),t=>({label:t.getAttribute("title"),type:t.getAttribute("type")?.split(/\s/),href:`filepos:${t.getAttribute("filepos")}`}))}async loadResource(e){if(this.#e.has(e))return this.#e.get(e);const t=await this.mobi.loadResource(e),r=URL.createObjectURL(new Blob([t]));return this.#e.set(e,r),r}async loadRecindex(e){return this.loadResource(Number(e)-1)}async replaceResources(e){for(const t of e.querySelectorAll("img[recindex]")){const r=t.getAttribute("recindex");try{t.src=await this.loadRecindex(r)}catch{console.warn(`Failed to load image ${r}`)}}for(const t of e.querySelectorAll("[mediarecindex]")){const r=t.getAttribute("mediarecindex"),i=t.getAttribute("recindex");try{t.src=await this.loadRecindex(r),i&&(t.poster=await this.loadRecindex(i))}catch{console.warn(`Failed to load media ${r}`)}}for(const t of e.querySelectorAll("[filepos]")){const r=t.getAttribute("filepos");t.href=`filepos:${r}`}}async loadText(e){if(this.#r.has(e))return this.#r.get(e);const{raw:t}=e,r=this.#i.filter(({number:a})=>a>=e.start&&a<e.end).map(a=>({...a,offset:a.number-e.start}));let i=t;r.length&&(i=t.subarray(0,r[0].offset),r.forEach(({filepos:a,offset:o},c)=>{const l=r[c+1],u=this.mobi.encode(`<a id="filepos${a}"></a>`);i=gc(i,u,t.subarray(o,l?.offset))}));const s=this.mobi.decode(i).replaceAll(bc,"");return this.#r.set(e,s),s}async createDocument(e){const t=await this.loadText(e);return this.parser.parseFromString(t,this.#s)}async loadSection(e){if(this.#t.has(e))return this.#t.get(e);const t=await this.createDocument(e),r=t.createElement("style");t.head.append(r),r.append(t.createTextNode(`blockquote {
|
|
82
|
+
`],{type:"text/html"})),onZoom:({doc:s,scale:a})=>Zh(n,s,a)}},fc=n=>({label:n.title,href:JSON.stringify(n.dest),subitems:n.items.length?n.items.map(fc):null}),Qh=Object.freeze(Object.defineProperty({__proto__:null,makePDF:async n=>{const e=new pdfjsLib.PDFDataRangeTransport(n.size,[]);e.requestDataRange=(c,l)=>{n.slice(c,l).arrayBuffer().then(u=>{e.onDataRange(c,u)})};const t=await pdfjsLib.getDocument({range:e,isEvalSupported:!1}).promise,r={rendition:{layout:"pre-paginated"}},{metadata:i,info:s}=await t.getMetadata()??{};r.metadata={title:i?.get("dc:title")??s?.Title,author:i?.get("dc:creator")??s?.Author,contributor:i?.get("dc:contributor"),description:i?.get("dc:description")??s?.Subject,language:i?.get("dc:language"),publisher:i?.get("dc:publisher"),subject:i?.get("dc:subject"),identifier:i?.get("dc:identifier"),source:i?.get("dc:source"),rights:i?.get("dc:rights")};const a=await t.getOutline();r.toc=a?.map(fc);const o=new Map;return r.sections=Array.from({length:t.numPages}).map((c,l)=>({id:l,load:async()=>{const u=o.get(l);if(u)return u;const h=await hc(await t.getPage(l+1));return o.set(l,h),h},size:1e3})),r.isExternal=c=>/^\w+:/i.test(c),r.resolveHref=async c=>{const l=JSON.parse(c),u=typeof l=="string"?await t.getDestination(l):l;return{index:await t.getPageIndex(u[0])}},r.splitTOCHref=async c=>{const l=JSON.parse(c),u=typeof l=="string"?await t.getDestination(l):l;return[await t.getPageIndex(u[0]),null]},r.getTOCFragment=c=>c.documentElement,r.getCover=async()=>hc(await t.getPage(1),!0),r.destroy=()=>t.destroy(),r}},Symbol.toStringTag,{value:"Module"})),et=n=>{if(!n)return"";const e=document.createElement("textarea");return e.innerHTML=n,e.value},qe={XML:"application/xml",XHTML:"application/xhtml+xml",HTML:"text/html",CSS:"text/css",SVG:"image/svg+xml"},ef={name:[0,32,"string"],type:[60,4,"string"],creator:[64,4,"string"],numRecords:[76,2,"uint"]},tf={compression:[0,2,"uint"],numTextRecords:[8,2,"uint"],recordSize:[10,2,"uint"],encryption:[12,2,"uint"]},rf={magic:[16,4,"string"],length:[20,4,"uint"],type:[24,4,"uint"],encoding:[28,4,"uint"],uid:[32,4,"uint"],version:[36,4,"uint"],titleOffset:[84,4,"uint"],titleLength:[88,4,"uint"],localeRegion:[94,1,"uint"],localeLanguage:[95,1,"uint"],resourceStart:[108,4,"uint"],huffcdic:[112,4,"uint"],numHuffcdic:[116,4,"uint"],exthFlag:[128,4,"uint"],trailingFlags:[240,4,"uint"],indx:[244,4,"uint"]},nf={resourceStart:[108,4,"uint"],fdst:[192,4,"uint"],numFdst:[196,4,"uint"],frag:[248,4,"uint"],skel:[252,4,"uint"],guide:[260,4,"uint"]},sf={magic:[0,4,"string"],length:[4,4,"uint"],count:[8,4,"uint"]},dc={magic:[0,4,"string"],length:[4,4,"uint"],type:[8,4,"uint"],idxt:[20,4,"uint"],numRecords:[24,4,"uint"],encoding:[28,4,"uint"],language:[32,4,"uint"],total:[36,4,"uint"],ordt:[40,4,"uint"],ligt:[44,4,"uint"],numLigt:[48,4,"uint"],numCncx:[52,4,"uint"]},af={magic:[0,4,"string"],length:[4,4,"uint"],numControlBytes:[8,4,"uint"]},of={magic:[0,4,"string"],offset1:[8,4,"uint"],offset2:[12,4,"uint"]},cf={magic:[0,4,"string"],length:[4,4,"uint"],numEntries:[8,4,"uint"],codeLength:[12,4,"uint"]},lf={magic:[0,4,"string"],numEntries:[8,4,"uint"]},uf={flags:[8,4,"uint"],dataStart:[12,4,"uint"],keyLength:[16,4,"uint"],keyStart:[20,4,"uint"]},hf={1252:"windows-1252",65001:"utf-8"},pc={100:["creator","string",!0],101:["publisher"],103:["description"],104:["isbn"],105:["subject","string",!0],106:["date"],108:["contributor","string",!0],109:["rights"],110:["subjectCode","string",!0],112:["source","string",!0],113:["asin"],121:["boundary","uint"],122:["fixedLayout"],125:["numResources","uint"],126:["originalResolution"],127:["zeroGutter"],128:["zeroMargin"],129:["coverURI"],132:["regionMagnification"],201:["coverOffset","uint"],202:["thumbnailOffset","uint"],503:["title"],524:["language","string",!0],527:["pageProgressionDirection"]},ff={1:["ar","ar-SA","ar-IQ","ar-EG","ar-LY","ar-DZ","ar-MA","ar-TN","ar-OM","ar-YE","ar-SY","ar-JO","ar-LB","ar-KW","ar-AE","ar-BH","ar-QA"],2:["bg"],3:["ca"],4:["zh","zh-TW","zh-CN","zh-HK","zh-SG"],5:["cs"],6:["da"],7:["de","de-DE","de-CH","de-AT","de-LU","de-LI"],8:["el"],9:["en","en-US","en-GB","en-AU","en-CA","en-NZ","en-IE","en-ZA","en-JM",null,"en-BZ","en-TT","en-ZW","en-PH"],10:["es","es-ES","es-MX",null,"es-GT","es-CR","es-PA","es-DO","es-VE","es-CO","es-PE","es-AR","es-EC","es-CL","es-UY","es-PY","es-BO","es-SV","es-HN","es-NI","es-PR"],11:["fi"],12:["fr","fr-FR","fr-BE","fr-CA","fr-CH","fr-LU","fr-MC"],13:["he"],14:["hu"],15:["is"],16:["it","it-IT","it-CH"],17:["ja"],18:["ko"],19:["nl","nl-NL","nl-BE"],20:["no","nb","nn"],21:["pl"],22:["pt","pt-BR","pt-PT"],23:["rm"],24:["ro"],25:["ru"],26:["hr",null,"sr"],27:["sk"],28:["sq"],29:["sv","sv-SE","sv-FI"],30:["th"],31:["tr"],32:["ur"],33:["id"],34:["uk"],35:["be"],36:["sl"],37:["et"],38:["lv"],39:["lt"],41:["fa"],42:["vi"],43:["hy"],44:["az"],45:["eu"],46:["hsb"],47:["mk"],48:["st"],49:["ts"],50:["tn"],52:["xh"],53:["zu"],54:["af"],55:["ka"],56:["fo"],57:["hi"],58:["mt"],59:["se"],62:["ms"],63:["kk"],65:["sw"],67:["uz",null,"uz-UZ"],68:["tt"],69:["bn"],70:["pa"],71:["gu"],72:["or"],73:["ta"],74:["te"],75:["kn"],76:["ml"],77:["as"],78:["mr"],79:["sa"],82:["cy","cy-GB"],83:["gl","gl-ES"],87:["kok"],97:["ne"],98:["fy"]},pi=(n,e)=>{const t=new n.constructor(n.length+e.length);return t.set(n),t.set(e,n.length),t},gc=(n,e,t)=>{const r=new n.constructor(n.length+e.length+t.length);return r.set(n),r.set(e,n.length),r.set(t,n.length+e.length),r},df=new TextDecoder,Ct=n=>df.decode(n),ue=n=>{if(!n)return;const e=n.byteLength,t=e===4?"getUint32":e===2?"getUint16":"getUint8";return new DataView(n)[t](0)},we=(n,e)=>Object.fromEntries(Array.from(Object.entries(n)).map(([t,[r,i,s]])=>[t,(s==="string"?Ct:ue)(e.slice(r,r+i))])),gi=n=>new TextDecoder(hf[n]),rr=(n,e=0)=>{let t=0,r=0;for(const i of n.subarray(e,e+4))if(t=t<<7|(i&127)>>>0,r++,i&128)break;return{value:t,length:r}},pf=n=>{let e=0;for(const t of n.subarray(-4))t&128&&(e=0),e=e<<7|t&127;return e},mc=n=>{let e=0;for(;n>0;n=n>>1)(n&1)===1&&e++;return e},gf=n=>{let e=0;for(;(n&1)===0;)n=n>>1,e++;return e},mf=n=>{let e=[];for(let t=0;t<n.length;t++){const r=n[t];if(r===0)e.push(0);else if(r<=8)for(const i of n.subarray(t+1,(t+=r)+1))e.push(i);else if(r<=127)e.push(r);else if(r<=191){const i=r<<8|n[t+++1],s=(i&16383)>>>3,a=(i&7)+3;for(let o=0;o<a;o++)e.push(e[e.length-s])}else e.push(32,r^128)}return Uint8Array.from(e)},bf=(n,e)=>{const t=e>>3,r=e+32,i=r>>3;let s=0n;for(let a=t;a<=i;a++)s=s<<8n|BigInt(n[a]??0);return s>>8n-BigInt(r&7)&0xffffffffn},yf=async(n,e)=>{const t=await e(n.huffcdic),{magic:r,offset1:i,offset2:s}=we(of,t);if(r!=="HUFF")throw new Error("Invalid HUFF record");const a=Array.from({length:256},(u,h)=>i+h*4).map(u=>ue(t.slice(u,u+4))).map(u=>[u&128,u&31,u>>>8]),o=[null].concat(Array.from({length:32},(u,h)=>s+h*8).map(u=>[ue(t.slice(u,u+4)),ue(t.slice(u+4,u+8))])),c=[];for(let u=1;u<n.numHuffcdic;u++){const h=await e(n.huffcdic+u),p=we(cf,h);if(p.magic!=="CDIC")throw new Error("Invalid CDIC record");const g=Math.min(1<<p.codeLength,p.numEntries-c.length),f=h.slice(p.length);for(let b=0;b<g;b++){const d=ue(f.slice(b*2,b*2+2)),m=ue(f.slice(d,d+2)),y=m&32767,w=m&32768,S=new Uint8Array(f.slice(d+2,d+2+y));c.push([S,w])}}const l=u=>{let h=new Uint8Array;const p=u.byteLength*8;for(let g=0;g<p;){const f=Number(bf(u,g));let[b,d,m]=a[f>>>24];if(!b){for(;f>>>32-d<o[d][0];)d+=1;m=o[d][1]}if((g+=d)>p)break;const y=m-(f>>>32-d);let[w,S]=c[y];S||(w=l(w),c[y]=[w,!0]),h=pi(h,w)}return h};return l},nr=async(n,e)=>{const t=await e(n),r=we(dc,t);if(r.magic!=="INDX")throw new Error("Invalid INDX record");const i=gi(r.encoding),s=t.slice(r.length),a=we(af,s);if(a.magic!=="TAGX")throw new Error("Invalid TAGX section");const o=(a.length-12)/4,c=Array.from({length:o},(p,g)=>new Uint8Array(s.slice(12+g*4,12+g*4+4))),l={};let u=0;for(let p=0;p<r.numCncx;p++){const g=await e(n+r.numRecords+p+1),f=new Uint8Array(g);for(let b=0;b<f.byteLength;){const d=b,{value:m,length:y}=rr(f,b);b+=y;const w=g.slice(b,b+m);b+=m,l[u+d]=i.decode(w)}u+=65536}const h=[];for(let p=0;p<r.numRecords;p++){const g=await e(n+1+p),f=new Uint8Array(g),b=we(dc,g);if(b.magic!=="INDX")throw new Error("Invalid INDX record");for(let d=0;d<b.numRecords;d++){const m=b.idxt+4+2*d,y=ue(g.slice(m,m+2)),w=ue(g.slice(y,y+1)),S=Ct(g.slice(y+1,y+1+w)),R=[],T=y+1+w;let _=0,x=T+a.numControlBytes;for(const[k,E,O,I]of c){if(I&1){_++;continue}const L=T+_,P=ue(g.slice(L,L+1))&O;if(P===O)if(mc(O)>1){const{value:j,length:F}=rr(f,x);R.push([k,null,j,E]),x+=F}else R.push([k,1,null,E]);else R.push([k,P>>gf(O),null,E])}const A={};for(const[k,E,O,I]of R){const L=[];if(E!=null)for(let P=0;P<E*I;P++){const{value:j,length:F}=rr(f,x);L.push(j),x+=F}else{let P=0;for(;P<O;){const{value:j,length:F}=rr(f,x);L.push(j),x+=F,P+=F}}A[k]=L}h.push({name:S,tagMap:A})}}return{table:h,cncx:l}},vf=async(n,e)=>{const{table:t,cncx:r}=await nr(n,e),i=t.map(({tagMap:a},o)=>({index:o,offset:a[1]?.[0],size:a[2]?.[0],label:r[a[3]]??"",headingLevel:a[4]?.[0],pos:a[6],parent:a[21]?.[0],firstChild:a[22]?.[0],lastChild:a[23]?.[0]})),s=a=>(a.firstChild==null||(a.children=i.filter(o=>o.parent===a.index).map(s)),a);return i.filter(a=>a.headingLevel===0).map(s)},wf=(n,e)=>{const{magic:t,count:r}=we(sf,n);if(t!=="EXTH")throw new Error("Invalid EXTH header");const i=gi(e),s={};let a=12;for(let o=0;o<r;o++){const c=ue(n.slice(a,a+4)),l=ue(n.slice(a+4,a+8));if(c in pc){const[u,h,p]=pc[c],g=n.slice(a+8,a+l),f=h==="uint"?ue(g):i.decode(g);p?(s[u]??=[],s[u].push(f)):s[u]=f}a+=l}return s},xf=async(n,e)=>{const{flags:t,dataStart:r,keyLength:i,keyStart:s}=we(uf,n),a=new Uint8Array(n.slice(r));if(t&2){const c=i===16?1024:1040,l=new Uint8Array(n.slice(s,s+i)),u=Math.min(c,a.length);for(var o=0;o<u;o++)a[o]=a[o]^l[o%l.length]}if(t&1)try{return await e(a)}catch(c){console.warn(c),console.warn("Failed to decompress font")}return a},_f=async n=>Ct(await n.slice(60,68).arrayBuffer())==="BOOKMOBI";class Sf{#e;#r;pdb;async open(e){this.#e=e;const t=we(ef,await e.slice(0,78).arrayBuffer());this.pdb=t;const r=await e.slice(78,78+t.numRecords*8).arrayBuffer();this.#r=Array.from({length:t.numRecords},(i,s)=>ue(r.slice(s*8,s*8+4))).map((i,s,a)=>[i,a[s+1]])}loadRecord(e){const t=this.#r[e];if(!t)throw new RangeError("Record index out of bounds");return this.#e.slice(...t).arrayBuffer()}async loadMagic(e){const t=this.#r[e][0];return Ct(await this.#e.slice(t,t+4).arrayBuffer())}}class Ef extends Sf{#e=0;#r;#t;#n;#i;#s;constructor({unzlib:e}){super(),this.unzlib=e}async open(e){await super.open(e),this.headers=this.#o(await super.loadRecord(0)),this.#r=this.headers.mobi.resourceStart;let t=this.headers.mobi.version>=8;if(!t){const r=this.headers.exth?.boundary;if(r<4294967295)try{this.headers=this.#o(await super.loadRecord(r)),this.#e=r,t=!0}catch(i){console.warn(i),console.warn("Failed to open KF8; falling back to MOBI")}}return await this.#a(),t?new Pf(this).init():new Of(this).init()}#o(e){const t=we(tf,e),r=we(rf,e);if(r.magic!=="MOBI")throw new Error("Missing MOBI header");const{titleOffset:i,titleLength:s,localeLanguage:a,localeRegion:o}=r;r.title=e.slice(i,i+s);const c=ff[a];r.language=c?.[o>>2]??c?.[0];const l=r.exthFlag&64?wf(e.slice(r.length+16),r.encoding):null,u=r.version>=8?we(nf,e):null;return{palmdoc:t,mobi:r,exth:l,kf8:u}}async#a(){const{palmdoc:e,mobi:t}=this.headers;this.#t=gi(t.encoding),this.#n=new TextEncoder;const{compression:r}=e;if(this.#i=r===1?o=>o:r===2?mf:r===17480?await yf(t,this.loadRecord.bind(this)):null,!this.#i)throw new Error("Unknown compression type");const{trailingFlags:i}=t,s=i&1,a=mc(i>>>1);this.#s=o=>{for(let c=0;c<a;c++){const l=pf(o);o=o.subarray(0,-l)}if(s){const c=(o[o.length-1]&3)+1;o=o.subarray(0,-c)}return o}}decode(...e){return this.#t.decode(...e)}encode(...e){return this.#n.encode(...e)}loadRecord(e){return super.loadRecord(this.#e+e)}loadMagic(e){return super.loadMagic(this.#e+e)}loadText(e){return this.loadRecord(e+1).then(t=>new Uint8Array(t)).then(this.#s).then(this.#i)}async loadResource(e){const t=await super.loadRecord(this.#r+e),r=Ct(t.slice(0,4));return r==="FONT"?xf(t,this.unzlib):r==="VIDE"||r==="AUDI"?t.slice(12):t}getNCX(){const e=this.headers.mobi.indx;if(e<4294967295)return vf(e,this.loadRecord.bind(this))}getMetadata(){const{mobi:e,exth:t}=this.headers;return{identifier:e.uid.toString(),title:et(t?.title||this.decode(e.title)),author:t?.creator?.map(et),publisher:et(t?.publisher),language:t?.language??e.language,published:t?.date,description:et(t?.description),subject:t?.subject?.map(et),rights:et(t?.rights),contributor:t?.contributor}}async getCover(){const{exth:e}=this.headers,t=e?.coverOffset<4294967295?e?.coverOffset:e?.thumbnailOffset<4294967295?e?.thumbnailOffset:null;if(t!=null){const r=await this.loadResource(t);return new Blob([r])}}}const bc=/<\s*(?:mbp:)?pagebreak[^>]*>/gi,Tf=/<[^<>]+filepos=['"]{0,1}(\d+)[^<>]*>/gi,Rf=n=>{let e=0;for(;n;){const t=n.parentElement;if(t){const r=t.tagName.toLowerCase();r==="p"?e+=1.5:r==="blockquote"&&(e+=2)}n=t}return e};function kf(n){let t="";for(let r=0;r<n.length;r+=32768)t+=String.fromCharCode.apply(null,n.subarray(r,r+32768));return t}class Of{parser=new DOMParser;serializer=new XMLSerializer;#e=new Map;#r=new Map;#t=new Map;#n;#i=[];#s=qe.HTML;constructor(e){this.mobi=e}async init(){const e=[];for(let s=0;s<this.mobi.headers.palmdoc.numTextRecords;s++){const a=await this.mobi.loadText(s);e.push(a)}const t=e.reduce((s,a)=>s+a.byteLength,0),r=new Uint8Array(t);e.reduce((s,a)=>(r.set(new Uint8Array(a),s),s+a.byteLength),0);const i=kf(r);this.#n=[0].concat(Array.from(i.matchAll(bc),s=>s.index)).map((s,a,o)=>{const c=o[a+1]??r.length;return{book:this,raw:r.subarray(s,c)}}).map((s,a,o)=>(s.start=o[a-1]?.end??0,s.end=s.start+s.raw.byteLength,s)),this.sections=this.#n.map((s,a)=>({id:a,load:()=>this.loadSection(s),createDocument:()=>this.createDocument(s),size:s.end-s.start}));try{this.landmarks=await this.getGuide();const s=this.landmarks.find(({type:a})=>a?.includes("toc"))?.href;if(s){const{index:a}=this.resolveHref(s),o=await this.sections[a].createDocument();let c,l=0,u=0;const h=new Map,p=new Map;this.toc=Array.from(o.querySelectorAll("a[filepos]")).reduce((g,f)=>{const b=Rf(f),d={label:f.innerText?.trim()??"",href:`filepos:${f.getAttribute("filepos")}`},m=b>u?l+1:b===u?l:h.get(b)??Math.max(0,l-1);if(m>l)c?(c.subitems??=[],c.subitems.push(d),p.set(m,c)):g.push(d);else{const y=p.get(m);y?y.subitems.push(d):g.push(d)}return c=d,l=m,u=b,h.set(b,m),g},[])}}catch(s){console.warn(s)}return this.#i=[...new Set(Array.from(i.matchAll(Tf),s=>s[1]))].map(s=>({filepos:s,number:Number(s)})).sort((s,a)=>s.number-a.number),this.metadata=this.mobi.getMetadata(),this.getCover=this.mobi.getCover.bind(this.mobi),this}async getGuide(){const e=await this.createDocument(this.#n[0]);return Array.from(e.getElementsByTagName("reference"),t=>({label:t.getAttribute("title"),type:t.getAttribute("type")?.split(/\s/),href:`filepos:${t.getAttribute("filepos")}`}))}async loadResource(e){if(this.#e.has(e))return this.#e.get(e);const t=await this.mobi.loadResource(e),r=URL.createObjectURL(new Blob([t]));return this.#e.set(e,r),r}async loadRecindex(e){return this.loadResource(Number(e)-1)}async replaceResources(e){for(const t of e.querySelectorAll("img[recindex]")){const r=t.getAttribute("recindex");try{t.src=await this.loadRecindex(r)}catch{console.warn(`Failed to load image ${r}`)}}for(const t of e.querySelectorAll("[mediarecindex]")){const r=t.getAttribute("mediarecindex"),i=t.getAttribute("recindex");try{t.src=await this.loadRecindex(r),i&&(t.poster=await this.loadRecindex(i))}catch{console.warn(`Failed to load media ${r}`)}}for(const t of e.querySelectorAll("[filepos]")){const r=t.getAttribute("filepos");t.href=`filepos:${r}`}}async loadText(e){if(this.#r.has(e))return this.#r.get(e);const{raw:t}=e,r=this.#i.filter(({number:a})=>a>=e.start&&a<e.end).map(a=>({...a,offset:a.number-e.start}));let i=t;r.length&&(i=t.subarray(0,r[0].offset),r.forEach(({filepos:a,offset:o},c)=>{const l=r[c+1],u=this.mobi.encode(`<a id="filepos${a}"></a>`);i=gc(i,u,t.subarray(o,l?.offset))}));const s=this.mobi.decode(i).replaceAll(bc,"");return this.#r.set(e,s),s}async createDocument(e){const t=await this.loadText(e);return this.parser.parseFromString(t,this.#s)}async loadSection(e){if(this.#t.has(e))return this.#t.get(e);const t=await this.createDocument(e),r=t.createElement("style");t.head.append(r),r.append(t.createTextNode(`blockquote {
|
|
83
83
|
margin-block-start: 0;
|
|
84
84
|
margin-block-end: 0;
|
|
85
85
|
margin-inline-start: 1em;
|
|
@@ -406,7 +406,7 @@ const yt = async (n) => {
|
|
|
406
406
|
typeof n == "string" && (n = await yt(n));
|
|
407
407
|
let t;
|
|
408
408
|
if (n.isDirectory) {
|
|
409
|
-
const e = await wt(n), { EPUB: s } = await import("./epub-
|
|
409
|
+
const e = await wt(n), { EPUB: s } = await import("./epub-vizLT02-.js");
|
|
410
410
|
t = await new s(e).init();
|
|
411
411
|
} else if (n.size) if (await dt(n)) {
|
|
412
412
|
const e = await mt(n);
|
|
@@ -417,11 +417,11 @@ const yt = async (n) => {
|
|
|
417
417
|
const { makeFB2: s } = await import("./fb2-DCqZtzQ5.js"), { entries: i } = e, o = i.find((a) => a.filename.endsWith(".fb2")), r = await e.loadBlob((o ?? i[0]).filename);
|
|
418
418
|
t = await s(r);
|
|
419
419
|
} else {
|
|
420
|
-
const { EPUB: s } = await import("./epub-
|
|
420
|
+
const { EPUB: s } = await import("./epub-vizLT02-.js");
|
|
421
421
|
t = await new s(e).init();
|
|
422
422
|
}
|
|
423
423
|
} else if (await ut(n)) {
|
|
424
|
-
const { makePDF: e } = await import("./pdf-
|
|
424
|
+
const { makePDF: e } = await import("./pdf-D5_pBBCO.js");
|
|
425
425
|
t = await e(n);
|
|
426
426
|
} else {
|
|
427
427
|
const { isMOBI: e, MOBI: s } = await import("./mobi-CikMBloG.js");
|