@storyteller-platform/epub 0.2.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.
- package/LICENSE.txt +21 -0
- package/README.md +2577 -0
- package/dist/index.cjs +1838 -0
- package/dist/index.d.cts +714 -0
- package/dist/index.d.ts +714 -0
- package/dist/index.js +1810 -0
- package/dist/node.cjs +66 -0
- package/dist/node.d.cts +28 -0
- package/dist/node.d.ts +28 -0
- package/dist/node.js +42 -0
- package/package.json +73 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
import { Entry } from '@zip.js/zip.js';
|
|
2
|
+
import { XMLParser, XMLBuilder } from 'fast-xml-parser';
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
namespace Intl {
|
|
6
|
+
interface Locale {
|
|
7
|
+
textInfo: {
|
|
8
|
+
direction: "rtl" | "ltr";
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
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";
|
|
14
|
+
type QuestionMark = "?";
|
|
15
|
+
/** A valid name for an XML element (must start with a letter) */
|
|
16
|
+
type ElementName = `${Letter | Uppercase<Letter> | QuestionMark}${string}`;
|
|
17
|
+
/** An XML element */
|
|
18
|
+
type XmlElement<Name extends ElementName = ElementName> = {
|
|
19
|
+
":@"?: Record<string, string>;
|
|
20
|
+
} & {
|
|
21
|
+
[key in Name]: ParsedXml;
|
|
22
|
+
};
|
|
23
|
+
/** A text node in an XML document */
|
|
24
|
+
type XmlTextNode = {
|
|
25
|
+
"#text": string;
|
|
26
|
+
};
|
|
27
|
+
/** A valid XML node. May be either an element or a text node. */
|
|
28
|
+
type XmlNode = XmlElement | XmlTextNode;
|
|
29
|
+
/** An XML structure */
|
|
30
|
+
type ParsedXml = Array<XmlNode>;
|
|
31
|
+
type ManifestItem = {
|
|
32
|
+
id: string;
|
|
33
|
+
href: string;
|
|
34
|
+
mediaType?: string | undefined;
|
|
35
|
+
fallback?: string | undefined;
|
|
36
|
+
mediaOverlay?: string | undefined;
|
|
37
|
+
properties?: string[] | undefined;
|
|
38
|
+
};
|
|
39
|
+
declare class EpubEntry {
|
|
40
|
+
filename: string;
|
|
41
|
+
private entry;
|
|
42
|
+
private data;
|
|
43
|
+
getData(): Promise<Uint8Array<ArrayBufferLike>>;
|
|
44
|
+
setData(data: Uint8Array): void;
|
|
45
|
+
constructor(entry: Entry | {
|
|
46
|
+
filename: string;
|
|
47
|
+
data: Uint8Array;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
type MetadataEntry = {
|
|
51
|
+
id?: string | undefined;
|
|
52
|
+
type: ElementName;
|
|
53
|
+
properties: Record<string, string>;
|
|
54
|
+
value: string | undefined;
|
|
55
|
+
};
|
|
56
|
+
type EpubMetadata = MetadataEntry[];
|
|
57
|
+
interface DcSubject {
|
|
58
|
+
value: string;
|
|
59
|
+
authority: string;
|
|
60
|
+
term: string;
|
|
61
|
+
}
|
|
62
|
+
interface AlternateScript {
|
|
63
|
+
name: string;
|
|
64
|
+
locale: Intl.Locale;
|
|
65
|
+
}
|
|
66
|
+
interface DcCreator {
|
|
67
|
+
name: string;
|
|
68
|
+
role?: string;
|
|
69
|
+
roleScheme?: string;
|
|
70
|
+
fileAs?: string;
|
|
71
|
+
alternateScripts?: AlternateScript[];
|
|
72
|
+
}
|
|
73
|
+
interface DublinCore {
|
|
74
|
+
title: string;
|
|
75
|
+
language: Intl.Locale;
|
|
76
|
+
identifier: string;
|
|
77
|
+
date?: Date;
|
|
78
|
+
subjects?: Array<string | DcSubject>;
|
|
79
|
+
creators?: DcCreator[];
|
|
80
|
+
contributors?: DcCreator[];
|
|
81
|
+
type?: string;
|
|
82
|
+
}
|
|
83
|
+
interface Collection {
|
|
84
|
+
name: string;
|
|
85
|
+
type?: string;
|
|
86
|
+
position?: string;
|
|
87
|
+
}
|
|
88
|
+
type PackageElement = XmlElement<"package"> | XmlElement<"opf:package">;
|
|
89
|
+
/**
|
|
90
|
+
* A single EPUB instance.
|
|
91
|
+
*
|
|
92
|
+
* The entire EPUB contents will be read into memory.
|
|
93
|
+
*
|
|
94
|
+
* Example usage:
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { Epub, getBody, findByName, textContent } from '@smoores/epub';
|
|
98
|
+
*
|
|
99
|
+
* const epub = await Epub.from('./path/to/book.epub');
|
|
100
|
+
* const title = await epub.getTitle();
|
|
101
|
+
* const spineItems = await epub.getSpineItems();
|
|
102
|
+
* const chptOne = spineItems[0];
|
|
103
|
+
* const chptOneXml = await epub.readXhtmlItemContents(chptOne.id);
|
|
104
|
+
*
|
|
105
|
+
* const body = getBody(chptOneXml);
|
|
106
|
+
* const h1 = Epub.findXmlChildByName('h1', body);
|
|
107
|
+
* const headingText = textContent(h1);
|
|
108
|
+
*
|
|
109
|
+
* await epub.setTitle(headingText);
|
|
110
|
+
* await epub.writeToFile('./path/to/updated.epub');
|
|
111
|
+
* await epub.close();
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @link https://www.w3.org/TR/epub-33/
|
|
115
|
+
*/
|
|
116
|
+
declare class Epub {
|
|
117
|
+
private entries;
|
|
118
|
+
private onClose?;
|
|
119
|
+
static xmlParser: XMLParser;
|
|
120
|
+
static xhtmlParser: XMLParser;
|
|
121
|
+
static xmlBuilder: XMLBuilder;
|
|
122
|
+
static xhtmlBuilder: XMLBuilder;
|
|
123
|
+
/**
|
|
124
|
+
* Format a duration, provided as a number of seconds, as
|
|
125
|
+
* a SMIL clock value, to be used for Media Overlays.
|
|
126
|
+
*
|
|
127
|
+
* @link https://www.w3.org/TR/epub-33/#sec-duration
|
|
128
|
+
*/
|
|
129
|
+
static formatSmilDuration(duration: number): string;
|
|
130
|
+
/**
|
|
131
|
+
* Given an XML structure representing a complete XHTML document,
|
|
132
|
+
* add a `link` element to the `head` of the document.
|
|
133
|
+
*
|
|
134
|
+
* This method modifies the provided XML structure.
|
|
135
|
+
*/
|
|
136
|
+
static addLinkToXhtmlHead(xml: ParsedXml, link: {
|
|
137
|
+
rel: string;
|
|
138
|
+
href: string;
|
|
139
|
+
type: string;
|
|
140
|
+
}): void;
|
|
141
|
+
/**
|
|
142
|
+
* Given an XML structure representing a complete XHTML document,
|
|
143
|
+
* return the sub-structure representing the children of the
|
|
144
|
+
* document's body element.
|
|
145
|
+
*/
|
|
146
|
+
static getXhtmlBody(xml: ParsedXml): ParsedXml;
|
|
147
|
+
static createXmlElement<Name extends ElementName>(name: Name, properties: Record<string, string>, children?: XmlNode[]): XmlElement<Name>;
|
|
148
|
+
static createXmlTextNode(text: string): XmlTextNode;
|
|
149
|
+
/**
|
|
150
|
+
* Given an XML structure representing a complete XHTML document,
|
|
151
|
+
* return a string representing the concatenation of all text nodes
|
|
152
|
+
* in the document.
|
|
153
|
+
*/
|
|
154
|
+
static getXhtmlTextContent(xml: ParsedXml): string;
|
|
155
|
+
/**
|
|
156
|
+
* Given an XMLElement, return its tag name.
|
|
157
|
+
*/
|
|
158
|
+
static getXmlElementName<Name extends ElementName>(element: XmlElement<Name>): Name;
|
|
159
|
+
/**
|
|
160
|
+
* Given an XMLElement, return a list of its children
|
|
161
|
+
*/
|
|
162
|
+
static getXmlChildren<Name extends ElementName>(element: XmlElement<Name>): ParsedXml;
|
|
163
|
+
static replaceXmlChildren<Name extends ElementName>(element: XmlElement<Name>, children: XmlNode[]): void;
|
|
164
|
+
/**
|
|
165
|
+
* Given an XML structure, find the first child matching
|
|
166
|
+
* the provided name and optional filter.
|
|
167
|
+
*/
|
|
168
|
+
static findXmlChildByName<Name extends ElementName>(name: Name, xml: ParsedXml, filter?: (node: XmlNode) => boolean): XmlElement<Name> | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* Given an XMLNode, determine whether it represents
|
|
171
|
+
* a text node or an XML element.
|
|
172
|
+
*/
|
|
173
|
+
static isXmlTextNode(node: XmlNode): node is XmlTextNode;
|
|
174
|
+
private zipWriter;
|
|
175
|
+
private dataWriter;
|
|
176
|
+
private rootfile;
|
|
177
|
+
private manifest;
|
|
178
|
+
private spine;
|
|
179
|
+
private packageMutex;
|
|
180
|
+
protected constructor(entries: EpubEntry[], onClose?: (() => Promise<void> | void) | undefined);
|
|
181
|
+
/**
|
|
182
|
+
* Close the Epub. Must be called before the Epub goes out
|
|
183
|
+
* of scope/is garbage collected.
|
|
184
|
+
*/
|
|
185
|
+
close(): Promise<void>;
|
|
186
|
+
/**
|
|
187
|
+
* Construct an Epub instance, optionally beginning
|
|
188
|
+
* with the provided metadata.
|
|
189
|
+
*
|
|
190
|
+
* @param dublinCore Core metadata terms
|
|
191
|
+
* @param additionalMetadata An array of additional metadata entries
|
|
192
|
+
*/
|
|
193
|
+
static create({ title, language, identifier, date, subjects, type, creators, contributors, }: DublinCore, additionalMetadata?: EpubMetadata): Promise<Epub>;
|
|
194
|
+
/**
|
|
195
|
+
* Construct an Epub instance by reading an existing EPUB
|
|
196
|
+
* publication.
|
|
197
|
+
*
|
|
198
|
+
* @param pathOrData Must be either a string representing the
|
|
199
|
+
* path to an EPUB file on disk, or a Uint8Array representing
|
|
200
|
+
* the data of the EPUB publication.
|
|
201
|
+
*/
|
|
202
|
+
static from(pathOrData: string | Uint8Array): Promise<Epub>;
|
|
203
|
+
private getEntry;
|
|
204
|
+
private removeEntry;
|
|
205
|
+
private getFileData;
|
|
206
|
+
private getRootfile;
|
|
207
|
+
private migratePackageDocument;
|
|
208
|
+
private getPackageDocument;
|
|
209
|
+
private getPackageElement;
|
|
210
|
+
/**
|
|
211
|
+
* Safely modify the package document, without race conditions.
|
|
212
|
+
*
|
|
213
|
+
* Since the reading the package document is an async process,
|
|
214
|
+
* multiple simultaneously dispatched function calls that all
|
|
215
|
+
* attempt to modify it can clobber each other's changes. This
|
|
216
|
+
* method uses a mutex to ensure that each update runs exclusively.
|
|
217
|
+
*
|
|
218
|
+
* @param producer The function to update the package document. If
|
|
219
|
+
* it returns a new package document, that will be persisted, otherwise
|
|
220
|
+
* it will be assumed that the package document was modified in place.
|
|
221
|
+
*/
|
|
222
|
+
private withPackage;
|
|
223
|
+
/**
|
|
224
|
+
* Retrieve the manifest for the Epub.
|
|
225
|
+
*
|
|
226
|
+
* This is represented as a map from each manifest items'
|
|
227
|
+
* id to the rest of its properties.
|
|
228
|
+
*
|
|
229
|
+
* @link https://www.w3.org/TR/epub-33/#sec-pkg-manifest
|
|
230
|
+
*/
|
|
231
|
+
getManifest(): Promise<Record<string, ManifestItem>>;
|
|
232
|
+
/**
|
|
233
|
+
* Returns the first index in the metadata element's children array
|
|
234
|
+
* that matches the provided predicate.
|
|
235
|
+
*
|
|
236
|
+
* Note: This may technically be different than the index in the
|
|
237
|
+
* getMetadata() array, as it includes non-metadata nodes, like
|
|
238
|
+
* text nodes. These are technically not allowed, but may exist,
|
|
239
|
+
* nonetheless. As consumers only ever see the getMetadata()
|
|
240
|
+
* array, this method is only meant to be used internally.
|
|
241
|
+
*/
|
|
242
|
+
private findMetadataIndex;
|
|
243
|
+
/**
|
|
244
|
+
* Returns the item in the metadata element's children array
|
|
245
|
+
* that matches the provided predicate.
|
|
246
|
+
*/
|
|
247
|
+
findMetadataItem(predicate: (entry: MetadataEntry) => boolean): Promise<{
|
|
248
|
+
id: string | undefined;
|
|
249
|
+
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}`;
|
|
250
|
+
properties: {
|
|
251
|
+
[k: string]: string;
|
|
252
|
+
};
|
|
253
|
+
value: string | undefined;
|
|
254
|
+
} | null>;
|
|
255
|
+
/**
|
|
256
|
+
* Returns the item in the metadata element's children array
|
|
257
|
+
* that matches the provided predicate.
|
|
258
|
+
*/
|
|
259
|
+
findAllMetadataItems(predicate: (entry: MetadataEntry) => boolean): Promise<{
|
|
260
|
+
id: string | undefined;
|
|
261
|
+
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}`;
|
|
262
|
+
properties: {
|
|
263
|
+
[k: string]: string;
|
|
264
|
+
};
|
|
265
|
+
value: string | undefined;
|
|
266
|
+
}[]>;
|
|
267
|
+
private static parseMetadataItem;
|
|
268
|
+
/**
|
|
269
|
+
* Retrieve the metadata entries for the Epub.
|
|
270
|
+
*
|
|
271
|
+
* This is represented as an array of metadata entries,
|
|
272
|
+
* in the order that they're presented in the Epub package document.
|
|
273
|
+
*
|
|
274
|
+
* For more useful semantic representations of metadata, use
|
|
275
|
+
* specific methods such as `getTitle()` and `getAuthors()`.
|
|
276
|
+
*
|
|
277
|
+
* @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
|
|
278
|
+
*/
|
|
279
|
+
getMetadata(): Promise<EpubMetadata>;
|
|
280
|
+
/**
|
|
281
|
+
* Even "EPUB 3" publications sometimes still only use the
|
|
282
|
+
* EPUB 2 specification for identifying the cover image.
|
|
283
|
+
* This is a private method that is used as a fallback if
|
|
284
|
+
* we fail to find the cover image according to the EPUB 3
|
|
285
|
+
* spec.
|
|
286
|
+
*/
|
|
287
|
+
private getEpub2CoverImage;
|
|
288
|
+
/**
|
|
289
|
+
* Retrieve the cover image manifest item.
|
|
290
|
+
*
|
|
291
|
+
* This does not return the actual image data. To
|
|
292
|
+
* retrieve the image data, pass this item's id to
|
|
293
|
+
* epub.readItemContents, or use epub.getCoverImage()
|
|
294
|
+
* instead.
|
|
295
|
+
*
|
|
296
|
+
* @link https://www.w3.org/TR/epub-33/#sec-cover-image
|
|
297
|
+
*/
|
|
298
|
+
getCoverImageItem(): Promise<ManifestItem | null>;
|
|
299
|
+
/**
|
|
300
|
+
* Retrieve the cover image data as a byte array.
|
|
301
|
+
*
|
|
302
|
+
* This does not include, for example, the cover image's
|
|
303
|
+
* filename or mime type. To retrieve the image manifest
|
|
304
|
+
* item, use epub.getCoverImageItem().
|
|
305
|
+
*
|
|
306
|
+
* @link https://www.w3.org/TR/epub-33/#sec-cover-image
|
|
307
|
+
*/
|
|
308
|
+
getCoverImage(): Promise<Uint8Array<ArrayBufferLike> | null>;
|
|
309
|
+
/**
|
|
310
|
+
* Set the cover image for the EPUB.
|
|
311
|
+
*
|
|
312
|
+
* Adds a manifest item with the `cover-image` property, per
|
|
313
|
+
* the EPUB 3 spec, and then writes the provided image data to
|
|
314
|
+
* the provided href within the publication.
|
|
315
|
+
*/
|
|
316
|
+
setCoverImage(href: string, data: Uint8Array): Promise<void>;
|
|
317
|
+
/**
|
|
318
|
+
* Retrieve the publication date from the dc:date element
|
|
319
|
+
* in the EPUB metadata as a Date object.
|
|
320
|
+
*
|
|
321
|
+
* If there is no dc:date element, returns null.
|
|
322
|
+
*
|
|
323
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dcdate
|
|
324
|
+
*/
|
|
325
|
+
getPublicationDate(): Promise<Date | null>;
|
|
326
|
+
/**
|
|
327
|
+
* Set the dc:date metadata element with the provided date.
|
|
328
|
+
*
|
|
329
|
+
* Updates the existing dc:date element if one exists.
|
|
330
|
+
* Otherwise creates a new element
|
|
331
|
+
*
|
|
332
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dcdate
|
|
333
|
+
*/
|
|
334
|
+
setPublicationDate(date: Date): Promise<void>;
|
|
335
|
+
/**
|
|
336
|
+
* Set the dc:type metadata element.
|
|
337
|
+
*
|
|
338
|
+
* Updates the existing dc:type element if one exists.
|
|
339
|
+
* Otherwise creates a new element.
|
|
340
|
+
*
|
|
341
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dctype
|
|
342
|
+
*/
|
|
343
|
+
setType(type: string): Promise<void>;
|
|
344
|
+
/**
|
|
345
|
+
* Retrieve the publication type from the dc:type element
|
|
346
|
+
* in the EPUB metadata.
|
|
347
|
+
*
|
|
348
|
+
* If there is no dc:type element, returns null.
|
|
349
|
+
*
|
|
350
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dctype
|
|
351
|
+
*/
|
|
352
|
+
getType(): Promise<MetadataEntry | null>;
|
|
353
|
+
/**
|
|
354
|
+
* Add a subject to the EPUB metadata.
|
|
355
|
+
*
|
|
356
|
+
* @param subject May be a string representing just a schema-less
|
|
357
|
+
* subject name, or a DcSubject object
|
|
358
|
+
*
|
|
359
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dcsubject
|
|
360
|
+
*/
|
|
361
|
+
addSubject(subject: string | DcSubject): Promise<void>;
|
|
362
|
+
/**
|
|
363
|
+
* Remove a subject from the EPUB metadata.
|
|
364
|
+
*
|
|
365
|
+
* Removes the subject at the provided index. This index
|
|
366
|
+
* refers to the array returned by `epub.getSubjects()`.
|
|
367
|
+
*
|
|
368
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
|
|
369
|
+
*/
|
|
370
|
+
removeSubject(index: number): Promise<void>;
|
|
371
|
+
/**
|
|
372
|
+
* Retrieve the list of subjects for this EPUB.
|
|
373
|
+
*
|
|
374
|
+
* Subjects without associated authority and term metadata
|
|
375
|
+
* will be returned as strings. Otherwise, they will
|
|
376
|
+
* be represented as DcSubject objects, with a value,
|
|
377
|
+
* authority, and term.
|
|
378
|
+
*
|
|
379
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dcsubject
|
|
380
|
+
*/
|
|
381
|
+
getSubjects(): Promise<(string | DcSubject)[]>;
|
|
382
|
+
/**
|
|
383
|
+
* Retrieve the Epub's language as specified in its
|
|
384
|
+
* package document metadata.
|
|
385
|
+
*
|
|
386
|
+
* If no language metadata is specified, returns null.
|
|
387
|
+
* Returns the language as an Intl.Locale instance.
|
|
388
|
+
*
|
|
389
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dclanguage
|
|
390
|
+
*/
|
|
391
|
+
getLanguage(): Promise<Intl.Locale | null>;
|
|
392
|
+
/**
|
|
393
|
+
* Update the Epub's language metadata entry.
|
|
394
|
+
*
|
|
395
|
+
* Updates the existing dc:language element if one exists.
|
|
396
|
+
* Otherwise creates a new element
|
|
397
|
+
*
|
|
398
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dclanguage
|
|
399
|
+
*/
|
|
400
|
+
setLanguage(locale: Intl.Locale): Promise<void>;
|
|
401
|
+
/**
|
|
402
|
+
* Retrieve the title of the Epub.
|
|
403
|
+
*
|
|
404
|
+
* @param main Optional - whether to return only the first title segment
|
|
405
|
+
* if multiple are found. Otherwise, will follow the spec to combine title
|
|
406
|
+
* segments
|
|
407
|
+
*
|
|
408
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
|
|
409
|
+
*/
|
|
410
|
+
getTitle(expanded?: boolean): Promise<string | null>;
|
|
411
|
+
/**
|
|
412
|
+
* Retrieve the subtitle of the Epub, if it exists.
|
|
413
|
+
*
|
|
414
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
|
|
415
|
+
*/
|
|
416
|
+
getSubtitle(): Promise<string | null>;
|
|
417
|
+
/**
|
|
418
|
+
* Retrieve all title entries of the Epub.
|
|
419
|
+
*
|
|
420
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
|
|
421
|
+
*/
|
|
422
|
+
getTitles(): Promise<{
|
|
423
|
+
title: string;
|
|
424
|
+
type: string | null;
|
|
425
|
+
}[]>;
|
|
426
|
+
/**
|
|
427
|
+
* Update the Epub's description metadata entry.
|
|
428
|
+
*
|
|
429
|
+
* Updates the existing dc:description element if one exists.
|
|
430
|
+
* Otherwise creates a new element. Any non-ASCII symbols,
|
|
431
|
+
* `&`, `<`, `>`, `"`, `'`, and `\``` will be encoded as HTML entities.
|
|
432
|
+
*/
|
|
433
|
+
setDescription(description: string): Promise<void>;
|
|
434
|
+
/**
|
|
435
|
+
* Retrieve the Epub's description as specified in its
|
|
436
|
+
* package document metadata.
|
|
437
|
+
*
|
|
438
|
+
* If no description metadata is specified, returns null.
|
|
439
|
+
* Returns the description as a string. Descriptions may
|
|
440
|
+
* include HTML markup.
|
|
441
|
+
*/
|
|
442
|
+
getDescription(): Promise<string | null>;
|
|
443
|
+
/**
|
|
444
|
+
* Return the set of custom vocabulary prefixes set on this publication's
|
|
445
|
+
* root package element.
|
|
446
|
+
*
|
|
447
|
+
* Returns a map from prefix to URI
|
|
448
|
+
*
|
|
449
|
+
* @link https://www.w3.org/TR/epub-33/#sec-prefix-attr
|
|
450
|
+
*/
|
|
451
|
+
getPackageVocabularyPrefixes(): Promise<Record<string, string>>;
|
|
452
|
+
/**
|
|
453
|
+
* Set a custom vocabulary prefix on the root package element.
|
|
454
|
+
*
|
|
455
|
+
* @link https://www.w3.org/TR/epub-33/#sec-prefix-attr
|
|
456
|
+
*/
|
|
457
|
+
setPackageVocabularyPrefix(prefix: string, uri: string): Promise<void>;
|
|
458
|
+
/**
|
|
459
|
+
* Set the title of the Epub.
|
|
460
|
+
*
|
|
461
|
+
* This will replace all existing dc:title elements with
|
|
462
|
+
* this title. It will be given title-type "main".
|
|
463
|
+
*
|
|
464
|
+
* To set specific titles and their types, use epub.setTitles().
|
|
465
|
+
*
|
|
466
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
|
|
467
|
+
*/
|
|
468
|
+
setTitle(title: string): Promise<void>;
|
|
469
|
+
setTitles(entries: {
|
|
470
|
+
title: string;
|
|
471
|
+
type: string | null;
|
|
472
|
+
}[]): Promise<void>;
|
|
473
|
+
/**
|
|
474
|
+
* Retrieve the list of collections.
|
|
475
|
+
*/
|
|
476
|
+
getCollections(): Promise<Collection[]>;
|
|
477
|
+
/**
|
|
478
|
+
* Add a collection to the EPUB metadata.
|
|
479
|
+
*
|
|
480
|
+
* If index is provided, the collection will be placed at
|
|
481
|
+
* that index in the list of collections. Otherwise, it
|
|
482
|
+
* will be added to the end of the list.
|
|
483
|
+
*/
|
|
484
|
+
addCollection(collection: Collection, index?: number): Promise<void>;
|
|
485
|
+
/**
|
|
486
|
+
* Remove a collection from the EPUB metadata.
|
|
487
|
+
*
|
|
488
|
+
* Removes the collection at the provided index. This index
|
|
489
|
+
* refers to the array returned by `epub.getCollections()`.
|
|
490
|
+
*/
|
|
491
|
+
removeCollection(index: number): Promise<void>;
|
|
492
|
+
/**
|
|
493
|
+
* Retrieve the list of creators.
|
|
494
|
+
*
|
|
495
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
|
|
496
|
+
*/
|
|
497
|
+
getCreators(type?: "creator" | "contributor"): Promise<DcCreator[]>;
|
|
498
|
+
/**
|
|
499
|
+
* Retrieve the list of contributors.
|
|
500
|
+
*
|
|
501
|
+
* This is a convenience method for
|
|
502
|
+
* `epub.getCreators('contributor')`.
|
|
503
|
+
*
|
|
504
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dccontributor
|
|
505
|
+
*/
|
|
506
|
+
getContributors(): Promise<DcCreator[]>;
|
|
507
|
+
/**
|
|
508
|
+
* Add a creator to the EPUB metadata.
|
|
509
|
+
*
|
|
510
|
+
* If index is provided, the creator will be placed at
|
|
511
|
+
* that index in the list of creators. Otherwise, it
|
|
512
|
+
* will be added to the end of the list.
|
|
513
|
+
*
|
|
514
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
|
|
515
|
+
*/
|
|
516
|
+
addCreator(creator: DcCreator, index?: number, type?: "creator" | "contributor"): Promise<void>;
|
|
517
|
+
/**
|
|
518
|
+
* Remove a creator from the EPUB metadata.
|
|
519
|
+
*
|
|
520
|
+
* Removes the creator at the provided index. This index
|
|
521
|
+
* refers to the array returned by `epub.getCreators()`.
|
|
522
|
+
*
|
|
523
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
|
|
524
|
+
*/
|
|
525
|
+
removeCreator(index: number, type?: "creator" | "contributor"): Promise<void>;
|
|
526
|
+
/**
|
|
527
|
+
* Remove a contributor from the EPUB metadata.
|
|
528
|
+
*
|
|
529
|
+
* Removes the contributor at the provided index. This index
|
|
530
|
+
* refers to the array returned by `epub.getContributors()`.
|
|
531
|
+
*
|
|
532
|
+
* This is a convenience method for
|
|
533
|
+
* `epub.removeCreator(index, 'contributor')`.
|
|
534
|
+
*
|
|
535
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
|
|
536
|
+
*/
|
|
537
|
+
removeContributor(index: number): Promise<void>;
|
|
538
|
+
/**
|
|
539
|
+
* Add a contributor to the EPUB metadata.
|
|
540
|
+
*
|
|
541
|
+
* If index is provided, the creator will be placed at
|
|
542
|
+
* that index in the list of creators. Otherwise, it
|
|
543
|
+
* will be added to the end of the list.
|
|
544
|
+
*
|
|
545
|
+
* This is a convenience method for
|
|
546
|
+
* `epub.addCreator(contributor, index, 'contributor')`.
|
|
547
|
+
*
|
|
548
|
+
* @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
|
|
549
|
+
*/
|
|
550
|
+
addContributor(contributor: DcCreator, index?: number): Promise<void>;
|
|
551
|
+
private getSpine;
|
|
552
|
+
/**
|
|
553
|
+
* Retrieve the manifest items that make up the Epub's spine.
|
|
554
|
+
*
|
|
555
|
+
* The spine specifies the order that the contents of the Epub
|
|
556
|
+
* should be displayed to users by default.
|
|
557
|
+
*
|
|
558
|
+
* @link https://www.w3.org/TR/epub-33/#sec-spine-elem
|
|
559
|
+
*/
|
|
560
|
+
getSpineItems(): Promise<ManifestItem[]>;
|
|
561
|
+
/**
|
|
562
|
+
* Add an item to the spine of the EPUB.
|
|
563
|
+
*
|
|
564
|
+
* If `index` is undefined, the item will be added
|
|
565
|
+
* to the end of the spine. Otherwise it will be
|
|
566
|
+
* inserted at the specified index.
|
|
567
|
+
*
|
|
568
|
+
* If the manifestId does not correspond to an item
|
|
569
|
+
* in the manifest, this will throw an error.
|
|
570
|
+
*
|
|
571
|
+
* @link https://www.w3.org/TR/epub-33/#sec-spine-elem
|
|
572
|
+
*/
|
|
573
|
+
addSpineItem(manifestId: string, index?: number): Promise<void>;
|
|
574
|
+
/**
|
|
575
|
+
* Remove the spine item at the specified index.
|
|
576
|
+
*
|
|
577
|
+
* @link https://www.w3.org/TR/epub-33/#sec-spine-elem
|
|
578
|
+
*/
|
|
579
|
+
removeSpineItem(index: number): Promise<void>;
|
|
580
|
+
/**
|
|
581
|
+
* Returns a Zip Entry path for an HREF
|
|
582
|
+
*/
|
|
583
|
+
private resolveHref;
|
|
584
|
+
/**
|
|
585
|
+
* Retrieve the contents of a manifest item, given its id.
|
|
586
|
+
*
|
|
587
|
+
* @param id The id of the manifest item to retrieve
|
|
588
|
+
* @param [encoding] Optional - must be the string "utf-8". If
|
|
589
|
+
* provided, the function will encode the data into a unicode string.
|
|
590
|
+
* Otherwise, the data will be returned as a byte array.
|
|
591
|
+
*
|
|
592
|
+
* @link https://www.w3.org/TR/epub-33/#sec-contentdocs
|
|
593
|
+
*/
|
|
594
|
+
readItemContents(id: string): Promise<Uint8Array>;
|
|
595
|
+
readItemContents(id: string, encoding: "utf-8"): Promise<string>;
|
|
596
|
+
/**
|
|
597
|
+
* Create a new XHTML document with the given body
|
|
598
|
+
* and head.
|
|
599
|
+
*
|
|
600
|
+
* @param body The XML nodes to place in the body of the document
|
|
601
|
+
* @param head Optional - the XMl nodes to place in the head
|
|
602
|
+
* @param language Optional - defaults to the EPUB's language
|
|
603
|
+
*/
|
|
604
|
+
createXhtmlDocument(body: ParsedXml, head?: ParsedXml, language?: Intl.Locale): Promise<(XmlElement<"?xml"> | XmlElement<"html">)[]>;
|
|
605
|
+
/**
|
|
606
|
+
* Retrieves the contents of an XHTML item, given its manifest id.
|
|
607
|
+
*
|
|
608
|
+
* @param id The id of the manifest item to retrieve
|
|
609
|
+
* @param [as] Optional - whether to return the parsed XML document tree,
|
|
610
|
+
* or the concatenated text of the document. Defaults to the parsed XML tree.
|
|
611
|
+
*
|
|
612
|
+
* @link https://www.w3.org/TR/epub-33/#sec-xhtml
|
|
613
|
+
*/
|
|
614
|
+
readXhtmlItemContents(id: string, as?: "xhtml"): Promise<ParsedXml>;
|
|
615
|
+
readXhtmlItemContents(id: string, as: "text"): Promise<string>;
|
|
616
|
+
private writeEntryContents;
|
|
617
|
+
/**
|
|
618
|
+
* Write new contents for an existing manifest item,
|
|
619
|
+
* specified by its id.
|
|
620
|
+
*
|
|
621
|
+
* The id must reference an existing manifest item. If
|
|
622
|
+
* creating a new item, use `epub.addManifestItem()` instead.
|
|
623
|
+
*
|
|
624
|
+
* @param id The id of the manifest item to write new contents for
|
|
625
|
+
* @param contents The new contents. May be either a utf-8 encoded string
|
|
626
|
+
* or a byte array, as determined by the encoding
|
|
627
|
+
* @param [encoding] Optional - must be the string "utf-8". If provided,
|
|
628
|
+
* the contents will be interpreted as a unicode string. Otherwise, the
|
|
629
|
+
* contents must be a byte array.
|
|
630
|
+
*
|
|
631
|
+
* @link https://www.w3.org/TR/epub-33/#sec-contentdocs
|
|
632
|
+
*/
|
|
633
|
+
writeItemContents(id: string, contents: Uint8Array): Promise<void>;
|
|
634
|
+
writeItemContents(id: string, contents: string, encoding: "utf-8"): Promise<void>;
|
|
635
|
+
/**
|
|
636
|
+
* Write new contents for an existing XHTML item,
|
|
637
|
+
* specified by its id.
|
|
638
|
+
*
|
|
639
|
+
* The id must reference an existing manifest item. If
|
|
640
|
+
* creating a new item, use `epub.addManifestItem()` instead.
|
|
641
|
+
*
|
|
642
|
+
* @param id The id of the manifest item to write new contents for
|
|
643
|
+
* @param contents The new contents. Must be a parsed XML tree.
|
|
644
|
+
*
|
|
645
|
+
* @link https://www.w3.org/TR/epub-33/#sec-xhtml
|
|
646
|
+
*/
|
|
647
|
+
writeXhtmlItemContents(id: string, contents: ParsedXml): Promise<void>;
|
|
648
|
+
removeManifestItem(id: string): Promise<void>;
|
|
649
|
+
/**
|
|
650
|
+
* Create a new manifest item and write its contents to a
|
|
651
|
+
* new entry.
|
|
652
|
+
*
|
|
653
|
+
* @param id The id of the manifest item to write new contents for
|
|
654
|
+
* @param contents The new contents. May be either a parsed XML tree
|
|
655
|
+
* or a unicode string, as determined by the `as` argument.
|
|
656
|
+
* @param encoding Optional - whether to interpret contents as a parsed
|
|
657
|
+
* XML tree, a unicode string, or a byte array. Defaults to a byte array.
|
|
658
|
+
*
|
|
659
|
+
* @link https://www.w3.org/TR/epub-33/#sec-pkg-manifest
|
|
660
|
+
* @link https://www.w3.org/TR/epub-33/#sec-contentdocs
|
|
661
|
+
*/
|
|
662
|
+
addManifestItem(item: ManifestItem, contents: ParsedXml, encoding: "xml"): Promise<void>;
|
|
663
|
+
addManifestItem(item: ManifestItem, contents: string, encoding: "utf-8"): Promise<void>;
|
|
664
|
+
addManifestItem(item: ManifestItem, contents: Uint8Array): Promise<void>;
|
|
665
|
+
/**
|
|
666
|
+
* Update the manifest entry for an existing item.
|
|
667
|
+
*
|
|
668
|
+
* To update the contents of an entry, use `epub.writeItemContents()`
|
|
669
|
+
* or `epub.writeXhtmlItemContents()`
|
|
670
|
+
*
|
|
671
|
+
* @link https://www.w3.org/TR/epub-33/#sec-pkg-manifest
|
|
672
|
+
*/
|
|
673
|
+
updateManifestItem(id: string, newItem: Omit<ManifestItem, "id">): Promise<void>;
|
|
674
|
+
/**
|
|
675
|
+
* Add a new metadata entry to the Epub.
|
|
676
|
+
*
|
|
677
|
+
* This method, like `epub.getMetadata()`, operates on
|
|
678
|
+
* metadata entries. For more useful semantic representations
|
|
679
|
+
* of metadata, use specific methods such as `setTitle()` and
|
|
680
|
+
* `setLanguage()`.
|
|
681
|
+
*
|
|
682
|
+
* @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
|
|
683
|
+
*/
|
|
684
|
+
addMetadata(entry: MetadataEntry): Promise<void>;
|
|
685
|
+
/**
|
|
686
|
+
* Replace a metadata entry with a new one.
|
|
687
|
+
*
|
|
688
|
+
* The `predicate` argument will be used to determine which entry
|
|
689
|
+
* to replace. The first metadata entry that matches the
|
|
690
|
+
* predicate will be replaced.
|
|
691
|
+
*
|
|
692
|
+
* @param predicate Calls predicate once for each metadata entry,
|
|
693
|
+
* until it finds one where predicate returns true
|
|
694
|
+
* @param entry The new entry to replace the found entry with
|
|
695
|
+
*
|
|
696
|
+
* @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
|
|
697
|
+
*/
|
|
698
|
+
replaceMetadata(predicate: (entry: MetadataEntry) => boolean, entry: MetadataEntry): Promise<void>;
|
|
699
|
+
/**
|
|
700
|
+
* Write the current contents of the Epub to a new
|
|
701
|
+
* Uint8Array.
|
|
702
|
+
*
|
|
703
|
+
* This _does not_ close the Epub. It can continue to
|
|
704
|
+
* be modified after it has been written to disk. Use
|
|
705
|
+
* `epub.close()` to close the Epub.
|
|
706
|
+
*
|
|
707
|
+
* When this method is called, the "dcterms:modified"
|
|
708
|
+
* meta tag is automatically updated to the current UTC
|
|
709
|
+
* timestamp.
|
|
710
|
+
*/
|
|
711
|
+
writeToArray(): Promise<Uint8Array<ArrayBufferLike>>;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
export { type AlternateScript, type Collection, type DcCreator, type DcSubject, type DublinCore, type ElementName, Epub, type EpubMetadata, type ManifestItem, type MetadataEntry, type PackageElement, type ParsedXml, type XmlElement, type XmlNode, type XmlTextNode };
|