@storyteller-platform/epub 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -36,6 +36,7 @@ export type ParsedXml = Array<XmlNode>;
36
36
  export type ManifestItem = {
37
37
  id: string;
38
38
  href: string;
39
+ title?: string | undefined;
39
40
  mediaType?: string | undefined;
40
41
  fallback?: string | undefined;
41
42
  mediaOverlay?: string | undefined;
@@ -107,14 +108,16 @@ export interface Collection {
107
108
  export interface NavigationItem {
108
109
  title: string;
109
110
  href?: string;
111
+ role?: string;
110
112
  children?: NavigationList;
111
113
  }
112
114
  export type NavigationList = NavigationItem[];
113
115
  export interface Navigation {
114
116
  title?: string;
117
+ role: string;
115
118
  children: NavigationList;
116
119
  }
117
- interface GuideItem {
120
+ export interface GuideItem {
118
121
  href: string;
119
122
  title: string;
120
123
  type: string;
@@ -312,6 +315,7 @@ export declare class Epub {
312
315
  */
313
316
  getItemArchiveLength(id: string): Promise<number>;
314
317
  getRootfile(): Promise<string>;
318
+ getPackageDocumentSource(): Promise<string>;
315
319
  private getPackageDocument;
316
320
  getPackageElement(): Promise<XmlElement<"package">>;
317
321
  /**
@@ -834,7 +838,9 @@ export declare class Epub {
834
838
  *
835
839
  * @link https://www.w3.org/TR/epub-33/#sec-spine-elem
836
840
  */
837
- getSpineItems(): Promise<ManifestItem[]>;
841
+ getSpineItems(): Promise<(ManifestItem & {
842
+ linear: boolean;
843
+ })[]>;
838
844
  /**
839
845
  * Add an item to the spine of the EPUB.
840
846
  *
@@ -1129,6 +1135,7 @@ export declare class EpubFactory<A extends EpubStorageAdapterClass> {
1129
1135
  from(source: string | Uint8Array, options?: FromOptions & AdapterOptions<A>): Promise<EpubInstanceFor<A> & {
1130
1136
  storage: A["kind"];
1131
1137
  }>;
1138
+ readPackageDocumentSource(source: string | Uint8Array, options?: AdapterOptions<A>): Promise<string>;
1132
1139
  /**
1133
1140
  * Construct a new EPUB on this factory's adapter, optionally seeded
1134
1141
  * with the provided metadata. Requires a writable adapter that
package/dist/index.js CHANGED
@@ -440,12 +440,15 @@ export class Epub {
440
440
  this.rootfile = await this.resolveEntry("", fullPath);
441
441
  return this.rootfile;
442
442
  }
443
- async getPackageDocument() {
443
+ async getPackageDocumentSource() {
444
444
  const rootfile = await this.getRootfile();
445
445
  const packageDocumentString = await this.adapter.read(rootfile, "utf-8");
446
446
  if (!packageDocumentString)
447
447
  throw new Error(`Failed to parse EPUB: could not find package document at ${rootfile}`);
448
- const packageDocument = Epub.xmlParser.parse(packageDocumentString);
448
+ return packageDocumentString;
449
+ }
450
+ async getPackageDocument() {
451
+ const packageDocument = Epub.xmlParser.parse(await this.getPackageDocumentSource());
449
452
  return normalizeOpfNamespacePrefixes(packageDocument);
450
453
  }
451
454
  async getPackageElement() {
@@ -1824,8 +1827,15 @@ export class Epub {
1824
1827
  throw new Error("Failed to parse EPUB: Found no spine element in package document");
1825
1828
  this.spine = spine["spine"]
1826
1829
  .filter((node) => !Epub.isXmlTextNode(node))
1827
- .map((itemref) => itemref[":@"]?.["@_idref"])
1828
- .filter((idref) => !!idref);
1830
+ .map((itemref) => {
1831
+ const idRef = itemref[":@"]?.["@_idref"];
1832
+ const linear = itemref[":@"]?.["@_linear"];
1833
+ return {
1834
+ itemRef: idRef,
1835
+ linear: linear === "no" ? false : true,
1836
+ };
1837
+ })
1838
+ .filter((itemref) => !!itemref.itemRef);
1829
1839
  return this.spine;
1830
1840
  }
1831
1841
  /**
@@ -1839,7 +1849,14 @@ export class Epub {
1839
1849
  async getSpineItems() {
1840
1850
  const spine = await this.getSpine();
1841
1851
  const manifest = await this.getManifest();
1842
- return spine.map((itemref) => manifest[itemref]).filter((entry) => !!entry);
1852
+ const spineItems = [];
1853
+ for (const itemref of spine) {
1854
+ const item = manifest[itemref.itemRef];
1855
+ if (!item)
1856
+ continue;
1857
+ spineItems.push({ ...item, linear: itemref.linear });
1858
+ }
1859
+ return spineItems;
1843
1860
  }
1844
1861
  /**
1845
1862
  * Add an item to the spine of the EPUB.
@@ -1903,12 +1920,16 @@ export class Epub {
1903
1920
  (!secondChild || Epub.getXmlElementName(secondChild) !== "ol")) {
1904
1921
  continue;
1905
1922
  }
1923
+ const role = firstChild[":@"]?.["@_epub:type"]?.trim();
1906
1924
  children.push({
1907
1925
  title: Epub.getXhtmlTextContent(Epub.getXmlChildren(firstChild)),
1908
1926
  ...(Epub.getXmlElementName(firstChild) === "a" &&
1909
1927
  firstChild[":@"]?.["@_href"] && {
1910
- href: await this.resolveHref(firstChild[":@"]["@_href"], undefined, { toRoot: resolveToRoot }),
1928
+ href: await this.resolveHref(firstChild[":@"]["@_href"], navHref, {
1929
+ toRoot: resolveToRoot,
1930
+ }),
1911
1931
  }),
1932
+ ...(role && { role }),
1912
1933
  ...(secondChild &&
1913
1934
  Epub.getXmlElementName(secondChild) === "ol" && {
1914
1935
  children: await this.getNavigationChildren(secondChild, navHref, {
@@ -1947,6 +1968,7 @@ export class Epub {
1947
1968
  });
1948
1969
  return {
1949
1970
  ...(title && { title }),
1971
+ role,
1950
1972
  children,
1951
1973
  };
1952
1974
  }
@@ -1962,6 +1984,7 @@ export class Epub {
1962
1984
  return navigationToc;
1963
1985
  const ncxToc = await this.getNcxTableOfContents();
1964
1986
  return {
1987
+ role: "toc",
1965
1988
  children: ncxToc,
1966
1989
  };
1967
1990
  }
@@ -2490,6 +2513,16 @@ export class EpubFactory {
2490
2513
  }
2491
2514
  return epub;
2492
2515
  }
2516
+ async readPackageDocumentSource(source, options = {}) {
2517
+ const adapter = await this.adapterClass.init(source, options);
2518
+ const epub = new Epub(this.adapterClass, adapter, typeof source === "string" ? source : undefined, true);
2519
+ try {
2520
+ return await epub.getPackageDocumentSource();
2521
+ }
2522
+ finally {
2523
+ epub.discardAndClose();
2524
+ }
2525
+ }
2493
2526
  /**
2494
2527
  * Construct a new EPUB on this factory's adapter, optionally seeded
2495
2528
  * with the provided metadata. Requires a writable adapter that
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storyteller-platform/epub",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "type": "module",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "MIT",
@@ -38,10 +38,10 @@
38
38
  "vitest": "^5.0.0"
39
39
  },
40
40
  "dependencies": {
41
- "@readium/shared": "^2.2.0",
41
+ "@readium/shared": "^2.5.0",
42
42
  "@storyteller-platform/fs": "^0.3.0",
43
- "@storyteller-platform/media-types": "^0.2.0",
44
- "@storyteller-platform/path": "^0.3.0",
43
+ "@storyteller-platform/media-types": "^0.2.1",
44
+ "@storyteller-platform/path": "^0.3.1",
45
45
  "@zip.js/zip.js": "^2.0.0",
46
46
  "async-mutex": "^0.5.0",
47
47
  "fast-xml-builder": "^1.3.1",