@likecoin/epub-ts 0.4.5 → 0.4.7

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
@@ -49,6 +49,30 @@ fileInput.addEventListener("change", async (event) => {
49
49
  });
50
50
  ```
51
51
 
52
+ ### Node.js (parsing only)
53
+
54
+ Parse EPUB metadata, spine, navigation, and section content without a browser. Requires [`linkedom`](https://github.com/WebReflection/linkedom) as a peer dependency.
55
+
56
+ ```bash
57
+ npm install linkedom
58
+ ```
59
+
60
+ ```typescript
61
+ import { Book } from "@likecoin/epub-ts/node";
62
+ import { readFileSync } from "node:fs";
63
+
64
+ const data = readFileSync("book.epub");
65
+ const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
66
+ const book = new Book(arrayBuffer);
67
+ await book.opened;
68
+
69
+ console.log(book.packaging.metadata.title);
70
+ console.log(book.navigation.toc.map(item => item.label));
71
+
72
+ const section = book.spine.first();
73
+ const html = await section.render(book.archive.request.bind(book.archive));
74
+ ```
75
+
52
76
  ## Migration from epubjs
53
77
 
54
78
  Drop-in replacement. Change your import:
@@ -102,12 +126,11 @@ Key classes:
102
126
 
103
127
  ## Supported Environments
104
128
 
105
- | Environment | Notes |
106
- |-------------|-------|
107
- | Modern browsers | Chrome, Firefox, Safari, Edge (requires DOM) |
108
- | Vite / webpack | ESM or CJS imports |
109
-
110
- > **Note**: This library requires a DOM environment. Node.js support (parsing-only, no rendering) is planned but not yet available.
129
+ | Environment | Import | Notes |
130
+ |-------------|--------|-------|
131
+ | Modern browsers | `@likecoin/epub-ts` | Chrome, Firefox, Safari, Edge |
132
+ | Vite / webpack | `@likecoin/epub-ts` | ESM or CJS |
133
+ | Node.js 18+ | `@likecoin/epub-ts/node` | Parsing only (no rendering); requires `linkedom` peer dep |
111
134
 
112
135
  ## What's Changed from epubjs
113
136
 
@@ -118,6 +141,7 @@ Key classes:
118
141
  - Replaced `event-emitter` with inline typed emitter
119
142
  - Replaced `localforage` with native IndexedDB wrapper
120
143
  - Replaced `@xmldom/xmldom` with native DOMParser/XMLSerializer
144
+ - Added Node.js parsing-only entry point (`@likecoin/epub-ts/node`) with `linkedom`
121
145
  - Dropped IE8–IE11 support
122
146
 
123
147
  ## Development
@@ -114,7 +114,11 @@ declare class Annotations {
114
114
  * @param {object} styles CSS styles to assign to annotation
115
115
  * @returns {Annotation} annotation
116
116
  */
117
- declare class Annotation implements IEventEmitter {
117
+ export interface AnnotationEvents extends Record<string, any[]> {
118
+ "attach": [object | null | undefined];
119
+ "detach": [];
120
+ }
121
+ declare class Annotation implements IEventEmitter<AnnotationEvents> {
118
122
  type: string;
119
123
  cfiRange: string;
120
124
  data: Record<string, any>;
@@ -123,9 +127,9 @@ declare class Annotation implements IEventEmitter {
123
127
  cb: EventListener;
124
128
  className: string;
125
129
  styles: Record<string, string>;
126
- on: IEventEmitter["on"];
127
- off: IEventEmitter["off"];
128
- emit: IEventEmitter["emit"];
130
+ on: IEventEmitter<AnnotationEvents>["on"];
131
+ off: IEventEmitter<AnnotationEvents>["off"];
132
+ emit: IEventEmitter<AnnotationEvents>["emit"];
129
133
  constructor({ type, cfiRange, data, sectionIndex, cb, className, styles }: {
130
134
  type: string;
131
135
  cfiRange: string;
package/dist/book.d.ts CHANGED
@@ -35,6 +35,9 @@ interface BookLoadedState {
35
35
  resources: Promise<Resources>;
36
36
  displayOptions: Promise<DisplayOptions>;
37
37
  }
38
+ export interface BookEvents extends Record<string, any[]> {
39
+ "openFailed": [Error];
40
+ }
38
41
  /**
39
42
  * An Epub representation with methods for the loading, parsing and manipulation
40
43
  * of its contents.
@@ -53,7 +56,7 @@ interface BookLoadedState {
53
56
  * @example new Book("/path/to/book.epub", {})
54
57
  * @example new Book({ replacements: "blobUrl" })
55
58
  */
56
- declare class Book implements IEventEmitter {
59
+ declare class Book implements IEventEmitter<BookEvents> {
57
60
  settings: BookOptions;
58
61
  opening: defer<Book>;
59
62
  opened: Promise<Book>;
@@ -65,23 +68,23 @@ declare class Book implements IEventEmitter {
65
68
  request: RequestFunction;
66
69
  spine: Spine;
67
70
  locations: Locations;
68
- navigation: Navigation | undefined;
69
- pageList: PageList | undefined;
70
- url: Url | undefined;
71
+ navigation: Navigation;
72
+ pageList: PageList;
73
+ url: Url;
71
74
  path: Path | undefined;
72
75
  archived: boolean;
73
76
  archive: Archive | undefined;
74
77
  storage: Store | undefined;
75
- resources: Resources | undefined;
78
+ resources: Resources;
76
79
  rendition: Rendition | undefined;
77
80
  container: Container | undefined;
78
- packaging: Packaging | undefined;
81
+ packaging: Packaging;
79
82
  displayOptions: DisplayOptions | undefined;
80
83
  package: Packaging | undefined;
81
84
  cover: string;
82
- on: IEventEmitter["on"];
83
- off: IEventEmitter["off"];
84
- emit: IEventEmitter["emit"];
85
+ on: IEventEmitter<BookEvents>["on"];
86
+ off: IEventEmitter<BookEvents>["off"];
87
+ emit: IEventEmitter<BookEvents>["emit"];
85
88
  constructor(url?: string | ArrayBuffer | Blob | BookOptions, options?: BookOptions);
86
89
  /**
87
90
  * Open a epub or url
@@ -10,11 +10,22 @@ import { default as Section } from './section';
10
10
  * @param {string} cfiBase Section component of CFIs
11
11
  * @param {number} sectionIndex Index in Spine of Conntent's Section
12
12
  */
13
- declare class Contents implements IEventEmitter {
14
- on: (type: string, fn: (...args: any[]) => void) => this;
15
- off: (type: string, fn?: (...args: any[]) => void) => this;
16
- emit: (type: string, ...args: unknown[]) => void;
17
- __listeners: IEventEmitter["__listeners"];
13
+ export interface ContentsEvents {
14
+ "expand": [];
15
+ "resize": [{
16
+ width: number;
17
+ height: number;
18
+ }];
19
+ "selected": [string];
20
+ "selectedRange": [Range];
21
+ "linkClicked": [string];
22
+ [event: string]: any[];
23
+ }
24
+ declare class Contents implements IEventEmitter<ContentsEvents> {
25
+ on: IEventEmitter<ContentsEvents>["on"];
26
+ off: IEventEmitter<ContentsEvents>["off"];
27
+ emit: IEventEmitter<ContentsEvents>["emit"];
28
+ __listeners: IEventEmitter<ContentsEvents>["__listeners"];
18
29
  epubcfi: EpubCFI;
19
30
  document: Document;
20
31
  documentElement: HTMLElement;