@mdgate/epub 0.6.1 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  Convert EPUB books to Markdown. Outputs GitHub-Flavored Markdown. Works in Node, Edge, and
4
4
  browsers. No native addons.
5
5
 
6
- Handles: `.epub`
6
+ Handles: `.epub`, `.ibooks`
7
7
 
8
8
  ## Usage
9
9
 
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{detectZipDoc as f}from"@mdgate/containers";import{documentToMarkdown as g}from"@mdgate/document";import{fileExtension as i}from"@mdgate/utils";import{decodeFragment as w,Package as F,resolve as W}from"@mdgate/containers";import{ConvertError as I}from"@mdgate/core";import{emptyDocument as v,heading as j,plain as b}from"@mdgate/document";import{AssetSink as k,mediaTypeFor as m}from"@mdgate/office-common";import{isAbsoluteUri as L,trim as y,warn as Y}from"@mdgate/utils";import{Stylesheet as X,toBlocks as D}from"@mdgate/html";function E(u){let x=F.open(u),Z=x.requiredXmlPart("META-INF/container.xml"),M;for(let z of Z.descendantsAny("rootfile")){M=z.attrAny("full-path");break}if(M===void 0)throw I.malformedPart("META-INF/container.xml","no rootfile entry");let G=x.requiredXmlPart(M),B=v();for(let z of G.descendantsAny("title")){let R=y(z.text());if(R.length>0)B.blocks.push(j(1,[b(R)]));break}let N=new Map;for(let z of G.descendantsAny("item")){let R=z.attrAny("id"),K=z.attrAny("href");if(R===void 0||K===void 0)continue;N.set(R,{href:K,media:z.attrAny("media-type")??""})}let O=[];for(let z of G.descendantsAny("itemref")){let R=z.attrAny("idref");if(R===void 0)continue;let K=N.get(R);if(K!==void 0)O.push(K.href)}let T=new Set;for(let z of O)try{T.add(W(M,z).path)}catch{}let U=new k,J=new Map,Q=0;for(let z of O){let R;try{R=W(M,z).path}catch(V){Y(`skipping chapter with unresolvable href ${P(z)}: ${p(V)}`),Q+=1;continue}let K=x.optionalXmlPart(R);if(K===void 0){Y(`skipping unusable chapter ${R}`),Q+=1;continue}let q=K.childElems().find((V)=>V.local==="html")?.childElems().find((V)=>V.local==="body");if(q===void 0){Y(`skipping chapter ${R}: no body element`),Q+=1;continue}let C=o(K,R,x,J),S=new H(x,U,R,T);B.blocks.push({type:"paragraph",inlines:[{type:"anchor",id:R}]}),B.blocks.push(...D(q,C,S))}if(O.length>0&&Q===O.length)throw I.malformed("no chapter in the book could be read");return B.assets=U.assets,B}function o(u,x,Z,M){let G=new X,B=u.childElems();B.reverse();while(B.length>0){let N=B.pop();switch(N.local){case"link":{let T=(N.attrAny("rel")??"").split(/\s+/).some((J)=>n(J,"stylesheet")),U=N.attrAny("href");if(T&&U!==void 0){let J;try{J=W(x,U).path}catch{break}if(!M.has(J)){let z=Z.optionalPart(J);if(z===void 0)M.set(J,void 0);else{let R=new X;R.add(l(z)),M.set(J,R)}}let Q=M.get(J);if(Q!==void 0)G.addFrom(Q)}break}case"style":G.add(N.text());break;default:{let O=B.length;for(let T of N.childElems())B.push(T);d(B,O);break}}}return G}class H{pkg;assets;chapterPath;spineParts;constructor(u,x,Z,M){this.pkg=u;this.assets=x;this.chapterPath=Z;this.spineParts=M}linkTarget(u){if(u.length===0)return;if(u.startsWith("#")){let x=w(u.slice(1));return{type:"anchor",id:_(this.chapterPath,x)}}if(L(u))return{type:"external",url:u};try{let x=W(this.chapterPath,u);if(this.spineParts.has(x.path))return{type:"anchor",id:_(x.path,x.fragment)}}catch{}return{type:"relative",url:u}}imageSource(u){if(u.length===0)return;if(L(u))return{type:"external",url:u};let x;try{x=W(this.chapterPath,u).path}catch{return}let Z=this.pkg.optionalPart(x);if(Z===void 0)return;return{type:"asset",id:this.assets.add(m(x),x,Z)}}anchorId(u){return _(this.chapterPath,u)}}function _(u,x){if(x!==void 0&&x.length>0)return`${u}#${x}`;return u}function l(u){return new TextDecoder("utf-8",{fatal:!1}).decode(u)}function n(u,x){if(u.length!==x.length)return!1;for(let Z=0;Z<u.length;Z+=1){let M=u.charCodeAt(Z),G=x.charCodeAt(Z);if(A(M)!==A(G))return!1}return!0}function A(u){return u>=65&&u<=90?u+32:u}function d(u,x){let Z=x,M=u.length-1;while(Z<M){let G=u[Z];u[Z]=u[M],u[M]=G,Z+=1,M-=1}}function P(u){return JSON.stringify(u)}function p(u){return u instanceof Error?u.message:String(u)}function $(){return{id:"epub",sniff(u,x){if(f(u)==="epub")return 2;if(x?.path!==void 0&&i(x.path)==="epub")return 1;return 0},convert(u){return{markdown:g(E(u))}}}}import{create as c}from"@mdgate/core";var h=c([$()]);function s(u,x){return h(u,x)}export{s as toMarkdown,$ as epub};
1
+ import{detectZipDoc as f}from"@mdgate/containers";import{documentToMarkdown as g}from"@mdgate/document";import{fileExtension as i}from"@mdgate/utils";import{decodeFragment as C,Package as F,resolve as V}from"@mdgate/containers";import{ConvertError as E}from"@mdgate/core";import{emptyDocument as v,heading as j,plain as b}from"@mdgate/document";import{AssetSink as k,mediaTypeFor as m}from"@mdgate/office-common";import{isAbsoluteUri as I,trim as o,warn as Y}from"@mdgate/utils";import{Stylesheet as W,toBlocks as D}from"@mdgate/html";function S(u){let x=F.open(u),T=x.requiredXmlPart("META-INF/container.xml"),M;for(let X of T.descendantsAny("rootfile")){M=X.attrAny("full-path");break}if(M===void 0)throw E.malformedPart("META-INF/container.xml","no rootfile entry");let z=x.requiredXmlPart(M),Z=v();for(let X of z.descendantsAny("title")){let R=o(X.text());if(R.length>0)Z.blocks.push(j(1,[b(R)]));break}let J=new Map;for(let X of z.descendantsAny("item")){let R=X.attrAny("id"),G=X.attrAny("href");if(R===void 0||G===void 0)continue;J.set(R,{href:G,media:X.attrAny("media-type")??""})}let K=[];for(let X of z.descendantsAny("itemref")){let R=X.attrAny("idref");if(R===void 0)continue;let G=J.get(R);if(G!==void 0)K.push(G.href)}let O=new Set;for(let X of K)try{O.add(V(M,X).path)}catch{}let Q=new k,B=new Map,N=0;for(let X of K){let R;try{R=V(M,X).path}catch(U){Y(`skipping chapter with unresolvable href ${P(X)}: ${p(U)}`),N+=1;continue}let G=x.optionalXmlPart(R);if(G===void 0){Y(`skipping unusable chapter ${R}`),N+=1;continue}let q=G.childElems().find((U)=>U.local==="html")?.childElems().find((U)=>U.local==="body");if(q===void 0){Y(`skipping chapter ${R}: no body element`),N+=1;continue}let H=y(G,R,x,B),w=new A(x,Q,R,O);Z.blocks.push({type:"paragraph",inlines:[{type:"anchor",id:R}]}),Z.blocks.push(...D(q,H,w))}if(K.length>0&&N===K.length)throw E.malformed("no chapter in the book could be read");return Z.assets=Q.assets,Z}function y(u,x,T,M){let z=new W,Z=u.childElems();Z.reverse();while(Z.length>0){let J=Z.pop();switch(J.local){case"link":{let O=(J.attrAny("rel")??"").split(/\s+/).some((B)=>l(B,"stylesheet")),Q=J.attrAny("href");if(O&&Q!==void 0){let B;try{B=V(x,Q).path}catch{break}if(!M.has(B)){let X=T.optionalPart(B);if(X===void 0)M.set(B,void 0);else{let R=new W;R.add(n(X)),M.set(B,R)}}let N=M.get(B);if(N!==void 0)z.addFrom(N)}break}case"style":z.add(J.text());break;default:{let K=Z.length;for(let O of J.childElems())Z.push(O);d(Z,K);break}}}return z}class A{pkg;assets;chapterPath;spineParts;constructor(u,x,T,M){this.pkg=u;this.assets=x;this.chapterPath=T;this.spineParts=M}linkTarget(u){if(u.length===0)return;if(u.startsWith("#")){let x=C(u.slice(1));return{type:"anchor",id:_(this.chapterPath,x)}}if(I(u))return{type:"external",url:u};try{let x=V(this.chapterPath,u);if(this.spineParts.has(x.path))return{type:"anchor",id:_(x.path,x.fragment)}}catch{}return{type:"relative",url:u}}imageSource(u){if(u.length===0)return;if(I(u))return{type:"external",url:u};let x;try{x=V(this.chapterPath,u).path}catch{return}let T=this.pkg.optionalPart(x);if(T===void 0)return;return{type:"asset",id:this.assets.add(m(x),x,T)}}anchorId(u){return _(this.chapterPath,u)}}function _(u,x){if(x!==void 0&&x.length>0)return`${u}#${x}`;return u}function n(u){return new TextDecoder("utf-8",{fatal:!1}).decode(u)}function l(u,x){if(u.length!==x.length)return!1;for(let T=0;T<u.length;T+=1){let M=u.charCodeAt(T),z=x.charCodeAt(T);if(L(M)!==L(z))return!1}return!0}function L(u){return u>=65&&u<=90?u+32:u}function d(u,x){let T=x,M=u.length-1;while(T<M){let z=u[T];u[T]=u[M],u[M]=z,T+=1,M-=1}}function P(u){return JSON.stringify(u)}function p(u){return u instanceof Error?u.message:String(u)}var c=new Set(["epub","ibooks"]);function $(){return{id:"epub",sniff(u,x){if(f(u)==="epub")return 2;if(x?.path!==void 0&&c.has(i(x.path)??""))return 1;return 0},convert(u){return{markdown:g(S(u))}}}}import{create as h}from"@mdgate/core";var s=h([$()]);function r(u,x){return s(u,x)}export{r as toMarkdown,$ as epub};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/epub",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "mdgate EPUB converter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,12 +22,12 @@
22
22
  "prepublishOnly": "bun run build"
23
23
  },
24
24
  "dependencies": {
25
- "@mdgate/containers": "0.6.1",
26
- "@mdgate/core": "0.6.1",
27
- "@mdgate/document": "0.6.1",
28
- "@mdgate/html": "0.6.1",
29
- "@mdgate/office-common": "0.6.1",
30
- "@mdgate/utils": "0.6.1"
25
+ "@mdgate/containers": "0.6.3",
26
+ "@mdgate/core": "0.6.3",
27
+ "@mdgate/document": "0.6.3",
28
+ "@mdgate/html": "0.6.3",
29
+ "@mdgate/office-common": "0.6.3",
30
+ "@mdgate/utils": "0.6.3"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"