@storyteller-platform/epub 0.4.10 → 0.5.0

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.
@@ -0,0 +1,909 @@
1
+ import { XMLParser, XMLBuilder } from 'fast-xml-parser';
2
+
3
+ interface Landmark {
4
+ href: string;
5
+ title: string;
6
+ type: string;
7
+ }
8
+ interface Epub2UpgradeOptions {
9
+ /**
10
+ * The path to the output file. If provided, the input file will be copied to the output path.
11
+ */
12
+ outputPath?: string;
13
+ /**
14
+ * Whether to remove the NCX file, as it's technically optional in EPUB 3.
15
+ */
16
+ removeNcx?: boolean;
17
+ }
18
+ declare function upgradeIdentifiers(pkg: PackageElement): void;
19
+ declare function upgradeTitle(pkg: PackageElement): void;
20
+ declare function upgradeLanguages(pkg: PackageElement): void;
21
+ declare function upgradeAuthors(pkg: PackageElement): void;
22
+ declare function upgradeDate(pkg: PackageElement): void;
23
+ declare function upgradeMeta(pkg: PackageElement): void;
24
+ declare function upgradeCover(pkg: PackageElement): void;
25
+ declare function removeInvalidDcAttrs(pkg: PackageElement): void;
26
+ declare function setLastModified(pkg: PackageElement): void;
27
+ declare function upgradePackageMetadata(pkg: PackageElement): void;
28
+ declare function extractGuideLandmarks(pkg: PackageElement): Landmark[];
29
+ declare function removeGuide(pkg: PackageElement): void;
30
+ declare function removeSpineTocRef(pkg: PackageElement): void;
31
+ declare function collectManifestProperties(epub: Epub): Promise<void>;
32
+ declare function fixFontMimeTypes(pkg: PackageElement): void;
33
+ declare function buildTocOl(entries: NavigationItem[]): XmlElement<"ol">;
34
+ declare function buildNavDocument(epub: Epub, tocEntries: NavigationItem[], landmarks: Landmark[]): Promise<string>;
35
+ declare function setPackageVersion(pkg: PackageElement, version: string): void;
36
+ declare function chooseNavHref(epub: Epub): Promise<string>;
37
+ declare function removeNcx(epub: Epub): Promise<void>;
38
+
39
+ declare global {
40
+ namespace Intl {
41
+ interface Locale {
42
+ textInfo: {
43
+ direction: "rtl" | "ltr";
44
+ };
45
+ }
46
+ }
47
+ }
48
+ type Letter = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z";
49
+ type QuestionMark = "?";
50
+ /** A valid name for an XML element (must start with a letter) */
51
+ type ElementName = `${Letter | Uppercase<Letter> | QuestionMark}${string}`;
52
+ type PropertyPrefix = "@_";
53
+ /** An XML element */
54
+ type XmlElement<Name extends ElementName = ElementName> = {
55
+ ":@"?: Record<`${PropertyPrefix}${string}`, string>;
56
+ } & {
57
+ [key in Name]: ParsedXml;
58
+ };
59
+ /** A text node in an XML document */
60
+ type XmlTextNode = {
61
+ "#text": string;
62
+ };
63
+ /** A valid XML node. May be either an element or a text node. */
64
+ type XmlNode = XmlElement | XmlTextNode;
65
+ /** An XML structure */
66
+ type ParsedXml = Array<XmlNode>;
67
+ type ManifestItem = {
68
+ id: string;
69
+ href: string;
70
+ mediaType?: string | undefined;
71
+ fallback?: string | undefined;
72
+ mediaOverlay?: string | undefined;
73
+ properties?: string[] | undefined;
74
+ };
75
+ type MetadataEntry = {
76
+ id?: string | undefined;
77
+ type: ElementName;
78
+ properties: Record<string, string>;
79
+ value: string | undefined;
80
+ };
81
+ type EpubMetadata = MetadataEntry[];
82
+ interface DcSubject {
83
+ value: string;
84
+ authority: string;
85
+ term: string;
86
+ }
87
+ interface AlternateScript {
88
+ name: string;
89
+ locale: Intl.Locale;
90
+ }
91
+ interface DcCreator {
92
+ name: string;
93
+ role?: string;
94
+ roleScheme?: string;
95
+ fileAs?: string;
96
+ alternateScripts?: AlternateScript[];
97
+ }
98
+ interface DublinCore {
99
+ title: string;
100
+ language: Intl.Locale;
101
+ identifier: string;
102
+ date?: Date;
103
+ subjects?: Array<string | DcSubject>;
104
+ creators?: DcCreator[];
105
+ contributors?: DcCreator[];
106
+ type?: string;
107
+ }
108
+ interface Collection {
109
+ name: string;
110
+ type?: string;
111
+ position?: string;
112
+ }
113
+ interface NavigationItem {
114
+ title: string;
115
+ href?: string;
116
+ children?: NavigationList;
117
+ }
118
+ type NavigationList = NavigationItem[];
119
+ interface Navigation {
120
+ title?: string;
121
+ children: NavigationList;
122
+ }
123
+ interface GuideItem {
124
+ href: string;
125
+ title: string;
126
+ type: string;
127
+ }
128
+ type PackageElement = XmlElement<"package">;
129
+ declare class EpubVersionError extends Error {
130
+ }
131
+ /**
132
+ * A single EPUB instance.
133
+ *
134
+ * The entire EPUB contents will be read into memory.
135
+ *
136
+ * Example usage:
137
+ *
138
+ * ```ts
139
+ * import { Epub, getBody, findByName, textContent } from '@storyteller-platform/epub';
140
+ *
141
+ * const epub = await Epub.from('./path/to/book.epub');
142
+ * const title = await epub.getTitle();
143
+ * const spineItems = await epub.getSpineItems();
144
+ * const chptOne = spineItems[0];
145
+ * const chptOneXml = await epub.readXhtmlItemContents(chptOne.id);
146
+ *
147
+ * const body = getBody(chptOneXml);
148
+ * const h1 = Epub.findXmlChildByName('h1', body);
149
+ * const headingText = textContent(h1);
150
+ *
151
+ * await epub.setTitle(headingText);
152
+ * await epub.writeToFile('./path/to/updated.epub');
153
+ * await epub.close();
154
+ * ```
155
+ *
156
+ * @link https://www.w3.org/TR/epub-33/
157
+ */
158
+ declare class Epub {
159
+ protected extractPath: string;
160
+ protected inputPath: string | undefined;
161
+ static xmlParser: XMLParser;
162
+ static xhtmlParser: XMLParser;
163
+ static xmlBuilder: XMLBuilder;
164
+ static xhtmlBuilder: XMLBuilder;
165
+ /**
166
+ * Format a duration, provided as a number of seconds, as
167
+ * a SMIL clock value, to be used for Media Overlays.
168
+ *
169
+ * @link https://www.w3.org/TR/epub-33/#sec-duration
170
+ */
171
+ static formatSmilDuration(duration: number): string;
172
+ /**
173
+ * Given an XML structure representing a complete XHTML document,
174
+ * add a `link` element to the `head` of the document.
175
+ *
176
+ * This method modifies the provided XML structure.
177
+ */
178
+ static addLinkToXhtmlHead(xml: ParsedXml, link: {
179
+ rel: string;
180
+ href: string;
181
+ type: string;
182
+ }): void;
183
+ /**
184
+ * Given an XML structure representing a complete XHTML document,
185
+ * return the sub-structure representing the children of the
186
+ * document's body element.
187
+ */
188
+ static getXhtmlBody(xml: ParsedXml): ParsedXml;
189
+ static createXmlElement<Name extends ElementName>(name: Name, properties: Record<string, string>, children?: XmlNode[]): XmlElement<Name>;
190
+ static createXmlTextNode(text: string): XmlTextNode;
191
+ /**
192
+ * Given an XML structure representing a complete XHTML document,
193
+ * return a string representing the concatenation of all text nodes
194
+ * in the document.
195
+ */
196
+ static getXhtmlTextContent(xml: ParsedXml): string;
197
+ /**
198
+ * Given an XMLElement, return its attributes.
199
+ */
200
+ static getXmlAttributes(element: XmlElement): Record<string, string>;
201
+ /**
202
+ * Given an XMLElement, return its tag name.
203
+ */
204
+ static getXmlElementName<Name extends ElementName>(element: XmlElement<Name>): Name;
205
+ /**
206
+ * Given an XMLElement, return a list of its children
207
+ */
208
+ static getXmlChildren<Name extends ElementName>(element: XmlElement<Name>): ParsedXml;
209
+ static replaceXmlChildren<Name extends ElementName>(element: XmlElement<Name>, children: XmlNode[]): void;
210
+ /**
211
+ * Given an XML structure, find the first child matching
212
+ * the provided name and optional filter.
213
+ */
214
+ static findXmlChildByName<Name extends ElementName>(name: Name, xml: ParsedXml, filter?: (node: XmlElement<Name>) => boolean): XmlElement<Name> | undefined;
215
+ /**
216
+ * Given an XML structure, find the first descendant matching
217
+ * the provided name and optional filter.
218
+ *
219
+ * Will perform a breadth first search for the element, returning
220
+ * the highest element in the tree matching the name and filter.
221
+ */
222
+ static findXmlDescendantByName<Name extends ElementName>(name: Name, xml: ParsedXml, filter?: (node: XmlElement<Name>) => boolean): XmlElement<Name> | undefined;
223
+ /**
224
+ * Given an XMLNode, determine whether it represents
225
+ * a text node or an XML element.
226
+ */
227
+ static isXmlTextNode(node: XmlNode): node is XmlTextNode;
228
+ private rootfile;
229
+ private manifest;
230
+ private spine;
231
+ private packageMutex;
232
+ protected constructor(extractPath: string, inputPath: string | undefined);
233
+ /**
234
+ * Construct an Epub instance, optionally beginning
235
+ * with the provided metadata.
236
+ *
237
+ * @param dublinCore Core metadata terms
238
+ * @param additionalMetadata An array of additional metadata entries
239
+ */
240
+ static create(path: string, { title, language, identifier, date, subjects, type, creators, contributors, }: DublinCore, additionalMetadata?: EpubMetadata): Promise<Epub>;
241
+ /**
242
+ * Construct an Epub instance by reading an existing EPUB
243
+ * publication.
244
+ *
245
+ * @param pathOrData Must be either a string representing the
246
+ * path to an EPUB file on disk, or a Uint8Array representing
247
+ * the data of the EPUB publication.
248
+ */
249
+ static from(pathOrData: string | Uint8Array): Promise<Epub>;
250
+ /**
251
+ * Open an EPUB publication and return an Epub instance.
252
+ */
253
+ private static open;
254
+ copy(path?: string): Promise<Epub>;
255
+ private removeEntry;
256
+ private getFileData;
257
+ getRootfile(): Promise<string>;
258
+ private getPackageDocument;
259
+ private getPackageElement;
260
+ /**
261
+ * Safely modify the package document, without race conditions.
262
+ *
263
+ * Since the reading the package document is an async process,
264
+ * multiple simultaneously dispatched function calls that all
265
+ * attempt to modify it can clobber each other's changes. This
266
+ * method uses a mutex to ensure that each update runs exclusively.
267
+ *
268
+ * @param producer The function to update the package document. If
269
+ * it returns a new package document, that will be persisted, otherwise
270
+ * it will be assumed that the package document was modified in place.
271
+ */
272
+ private withPackage;
273
+ /**
274
+ * Retrieve the manifest for the Epub.
275
+ *
276
+ * This is represented as a map from each manifest items'
277
+ * id to the rest of its properties.
278
+ *
279
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-manifest
280
+ */
281
+ getManifest(): Promise<Record<string, ManifestItem>>;
282
+ /**
283
+ * Returns the first index in the metadata element's children array
284
+ * that matches the provided predicate.
285
+ *
286
+ * Note: This may technically be different than the index in the
287
+ * getMetadata() array, as it includes non-metadata nodes, like
288
+ * text nodes. These are technically not allowed, but may exist,
289
+ * nonetheless. As consumers only ever see the getMetadata()
290
+ * array, this method is only meant to be used internally.
291
+ */
292
+ private findMetadataIndex;
293
+ /**
294
+ * Returns the item in the metadata element's children array
295
+ * that matches the provided predicate.
296
+ */
297
+ findMetadataItem(predicate: (entry: MetadataEntry) => boolean): Promise<{
298
+ id: string | undefined;
299
+ type: `a${string}` | `b${string}` | `c${string}` | `d${string}` | `e${string}` | `f${string}` | `g${string}` | `h${string}` | `i${string}` | `j${string}` | `k${string}` | `l${string}` | `m${string}` | `n${string}` | `o${string}` | `p${string}` | `q${string}` | `r${string}` | `s${string}` | `t${string}` | `u${string}` | `v${string}` | `w${string}` | `x${string}` | `y${string}` | `z${string}` | `A${string}` | `B${string}` | `C${string}` | `D${string}` | `E${string}` | `F${string}` | `G${string}` | `H${string}` | `I${string}` | `J${string}` | `K${string}` | `L${string}` | `M${string}` | `N${string}` | `O${string}` | `P${string}` | `Q${string}` | `R${string}` | `S${string}` | `T${string}` | `U${string}` | `V${string}` | `W${string}` | `X${string}` | `Y${string}` | `Z${string}` | `?${string}`;
300
+ properties: {
301
+ [k: string]: string;
302
+ };
303
+ value: string | undefined;
304
+ } | null>;
305
+ /**
306
+ * Returns the item in the metadata element's children array
307
+ * that matches the provided predicate.
308
+ */
309
+ findAllMetadataItems(predicate: (entry: MetadataEntry) => boolean): Promise<{
310
+ id: string | undefined;
311
+ type: `a${string}` | `b${string}` | `c${string}` | `d${string}` | `e${string}` | `f${string}` | `g${string}` | `h${string}` | `i${string}` | `j${string}` | `k${string}` | `l${string}` | `m${string}` | `n${string}` | `o${string}` | `p${string}` | `q${string}` | `r${string}` | `s${string}` | `t${string}` | `u${string}` | `v${string}` | `w${string}` | `x${string}` | `y${string}` | `z${string}` | `A${string}` | `B${string}` | `C${string}` | `D${string}` | `E${string}` | `F${string}` | `G${string}` | `H${string}` | `I${string}` | `J${string}` | `K${string}` | `L${string}` | `M${string}` | `N${string}` | `O${string}` | `P${string}` | `Q${string}` | `R${string}` | `S${string}` | `T${string}` | `U${string}` | `V${string}` | `W${string}` | `X${string}` | `Y${string}` | `Z${string}` | `?${string}`;
312
+ properties: {
313
+ [k: string]: string;
314
+ };
315
+ value: string | undefined;
316
+ }[]>;
317
+ private static parseMetadataItem;
318
+ /**
319
+ * Retrieve the metadata entries for the Epub.
320
+ *
321
+ * This is represented as an array of metadata entries,
322
+ * in the order that they're presented in the Epub package document.
323
+ *
324
+ * For more useful semantic representations of metadata, use
325
+ * specific methods such as `getTitle()` and `getAuthors()`.
326
+ *
327
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
328
+ */
329
+ getMetadata(): Promise<EpubMetadata>;
330
+ /**
331
+ * Retrieve the identifier from the dc:identifier element
332
+ * in the EPUB metadata.
333
+ *
334
+ * If there is no dc:identifier element, returns null.
335
+ *
336
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcidentifier
337
+ */
338
+ getIdentifier(): Promise<string | null>;
339
+ /**
340
+ * Set the dc:identifier metadata element with the provided string.
341
+ *
342
+ * Updates the existing dc:identifier element if one exists.
343
+ * Otherwise creates a new element
344
+ *
345
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcidentifier
346
+ */
347
+ setIdentifier(identifier: string): Promise<void>;
348
+ /**
349
+ * Even "EPUB 3" publications sometimes still only use the
350
+ * EPUB 2 specification for identifying the cover image.
351
+ * This is a private method that is used as a fallback if
352
+ * we fail to find the cover image according to the EPUB 3
353
+ * spec.
354
+ */
355
+ private getEpub2CoverImage;
356
+ /**
357
+ * Retrieve the cover image manifest item.
358
+ *
359
+ * This does not return the actual image data. To
360
+ * retrieve the image data, pass this item's id to
361
+ * epub.readItemContents, or use epub.getCoverImage()
362
+ * instead.
363
+ *
364
+ * @link https://www.w3.org/TR/epub-33/#sec-cover-image
365
+ */
366
+ getCoverImageItem(): Promise<ManifestItem | null>;
367
+ /**
368
+ * Retrieve the cover image data as a byte array.
369
+ *
370
+ * This does not include, for example, the cover image's
371
+ * filename or mime type. To retrieve the image manifest
372
+ * item, use epub.getCoverImageItem().
373
+ *
374
+ * @link https://www.w3.org/TR/epub-33/#sec-cover-image
375
+ */
376
+ getCoverImage(): Promise<Uint8Array<ArrayBufferLike> | null>;
377
+ /**
378
+ * Set the cover image for the EPUB.
379
+ *
380
+ * Adds a manifest item with the `cover-image` property, per
381
+ * the EPUB 3 spec, and then writes the provided image data to
382
+ * the provided href within the publication.
383
+ */
384
+ setCoverImage(href: string, data: Uint8Array): Promise<void>;
385
+ /**
386
+ * Retrieve the publication date from the dc:date element
387
+ * in the EPUB metadata as a Date object.
388
+ *
389
+ * If there is no dc:date element, returns null.
390
+ *
391
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcdate
392
+ */
393
+ getPublicationDate(): Promise<Date | null>;
394
+ /**
395
+ * Set the dc:date metadata element with the provided date.
396
+ *
397
+ * Updates the existing dc:date element if one exists.
398
+ * Otherwise creates a new element
399
+ *
400
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcdate
401
+ */
402
+ setPublicationDate(date: Date): Promise<void>;
403
+ /**
404
+ * Retrieve the modified date from the dcterms:modified metadata
405
+ * in the EPUB metadata as a Date object.
406
+ *
407
+ * If there is no meta element with dcterms:modified, returns null.
408
+ *
409
+ * @link https://www.w3.org/TR/epub-33/#sec-metadata-last-modified
410
+ */
411
+ getModifiedDate(): Promise<Date | null>;
412
+ /**
413
+ * Retrieve the layout from the rendition:layout meta element
414
+ * in the EPUB metadata.
415
+ *
416
+ * If there is no meta element, returns 'reflowable'.
417
+ *
418
+ * @link https://www.w3.org/TR/epub-33/#layout
419
+ */
420
+ getLayout(): Promise<"reflowable" | "pre-paginated">;
421
+ /**
422
+ * Retrieve the base direction from the package element.
423
+ *
424
+ * If there is no `dir` attribute on the package element,
425
+ * returns 'auto'.
426
+ *
427
+ * @link https://www.w3.org/TR/epub-33/#attrdef-dir
428
+ */
429
+ getBaseDirection(): Promise<"ltr" | "rtl" | "auto">;
430
+ /**
431
+ * Set the dc:type metadata element.
432
+ *
433
+ * Updates the existing dc:type element if one exists.
434
+ * Otherwise creates a new element.
435
+ *
436
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctype
437
+ */
438
+ setType(type: string): Promise<void>;
439
+ /**
440
+ * Retrieve the publication type from the dc:type element
441
+ * in the EPUB metadata.
442
+ *
443
+ * If there is no dc:type element, returns null.
444
+ *
445
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctype
446
+ */
447
+ getType(): Promise<MetadataEntry | null>;
448
+ /**
449
+ * Add a subject to the EPUB metadata.
450
+ *
451
+ * @param subject May be a string representing just a schema-less
452
+ * subject name, or a DcSubject object
453
+ *
454
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcsubject
455
+ */
456
+ addSubject(subject: string | DcSubject): Promise<void>;
457
+ /**
458
+ * Remove a subject from the EPUB metadata.
459
+ *
460
+ * Removes the subject at the provided index. This index
461
+ * refers to the array returned by `epub.getSubjects()`.
462
+ *
463
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
464
+ */
465
+ removeSubject(index: number): Promise<void>;
466
+ /**
467
+ * Retrieve the list of subjects for this EPUB.
468
+ *
469
+ * Subjects without associated authority and term metadata
470
+ * will be returned as strings. Otherwise, they will
471
+ * be represented as DcSubject objects, with a value,
472
+ * authority, and term.
473
+ *
474
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcsubject
475
+ */
476
+ getSubjects(): Promise<(string | DcSubject)[]>;
477
+ /**
478
+ * Retrieve the Epub's language as specified in its
479
+ * package document metadata.
480
+ *
481
+ * If no language metadata is specified, returns null.
482
+ * Returns the language as an Intl.Locale instance.
483
+ *
484
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dclanguage
485
+ */
486
+ getLanguage(): Promise<Intl.Locale | null>;
487
+ /**
488
+ * Update the Epub's language metadata entry.
489
+ *
490
+ * Updates the existing dc:language element if one exists.
491
+ * Otherwise creates a new element
492
+ *
493
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dclanguage
494
+ */
495
+ setLanguage(locale: Intl.Locale): Promise<void>;
496
+ /**
497
+ * Retrieve the title of the Epub.
498
+ *
499
+ * @param main Optional - whether to return only the first title segment
500
+ * if multiple are found. Otherwise, will follow the spec to combine title
501
+ * segments
502
+ *
503
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
504
+ */
505
+ getTitle(expanded?: boolean): Promise<string | null>;
506
+ /**
507
+ * Retrieve the subtitle of the Epub, if it exists.
508
+ *
509
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
510
+ */
511
+ getSubtitle(): Promise<string | null>;
512
+ /**
513
+ * Retrieve all title entries of the Epub.
514
+ *
515
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
516
+ */
517
+ getTitles(): Promise<{
518
+ title: string;
519
+ type: string | null;
520
+ }[]>;
521
+ /**
522
+ * Update the Epub's description metadata entry.
523
+ *
524
+ * Updates the existing dc:description element if one exists.
525
+ * Otherwise creates a new element. Any non-ASCII symbols,
526
+ * `&`, `<`, `>`, `"`, `'`, and `\``` will be encoded as HTML entities.
527
+ */
528
+ setDescription(description: string): Promise<void>;
529
+ /**
530
+ * Retrieve the Epub's description as specified in its
531
+ * package document metadata.
532
+ *
533
+ * If no description metadata is specified, returns null.
534
+ * Returns the description as a string. Descriptions may
535
+ * include HTML markup.
536
+ */
537
+ getDescription(): Promise<string | null>;
538
+ /**
539
+ * Return the set of custom vocabulary prefixes set on this publication's
540
+ * root package element.
541
+ *
542
+ * Returns a map from prefix to URI
543
+ *
544
+ * @link https://www.w3.org/TR/epub-33/#sec-prefix-attr
545
+ */
546
+ getPackageVocabularyPrefixes(): Promise<Record<string, string>>;
547
+ /**
548
+ * Set a custom vocabulary prefix on the root package element.
549
+ *
550
+ * @link https://www.w3.org/TR/epub-33/#sec-prefix-attr
551
+ */
552
+ setPackageVocabularyPrefix(prefix: string, uri: string): Promise<void>;
553
+ /**
554
+ * Set the title of the Epub.
555
+ *
556
+ * This will replace all existing dc:title elements with
557
+ * this title. It will be given title-type "main".
558
+ *
559
+ * To set specific titles and their types, use epub.setTitles().
560
+ *
561
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
562
+ */
563
+ setTitle(title: string): Promise<void>;
564
+ setTitles(entries: {
565
+ title: string;
566
+ type: string | null;
567
+ }[]): Promise<void>;
568
+ /**
569
+ * Retrieve the list of collections.
570
+ */
571
+ getCollections(): Promise<Collection[]>;
572
+ /**
573
+ * Add a collection to the EPUB metadata.
574
+ *
575
+ * If index is provided, the collection will be placed at
576
+ * that index in the list of collections. Otherwise, it
577
+ * will be added to the end of the list.
578
+ */
579
+ addCollection(collection: Collection, index?: number): Promise<void>;
580
+ /**
581
+ * Remove a collection from the EPUB metadata.
582
+ *
583
+ * Removes the collection at the provided index. This index
584
+ * refers to the array returned by `epub.getCollections()`.
585
+ */
586
+ removeCollection(index: number): Promise<void>;
587
+ /**
588
+ * Retrieve the list of creators.
589
+ *
590
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
591
+ */
592
+ getCreators(type?: "creator" | "contributor"): Promise<DcCreator[]>;
593
+ /**
594
+ * Retrieve the list of contributors.
595
+ *
596
+ * This is a convenience method for
597
+ * `epub.getCreators('contributor')`.
598
+ *
599
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccontributor
600
+ */
601
+ getContributors(): Promise<DcCreator[]>;
602
+ /**
603
+ * Add a creator to the EPUB metadata.
604
+ *
605
+ * If index is provided, the creator will be placed at
606
+ * that index in the list of creators. Otherwise, it
607
+ * will be added to the end of the list.
608
+ *
609
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
610
+ */
611
+ addCreator(creator: DcCreator, index?: number, type?: "creator" | "contributor"): Promise<void>;
612
+ /**
613
+ * Remove a creator from the EPUB metadata.
614
+ *
615
+ * Removes the creator at the provided index. This index
616
+ * refers to the array returned by `epub.getCreators()`.
617
+ *
618
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
619
+ */
620
+ removeCreator(index: number, type?: "creator" | "contributor"): Promise<void>;
621
+ /**
622
+ * Remove a contributor from the EPUB metadata.
623
+ *
624
+ * Removes the contributor at the provided index. This index
625
+ * refers to the array returned by `epub.getContributors()`.
626
+ *
627
+ * This is a convenience method for
628
+ * `epub.removeCreator(index, 'contributor')`.
629
+ *
630
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
631
+ */
632
+ removeContributor(index: number): Promise<void>;
633
+ /**
634
+ * Add a contributor to the EPUB metadata.
635
+ *
636
+ * If index is provided, the creator will be placed at
637
+ * that index in the list of creators. Otherwise, it
638
+ * will be added to the end of the list.
639
+ *
640
+ * This is a convenience method for
641
+ * `epub.addCreator(contributor, index, 'contributor')`.
642
+ *
643
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
644
+ */
645
+ addContributor(contributor: DcCreator, index?: number): Promise<void>;
646
+ private getSpine;
647
+ /**
648
+ * Retrieve the manifest items that make up the Epub's spine.
649
+ *
650
+ * The spine specifies the order that the contents of the Epub
651
+ * should be displayed to users by default.
652
+ *
653
+ * @link https://www.w3.org/TR/epub-33/#sec-spine-elem
654
+ */
655
+ getSpineItems(): Promise<ManifestItem[]>;
656
+ /**
657
+ * Add an item to the spine of the EPUB.
658
+ *
659
+ * If `index` is undefined, the item will be added
660
+ * to the end of the spine. Otherwise it will be
661
+ * inserted at the specified index.
662
+ *
663
+ * If the manifestId does not correspond to an item
664
+ * in the manifest, this will throw an error.
665
+ *
666
+ * @link https://www.w3.org/TR/epub-33/#sec-spine-elem
667
+ */
668
+ addSpineItem(manifestId: string, index?: number): Promise<void>;
669
+ /**
670
+ * Remove the spine item at the specified index.
671
+ *
672
+ * @link https://www.w3.org/TR/epub-33/#sec-spine-elem
673
+ */
674
+ removeSpineItem(index: number): Promise<void>;
675
+ private getNavigationChildren;
676
+ private getNavigation;
677
+ /**
678
+ * Returns the structured table of contents navigation document
679
+ * as a Navigation object.
680
+ *
681
+ * @link https://www.w3.org/TR/epub-33/#sec-nav-toc
682
+ */
683
+ getTableOfContents({ resolveToRoot, }?: {
684
+ resolveToRoot?: boolean;
685
+ }): Promise<Navigation | null>;
686
+ /**
687
+ * Returns the structured landmarks navigation document
688
+ * as a Navigation object
689
+ *
690
+ * @link https://www.w3.org/TR/epub-33/#sec-nav-landmarks
691
+ */
692
+ getLandmarks({ resolveToRoot, }?: {
693
+ resolveToRoot?: boolean;
694
+ }): Promise<Navigation | null>;
695
+ /**
696
+ * Returns the structured page list navigation document
697
+ * as a Navigation object
698
+ *
699
+ * @link https://www.w3.org/TR/epub-33/#sec-nav-landmarks
700
+ */
701
+ getPageList({ resolveToRoot, }?: {
702
+ resolveToRoot?: boolean;
703
+ }): Promise<Navigation | null>;
704
+ /**
705
+ * Returns a Zip Entry path for an HREF
706
+ */
707
+ private resolveInternalHref;
708
+ /**
709
+ * Returns a path-relative-scheme-less URL, relative to the
710
+ * container root.
711
+ *
712
+ * @param href The href to resolve
713
+ * @param [relativeTo] Optional - The href to resolve this href relative to.
714
+ Use if resolving a relative href from a file other than the package document.
715
+ */
716
+ resolveHref(href: string, relativeTo?: string, { toRoot }?: {
717
+ toRoot?: boolean | undefined;
718
+ }): Promise<string>;
719
+ /**
720
+ * Retrieve the contents of a file, given its href.
721
+ *
722
+ * Optionally takes the href that this href should be resolved relative to,
723
+ * and an encoding parameter.
724
+ *
725
+ * @param href The href of the file to retrieve
726
+ * @param [relitaveTo] Optional - The href to resolve this href relative to.
727
+ * Use if resolving a relative href from a file other than the package document.
728
+ * @param [encoding] Optional - Must be the string "utf-8". If provided,
729
+ * the function will encode the data into a unicode string.
730
+ * Otherwise, the data will be returned as a byte array.
731
+ */
732
+ readFileContents(href: string, relativeTo?: string): Promise<Uint8Array>;
733
+ readFileContents(href: string, relativeTo: string | undefined, encoding: "utf-8"): Promise<string>;
734
+ /**
735
+ * Retrieve the contents of a manifest item, given its id.
736
+ *
737
+ * @param id The id of the manifest item to retrieve
738
+ * @param [encoding] Optional - must be the string "utf-8". If
739
+ * provided, the function will encode the data into a unicode string.
740
+ * Otherwise, the data will be returned as a byte array.
741
+ *
742
+ * @link https://www.w3.org/TR/epub-33/#sec-contentdocs
743
+ */
744
+ readItemContents(id: string): Promise<Uint8Array>;
745
+ readItemContents(id: string, encoding: "utf-8"): Promise<string>;
746
+ /**
747
+ * Create a new XHTML document with the given body
748
+ * and head.
749
+ *
750
+ * @param body The XML nodes to place in the body of the document
751
+ * @param head Optional - the XMl nodes to place in the head
752
+ * @param language Optional - defaults to the EPUB's language
753
+ */
754
+ createXhtmlDocument(body: ParsedXml, head?: ParsedXml, language?: Intl.Locale): Promise<(XmlElement<"html"> | XmlElement<"?xml">)[]>;
755
+ /**
756
+ * Retrieves the contents of an XHTML item, given its manifest id.
757
+ *
758
+ * @param id The id of the manifest item to retrieve
759
+ * @param [as] Optional - whether to return the parsed XML document tree,
760
+ * or the concatenated text of the document. Defaults to the parsed XML tree.
761
+ *
762
+ * @link https://www.w3.org/TR/epub-33/#sec-xhtml
763
+ */
764
+ readXhtmlItemContents(id: string, as?: "xhtml"): Promise<ParsedXml>;
765
+ readXhtmlItemContents(id: string, as: "text"): Promise<string>;
766
+ private writeEntryContents;
767
+ /**
768
+ * Write new contents for an existing manifest item,
769
+ * specified by its id.
770
+ *
771
+ * The id must reference an existing manifest item. If
772
+ * creating a new item, use `epub.addManifestItem()` instead.
773
+ *
774
+ * @param id The id of the manifest item to write new contents for
775
+ * @param contents The new contents. May be either a utf-8 encoded string
776
+ * or a byte array, as determined by the encoding
777
+ * @param [encoding] Optional - must be the string "utf-8". If provided,
778
+ * the contents will be interpreted as a unicode string. Otherwise, the
779
+ * contents must be a byte array.
780
+ *
781
+ * @link https://www.w3.org/TR/epub-33/#sec-contentdocs
782
+ */
783
+ writeItemContents(id: string, contents: Uint8Array): Promise<void>;
784
+ writeItemContents(id: string, contents: string, encoding: "utf-8"): Promise<void>;
785
+ /**
786
+ * Write new contents for an existing XHTML item,
787
+ * specified by its id.
788
+ *
789
+ * The id must reference an existing manifest item. If
790
+ * creating a new item, use `epub.addManifestItem()` instead.
791
+ *
792
+ * @param id The id of the manifest item to write new contents for
793
+ * @param contents The new contents. Must be a parsed XML tree.
794
+ *
795
+ * @link https://www.w3.org/TR/epub-33/#sec-xhtml
796
+ */
797
+ writeXhtmlItemContents(id: string, contents: ParsedXml): Promise<void>;
798
+ removeManifestItem(id: string): Promise<void>;
799
+ /**
800
+ * Create a new manifest item and write its contents to a
801
+ * new entry.
802
+ *
803
+ * @param id The id of the manifest item to write new contents for
804
+ * @param contents The new contents. May be either a parsed XML tree
805
+ * or a unicode string, as determined by the `as` argument.
806
+ * @param encoding Optional - whether to interpret contents as a parsed
807
+ * XML tree, a unicode string, or a byte array. Defaults to a byte array.
808
+ *
809
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-manifest
810
+ * @link https://www.w3.org/TR/epub-33/#sec-contentdocs
811
+ */
812
+ addManifestItem(item: ManifestItem, contents: ParsedXml, encoding: "xml"): Promise<void>;
813
+ addManifestItem(item: ManifestItem, contents: string, encoding: "utf-8"): Promise<void>;
814
+ addManifestItem(item: ManifestItem, contents: Uint8Array): Promise<void>;
815
+ /**
816
+ * Update the manifest entry for an existing item.
817
+ *
818
+ * To update the contents of an entry, use `epub.writeItemContents()`
819
+ * or `epub.writeXhtmlItemContents()`
820
+ *
821
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-manifest
822
+ */
823
+ updateManifestItem(id: string, newItem: Omit<ManifestItem, "id">): Promise<void>;
824
+ /**
825
+ * Add a new metadata entry to the Epub.
826
+ *
827
+ * This method, like `epub.getMetadata()`, operates on
828
+ * metadata entries. For more useful semantic representations
829
+ * of metadata, use specific methods such as `setTitle()` and
830
+ * `setLanguage()`.
831
+ *
832
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
833
+ */
834
+ addMetadata(entry: MetadataEntry): Promise<void>;
835
+ /**
836
+ * Replace a metadata entry with a new one.
837
+ *
838
+ * The `predicate` argument will be used to determine which entry
839
+ * to replace. The first metadata entry that matches the
840
+ * predicate will be replaced.
841
+ *
842
+ * @param predicate Calls predicate once for each metadata entry,
843
+ * until it finds one where predicate returns true
844
+ * @param entry The new entry to replace the found entry with
845
+ *
846
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
847
+ */
848
+ replaceMetadata(predicate: (entry: MetadataEntry) => boolean, entry: MetadataEntry): Promise<void>;
849
+ /**
850
+ * Remove one or more metadata entries.
851
+ *
852
+ * The `predicate` argument will be used to determine which entries
853
+ * to remove. The all metadata entries that match the
854
+ * predicate will be removed.
855
+ *
856
+ * @param predicate Calls predicate once for each metadata entry,
857
+ * removing any for which it returns true
858
+ *
859
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
860
+ */
861
+ removeMetadata(predicate: (entry: MetadataEntry) => boolean): Promise<void>;
862
+ /**
863
+ * Returns the EPUB version declared on the package element.
864
+ */
865
+ getVersion(): Promise<string>;
866
+ /**
867
+ * Parse the NCX table of contents, if one exists, and return
868
+ * a tree of TocEntry nodes.
869
+ *
870
+ * Useful for both EPUB 2 publications (where the NCX is the
871
+ * primary navigation) and EPUB 3 publications that retain an
872
+ * NCX for backwards compatibility.
873
+ */
874
+ getNcxTableOfContents(): Promise<NavigationList>;
875
+ private parseNavPoints;
876
+ /**
877
+ * Retrieve the guide entries from the package document.
878
+ *
879
+ * The guide element is deprecated in EPUB 3 in favor of
880
+ * the landmarks nav, but many publications still include it.
881
+ */
882
+ getGuideEntries(): Promise<GuideItem[]>;
883
+ discardAndClose(): void;
884
+ /**
885
+ * Write the current contents of the Epub to a new
886
+ * EPUB archive on disk.
887
+ *
888
+ * When this method is called, the "dcterms:modified"
889
+ * meta tag is automatically updated to the current UTC
890
+ * timestamp.
891
+ */
892
+ saveAndClose(): Promise<void>;
893
+ /**
894
+ * Upgrade an EPUB 2 publication to EPUB 3 in place, returning a new, valid Epub 3 instance.
895
+ *
896
+ * Performs the following transformations:
897
+ * - upgrades OPF metadata to EPUB 3 conventions
898
+ * - scans XHTML documents and adds manifest item properties
899
+ * - parses the NCX into a TOC tree and generates a nav.xhtml
900
+ * - removes the NCX file and the guide element (configurable)
901
+ * - fixes common font MIME types
902
+ * - bumps the package version to 3.0
903
+ * - goes over each xhtml item and rewrites it using XMLParser to make sure the output is valid XHTML
904
+ */
905
+ static upgrade(path: string, options?: Epub2UpgradeOptions): Promise<Epub>;
906
+ [Symbol.dispose](): void;
907
+ }
908
+
909
+ export { type AlternateScript as A, type Collection as C, type DcSubject as D, type ElementName as E, type Epub2UpgradeOptions, type Landmark, type ManifestItem as M, type NavigationItem as N, type ParsedXml as P, type XmlElement as X, type XmlTextNode as a, type XmlNode as b, buildNavDocument, buildTocOl, type MetadataEntry as c, chooseNavHref, collectManifestProperties, type EpubMetadata as d, type DcCreator as e, extractGuideLandmarks, type DublinCore as f, fixFontMimeTypes, type NavigationList as g, type Navigation as h, type PackageElement as i, EpubVersionError as j, Epub as k, removeGuide, removeInvalidDcAttrs, removeNcx, removeSpineTocRef, setLastModified, setPackageVersion, upgradeAuthors, upgradeCover, upgradeDate, upgradeIdentifiers, upgradeLanguages, upgradeMeta, upgradePackageMetadata, upgradeTitle };