@epubook/core 0.0.2 → 0.0.4
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 +7 -13
- package/dist/chunks/index.cjs +4 -3
- package/dist/chunks/index.mjs +4 -3
- package/dist/index.cjs +588 -24
- package/dist/index.d.ts +131 -77
- package/dist/index.mjs +550 -7
- package/package.json +4 -4
- package/dist/shared/core.2c388ac3.cjs +0 -490
- package/dist/shared/core.c365bf12.mjs +0 -461
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm i @epubook/core
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
|
-
import { Epub } from '@epubook/core'
|
|
16
|
+
import { Epub, HTML } from '@epubook/core'
|
|
17
17
|
|
|
18
18
|
const book = new Epub({
|
|
19
19
|
title: 'Test Book',
|
|
@@ -24,7 +24,9 @@ const book = new Epub({
|
|
|
24
24
|
source: 'imagine'
|
|
25
25
|
})
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
const page = new HTML('start.xhtml', '...')
|
|
28
|
+
|
|
29
|
+
book.item(page).spine(page).toc([{ title: 'Start', page }])
|
|
28
30
|
|
|
29
31
|
await book.writeFile('./test.epub')
|
|
30
32
|
```
|
|
@@ -53,24 +55,16 @@ Currently, it supports use [unbuild](https://github.com/unjs/unbuild) to transfo
|
|
|
53
55
|
|
|
54
56
|
import { defineBuildConfig } from 'unbuild';
|
|
55
57
|
|
|
56
|
-
import {
|
|
58
|
+
import { UnbuildPreset } from '@epubook/core';
|
|
57
59
|
|
|
58
60
|
export default defineBuildConfig({
|
|
59
61
|
entries: ['src/index'],
|
|
60
62
|
declaration: true,
|
|
61
63
|
clean: true,
|
|
62
64
|
rollup: {
|
|
63
|
-
emitCJS: true
|
|
64
|
-
esbuild: EsbuildOptions
|
|
65
|
+
emitCJS: true
|
|
65
66
|
},
|
|
66
|
-
|
|
67
|
-
'rollup:options'(_options, config) {
|
|
68
|
-
const plugins = config.plugins;
|
|
69
|
-
if (Array.isArray(plugins)) {
|
|
70
|
-
plugins.push(Rollup());
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
67
|
+
preset: UnbuildPreset()
|
|
74
68
|
});
|
|
75
69
|
```
|
|
76
70
|
|
package/dist/chunks/index.cjs
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
const path = require('pathe');
|
|
4
4
|
const fflate = require('fflate');
|
|
5
5
|
const fastXmlParser = require('fast-xml-parser');
|
|
6
|
-
const index = require('../
|
|
7
|
-
require('node:
|
|
6
|
+
const index = require('../index.cjs');
|
|
7
|
+
require('node:path');
|
|
8
8
|
require('node:crypto');
|
|
9
9
|
require('defu');
|
|
10
|
+
require('node:fs');
|
|
10
11
|
|
|
11
12
|
function _interopNamespaceDefault(e) {
|
|
12
13
|
const n = Object.create(null);
|
|
@@ -146,7 +147,7 @@ function makePackageDocument(opf) {
|
|
|
146
147
|
{
|
|
147
148
|
"@_refines": "#" + opf.creator().uid,
|
|
148
149
|
"@_property": "file-as",
|
|
149
|
-
"#text": opf.creator()?.fileAs ??
|
|
150
|
+
"#text": opf.creator()?.fileAs ?? opf.creator().name
|
|
150
151
|
}
|
|
151
152
|
]
|
|
152
153
|
};
|
package/dist/chunks/index.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as path from 'pathe';
|
|
2
2
|
import * as fflate from 'fflate';
|
|
3
3
|
import { XMLBuilder } from 'fast-xml-parser';
|
|
4
|
-
import {
|
|
5
|
-
import 'node:
|
|
4
|
+
import { MIMETYPE, BundleError } from '../index.mjs';
|
|
5
|
+
import 'node:path';
|
|
6
6
|
import 'node:crypto';
|
|
7
7
|
import 'defu';
|
|
8
|
+
import 'node:fs';
|
|
8
9
|
|
|
9
10
|
function toISO8601String(date) {
|
|
10
11
|
function pad(n) {
|
|
@@ -130,7 +131,7 @@ function makePackageDocument(opf) {
|
|
|
130
131
|
{
|
|
131
132
|
"@_refines": "#" + opf.creator().uid,
|
|
132
133
|
"@_property": "file-as",
|
|
133
|
-
"#text": opf.creator()?.fileAs ??
|
|
134
|
+
"#text": opf.creator()?.fileAs ?? opf.creator().name
|
|
134
135
|
}
|
|
135
136
|
]
|
|
136
137
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -1,26 +1,590 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
require('
|
|
5
|
-
require('
|
|
6
|
-
require('
|
|
7
|
-
require('
|
|
8
|
-
require('
|
|
9
|
-
require('
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const node_crypto = require('node:crypto');
|
|
5
|
+
const defu$1 = require('defu');
|
|
6
|
+
const path$1 = require('pathe');
|
|
7
|
+
const fflate = require('fflate');
|
|
8
|
+
const fastXmlParser = require('fast-xml-parser');
|
|
9
|
+
const node_fs = require('node:fs');
|
|
10
|
+
|
|
11
|
+
function _interopNamespaceDefault(e) {
|
|
12
|
+
const n = Object.create(null);
|
|
13
|
+
if (e) {
|
|
14
|
+
for (const k in e) {
|
|
15
|
+
n[k] = e[k];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
n.default = e;
|
|
19
|
+
return n;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
23
|
+
const path__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(path$1);
|
|
24
|
+
|
|
25
|
+
const Fragment = "Fragment";
|
|
26
|
+
function h(tag, attrs = {}, ...children) {
|
|
27
|
+
const sub = children.flatMap(
|
|
28
|
+
(c) => typeof c === "object" && !Array.isArray(c) && c.tag === Fragment ? c.children ?? [] : c
|
|
29
|
+
).filter((c) => c !== void 0 && c !== null && c !== false);
|
|
30
|
+
const o = {
|
|
31
|
+
tag,
|
|
32
|
+
attrs: attrs ?? {},
|
|
33
|
+
children: sub
|
|
34
|
+
};
|
|
35
|
+
return o;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const MIMETYPE = "application/epub+zip";
|
|
39
|
+
const ImageGif = "image/gif";
|
|
40
|
+
const ImageJpeg = "image/jpeg";
|
|
41
|
+
const ImagePng = "image/png";
|
|
42
|
+
const ImageSvg = "image/svg+xml";
|
|
43
|
+
const ImageWebp = "image/webp";
|
|
44
|
+
const TextCSS = "text/css";
|
|
45
|
+
const TextXHTML = "application/xhtml+xml";
|
|
46
|
+
function getImageMediaType(file) {
|
|
47
|
+
const ext = path__namespace.extname(file);
|
|
48
|
+
switch (ext) {
|
|
49
|
+
case ".gif":
|
|
50
|
+
return ImageGif;
|
|
51
|
+
case ".jpg":
|
|
52
|
+
case ".jpeg":
|
|
53
|
+
return ImageJpeg;
|
|
54
|
+
case ".png":
|
|
55
|
+
return ImagePng;
|
|
56
|
+
case ".svg":
|
|
57
|
+
return ImageSvg;
|
|
58
|
+
case ".webp":
|
|
59
|
+
return ImageWebp;
|
|
60
|
+
default:
|
|
61
|
+
return void 0;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
class ManifestItem {
|
|
66
|
+
constructor(href, id) {
|
|
67
|
+
this.optional = {};
|
|
68
|
+
this._href = href;
|
|
69
|
+
this._id = id;
|
|
70
|
+
}
|
|
71
|
+
update(info) {
|
|
72
|
+
for (const [key, value] of Object.entries(info)) {
|
|
73
|
+
if (!!value) {
|
|
74
|
+
this.optional[key] = value;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
href() {
|
|
80
|
+
return this._href;
|
|
81
|
+
}
|
|
82
|
+
id() {
|
|
83
|
+
return this._id;
|
|
84
|
+
}
|
|
85
|
+
fallback() {
|
|
86
|
+
return this.optional.fallback;
|
|
87
|
+
}
|
|
88
|
+
mediaOverlay() {
|
|
89
|
+
return this.optional.mediaOverlay;
|
|
90
|
+
}
|
|
91
|
+
mediaType() {
|
|
92
|
+
return this.optional.mediaType;
|
|
93
|
+
}
|
|
94
|
+
properties() {
|
|
95
|
+
return this.optional.properties;
|
|
96
|
+
}
|
|
97
|
+
ref() {
|
|
98
|
+
return new ManifestItemRef(this._id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
class ManifestItemRef {
|
|
102
|
+
constructor(idref) {
|
|
103
|
+
this.optional = {};
|
|
104
|
+
this._idref = idref;
|
|
105
|
+
}
|
|
106
|
+
update(info) {
|
|
107
|
+
for (const [key, value] of Object.entries(info)) {
|
|
108
|
+
if (!!value) {
|
|
109
|
+
this.optional[key] = value;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
idref() {
|
|
115
|
+
return this._idref;
|
|
116
|
+
}
|
|
117
|
+
id() {
|
|
118
|
+
return this.optional.id;
|
|
119
|
+
}
|
|
120
|
+
linear() {
|
|
121
|
+
return this.optional.linear;
|
|
122
|
+
}
|
|
123
|
+
properties() {
|
|
124
|
+
return this.optional.properties;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
class Item {
|
|
129
|
+
constructor(file, mediaType) {
|
|
130
|
+
this.file = file;
|
|
131
|
+
this.mediaType = mediaType;
|
|
132
|
+
}
|
|
133
|
+
filename() {
|
|
134
|
+
return this.file;
|
|
135
|
+
}
|
|
136
|
+
relative(from) {
|
|
137
|
+
return path__namespace$1.relative(path__namespace$1.dirname(from), this.file);
|
|
138
|
+
}
|
|
139
|
+
update(info) {
|
|
140
|
+
if (info.properties) {
|
|
141
|
+
this._properties = info.properties;
|
|
142
|
+
}
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
id() {
|
|
146
|
+
return this.file.replace(/\/|\\/g, "_").replace(/\.[\w]+$/, "");
|
|
147
|
+
}
|
|
148
|
+
manifest() {
|
|
149
|
+
return new ManifestItem(this.file, this.id()).update({
|
|
150
|
+
mediaType: this.mediaType,
|
|
151
|
+
properties: this._properties
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
itemref() {
|
|
155
|
+
return this.manifest().ref();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
class Style extends Item {
|
|
159
|
+
constructor(file, content) {
|
|
160
|
+
super(file, TextCSS);
|
|
161
|
+
this.content = content;
|
|
162
|
+
}
|
|
163
|
+
static async read(src, dst) {
|
|
164
|
+
if (!src.endsWith(".css")) {
|
|
165
|
+
return void 0;
|
|
166
|
+
}
|
|
167
|
+
const content = await node_fs.promises.readFile(src, "utf-8");
|
|
168
|
+
return new Style(dst, content);
|
|
169
|
+
}
|
|
170
|
+
async bundle() {
|
|
171
|
+
return fflate.strToU8(this.content);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
class Image extends Item {
|
|
175
|
+
constructor(file, type, data) {
|
|
176
|
+
super(file, type);
|
|
177
|
+
this.data = data;
|
|
178
|
+
}
|
|
179
|
+
static async read(file, src) {
|
|
180
|
+
const content = await node_fs.promises.readFile(src);
|
|
181
|
+
const media = getImageMediaType(src);
|
|
182
|
+
if (media) {
|
|
183
|
+
return new Image(file, media, content);
|
|
184
|
+
} else {
|
|
185
|
+
return void 0;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async bundle() {
|
|
189
|
+
return this.data;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
class HTML extends Item {
|
|
193
|
+
constructor(file, content) {
|
|
194
|
+
super(file, TextXHTML);
|
|
195
|
+
this.content = content;
|
|
196
|
+
}
|
|
197
|
+
static async read(src, dst) {
|
|
198
|
+
if (!src.endsWith(".xhtml")) {
|
|
199
|
+
return void 0;
|
|
200
|
+
}
|
|
201
|
+
const content = await node_fs.promises.readFile(src, "utf-8");
|
|
202
|
+
return new HTML(dst, content);
|
|
203
|
+
}
|
|
204
|
+
async bundle() {
|
|
205
|
+
return fflate.strToU8(this.content);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const builder = new fastXmlParser.XMLBuilder({
|
|
210
|
+
format: true,
|
|
211
|
+
ignoreAttributes: false,
|
|
212
|
+
suppressUnpairedNode: false,
|
|
213
|
+
unpairedTags: ["link"]
|
|
214
|
+
});
|
|
215
|
+
class XHTML extends Item {
|
|
216
|
+
constructor(file, meta, content) {
|
|
217
|
+
super(file, TextXHTML);
|
|
218
|
+
this._meta = meta;
|
|
219
|
+
this._content = content;
|
|
220
|
+
}
|
|
221
|
+
meta() {
|
|
222
|
+
return this._meta;
|
|
223
|
+
}
|
|
224
|
+
title() {
|
|
225
|
+
return this._meta.title;
|
|
226
|
+
}
|
|
227
|
+
language() {
|
|
228
|
+
return this._meta.language;
|
|
229
|
+
}
|
|
230
|
+
content() {
|
|
231
|
+
return this._content;
|
|
232
|
+
}
|
|
233
|
+
async bundle() {
|
|
234
|
+
return fflate.strToU8(this._content);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
class XHTMLBuilder {
|
|
238
|
+
constructor(filename) {
|
|
239
|
+
this._meta = {
|
|
240
|
+
language: "en",
|
|
241
|
+
title: ""
|
|
242
|
+
};
|
|
243
|
+
this._head = [];
|
|
244
|
+
this._body = [];
|
|
245
|
+
this._filename = filename;
|
|
246
|
+
this._meta.title = path__namespace$1.basename(filename);
|
|
247
|
+
}
|
|
248
|
+
language(value) {
|
|
249
|
+
this._meta.language = value;
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
title(value) {
|
|
253
|
+
this._meta.title = value;
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
style(...list) {
|
|
257
|
+
for (const href of list) {
|
|
258
|
+
if (typeof href === "string") {
|
|
259
|
+
this._head.push({
|
|
260
|
+
tag: "link",
|
|
261
|
+
attrs: {
|
|
262
|
+
href,
|
|
263
|
+
rel: "stylesheet",
|
|
264
|
+
type: TextCSS
|
|
265
|
+
},
|
|
266
|
+
children: [""]
|
|
267
|
+
});
|
|
268
|
+
} else {
|
|
269
|
+
this._head.push({
|
|
270
|
+
tag: "link",
|
|
271
|
+
attrs: {
|
|
272
|
+
href: href.relative(this._filename),
|
|
273
|
+
rel: "stylesheet",
|
|
274
|
+
type: TextCSS
|
|
275
|
+
},
|
|
276
|
+
children: [""]
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
head(...node) {
|
|
283
|
+
this._head.push(...node);
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
body(...node) {
|
|
287
|
+
this._body.push(...node);
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
build() {
|
|
291
|
+
const content = builder.build({
|
|
292
|
+
html: {
|
|
293
|
+
"@_xmlns": "http://www.w3.org/1999/xhtml",
|
|
294
|
+
"@_xmlns:epub": "http://www.idpf.org/2007/ops",
|
|
295
|
+
"@_lang": this._meta.language,
|
|
296
|
+
"@_xml:lang": this._meta.language,
|
|
297
|
+
head: {
|
|
298
|
+
title: this._meta.title,
|
|
299
|
+
...list(this._head)
|
|
300
|
+
},
|
|
301
|
+
body: list(this._body)
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
return new XHTML(this._filename, this._meta, content);
|
|
305
|
+
function build(node) {
|
|
306
|
+
const attrs = Object.fromEntries(
|
|
307
|
+
Object.entries(node.attrs ?? {}).map(([key, value]) => ["@_" + key, value])
|
|
308
|
+
);
|
|
309
|
+
const obj = {
|
|
310
|
+
...attrs
|
|
311
|
+
};
|
|
312
|
+
if (Array.isArray(node.children)) {
|
|
313
|
+
const text = node.children.filter((c) => typeof c === "string");
|
|
314
|
+
const nodes = node.children.filter((c) => typeof c !== "string");
|
|
315
|
+
if (text.length > 0) {
|
|
316
|
+
obj["#text"] = text[0];
|
|
317
|
+
}
|
|
318
|
+
Object.assign(obj, list(nodes));
|
|
319
|
+
}
|
|
320
|
+
return obj;
|
|
321
|
+
}
|
|
322
|
+
function list(list2) {
|
|
323
|
+
const obj = {};
|
|
324
|
+
const nodes = list2.flatMap((n) => n.tag === Fragment ? n.children ?? [] : [n]);
|
|
325
|
+
for (const c of nodes) {
|
|
326
|
+
if (typeof c === "string") {
|
|
327
|
+
if (obj["#text"]) {
|
|
328
|
+
obj["#text"] += c;
|
|
329
|
+
} else {
|
|
330
|
+
obj["#text"] = c;
|
|
331
|
+
}
|
|
332
|
+
} else if (c.tag in obj) {
|
|
333
|
+
obj[c.tag].push(build(c));
|
|
334
|
+
} else {
|
|
335
|
+
obj[c.tag] = [build(c)];
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return obj;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
class Toc extends XHTML {
|
|
344
|
+
constructor(file, meta, content) {
|
|
345
|
+
super(file, meta, content);
|
|
346
|
+
this.update({ properties: "nav" });
|
|
347
|
+
}
|
|
348
|
+
static from(xhtml) {
|
|
349
|
+
return new Toc(xhtml.filename(), xhtml.meta(), xhtml.content());
|
|
350
|
+
}
|
|
351
|
+
static generate(file, nav, { title = "Nav", heading = 1, titleAttrs = {}, builder } = {}) {
|
|
352
|
+
if (!builder) {
|
|
353
|
+
builder = new XHTMLBuilder(file);
|
|
354
|
+
}
|
|
355
|
+
const root = {
|
|
356
|
+
tag: "nav",
|
|
357
|
+
attrs: {
|
|
358
|
+
"epub:type": "toc"
|
|
359
|
+
},
|
|
360
|
+
children: []
|
|
361
|
+
};
|
|
362
|
+
root.children.push(h("h" + heading, titleAttrs, title));
|
|
363
|
+
root.children.push(/* @__PURE__ */ h("ol", null, list(nav)));
|
|
364
|
+
return builder.body(root);
|
|
365
|
+
function list(items) {
|
|
366
|
+
return items.map(
|
|
367
|
+
(i) => "page" in i ? /* @__PURE__ */ h("li", { ...i.attrs }, /* @__PURE__ */ h("a", { href: i.page.filename() }, i.title)) : "list" in i ? /* @__PURE__ */ h("li", { ...i.attrs }, /* @__PURE__ */ h("span", null, i.title), /* @__PURE__ */ h("ol", null, list(i.list))) : false
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const defu = defu$1.createDefu((obj, key, value) => {
|
|
374
|
+
if (obj[key] instanceof Date && value instanceof Date) {
|
|
375
|
+
obj[key] = value;
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
class PackageDocument {
|
|
380
|
+
constructor(file) {
|
|
381
|
+
this.SpecVersion = "3.0";
|
|
382
|
+
this._uniqueIdentifier = "uuid";
|
|
383
|
+
this._identifier = node_crypto.randomUUID();
|
|
384
|
+
this._metadata = {
|
|
385
|
+
title: "",
|
|
386
|
+
language: "zh-CN",
|
|
387
|
+
contributor: [],
|
|
388
|
+
coverage: "",
|
|
389
|
+
creator: {
|
|
390
|
+
name: "unknown",
|
|
391
|
+
uid: "creator"
|
|
392
|
+
},
|
|
393
|
+
date: /* @__PURE__ */ new Date(),
|
|
394
|
+
description: "",
|
|
395
|
+
format: "",
|
|
396
|
+
publisher: "",
|
|
397
|
+
relation: "",
|
|
398
|
+
rights: "",
|
|
399
|
+
source: "",
|
|
400
|
+
subject: "",
|
|
401
|
+
type: "",
|
|
402
|
+
lastModified: /* @__PURE__ */ new Date()
|
|
403
|
+
};
|
|
404
|
+
this._items = [];
|
|
405
|
+
this._spine = [];
|
|
406
|
+
this.file = file;
|
|
407
|
+
}
|
|
408
|
+
filename() {
|
|
409
|
+
return this.file;
|
|
410
|
+
}
|
|
411
|
+
version() {
|
|
412
|
+
return this.SpecVersion;
|
|
413
|
+
}
|
|
414
|
+
// --- metadata ---
|
|
415
|
+
update(info) {
|
|
416
|
+
this._metadata = defu(info, this._metadata);
|
|
417
|
+
return this;
|
|
418
|
+
}
|
|
419
|
+
title() {
|
|
420
|
+
return this._metadata.title;
|
|
421
|
+
}
|
|
422
|
+
language() {
|
|
423
|
+
return this._metadata.language;
|
|
424
|
+
}
|
|
425
|
+
creator() {
|
|
426
|
+
return this._metadata.creator;
|
|
427
|
+
}
|
|
428
|
+
metadata() {
|
|
429
|
+
return this._metadata;
|
|
430
|
+
}
|
|
431
|
+
// --- manifest ---
|
|
432
|
+
addItem(item) {
|
|
433
|
+
this._items.push(item);
|
|
434
|
+
return this;
|
|
435
|
+
}
|
|
436
|
+
items() {
|
|
437
|
+
const l = [...this._items];
|
|
438
|
+
if (this._toc) {
|
|
439
|
+
l.push(this._toc);
|
|
440
|
+
}
|
|
441
|
+
return l;
|
|
442
|
+
}
|
|
443
|
+
manifest() {
|
|
444
|
+
return this.items().map((i) => i.manifest());
|
|
445
|
+
}
|
|
446
|
+
// --- navigation ---
|
|
447
|
+
spine() {
|
|
448
|
+
return this._spine;
|
|
449
|
+
}
|
|
450
|
+
setSpine(items) {
|
|
451
|
+
this._spine.splice(0, this._spine.length, ...items.map((i) => i.itemref()));
|
|
452
|
+
return this;
|
|
453
|
+
}
|
|
454
|
+
toc() {
|
|
455
|
+
return this._toc;
|
|
456
|
+
}
|
|
457
|
+
setToc(nav, option = {}) {
|
|
458
|
+
const toc = Toc.generate("nav.xhtml", nav, option);
|
|
459
|
+
if (!option.builder) {
|
|
460
|
+
toc.title(option.title ?? "Nav").language(this._metadata.language);
|
|
461
|
+
}
|
|
462
|
+
this._toc = Toc.from(toc.build());
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
465
|
+
// --- identifier ---
|
|
466
|
+
uniqueIdentifier() {
|
|
467
|
+
return this._uniqueIdentifier;
|
|
468
|
+
}
|
|
469
|
+
identifier() {
|
|
470
|
+
return this._identifier;
|
|
471
|
+
}
|
|
472
|
+
setIdentifier(identifier, uniqueIdentifier = "uuid") {
|
|
473
|
+
this._identifier = identifier;
|
|
474
|
+
this._uniqueIdentifier = uniqueIdentifier;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
class Epub {
|
|
479
|
+
constructor(meta = {}) {
|
|
480
|
+
/**
|
|
481
|
+
* See: https://www.w3.org/TR/epub-33/#sec-package-doc
|
|
482
|
+
*
|
|
483
|
+
* Now, it only supports single opf (OEBPS/content.opf)
|
|
484
|
+
*
|
|
485
|
+
* @returns list of package documents
|
|
486
|
+
*/
|
|
487
|
+
this.opfs = [new PackageDocument("OEBPS/content.opf")];
|
|
488
|
+
this.opfs[0].update(meta);
|
|
489
|
+
}
|
|
490
|
+
packages() {
|
|
491
|
+
return this.opfs;
|
|
492
|
+
}
|
|
493
|
+
main() {
|
|
494
|
+
return this.opfs[0];
|
|
495
|
+
}
|
|
496
|
+
item(...items) {
|
|
497
|
+
for (const item of items) {
|
|
498
|
+
this.opfs[0].addItem(item);
|
|
499
|
+
}
|
|
500
|
+
return this;
|
|
501
|
+
}
|
|
502
|
+
toc(nav, option = {}) {
|
|
503
|
+
this.opfs[0].setToc(nav, option);
|
|
504
|
+
return this;
|
|
505
|
+
}
|
|
506
|
+
spine(...items) {
|
|
507
|
+
this.opfs[0].setSpine(items);
|
|
508
|
+
return this;
|
|
509
|
+
}
|
|
510
|
+
async bundle() {
|
|
511
|
+
const { bundle } = await import('./chunks/index.cjs');
|
|
512
|
+
return await bundle(this);
|
|
513
|
+
}
|
|
514
|
+
async writeFile(file) {
|
|
515
|
+
const buffer = await this.bundle();
|
|
516
|
+
const dir = path__namespace$1.dirname(file);
|
|
517
|
+
if (!node_fs.existsSync(dir)) {
|
|
518
|
+
node_fs.mkdirSync(dir, { recursive: true });
|
|
519
|
+
}
|
|
520
|
+
await node_fs.promises.writeFile(file, buffer);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
class BundleError extends Error {
|
|
525
|
+
constructor(msg) {
|
|
526
|
+
super(msg);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const UnbuildPreset = ({
|
|
531
|
+
inject = true
|
|
532
|
+
} = {}) => ({
|
|
533
|
+
rollup: {
|
|
534
|
+
esbuild: {
|
|
535
|
+
jsxFactory: inject ? "__epubook_core.h" : "h",
|
|
536
|
+
jsxFragment: inject ? "__epubook_core.Fragment" : "Fragment",
|
|
537
|
+
loaders: {
|
|
538
|
+
".js": "js",
|
|
539
|
+
".ts": "ts",
|
|
540
|
+
".jsx": "jsx",
|
|
541
|
+
".tsx": "tsx"
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
},
|
|
545
|
+
hooks: {
|
|
546
|
+
"rollup:options"(_options, config) {
|
|
547
|
+
const plugins = config.plugins;
|
|
548
|
+
if (inject && Array.isArray(plugins)) {
|
|
549
|
+
plugins.push(Rollup());
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
function Rollup() {
|
|
555
|
+
return {
|
|
556
|
+
name: "epubook:inject-tsx",
|
|
557
|
+
transform(code, id) {
|
|
558
|
+
if (id.endsWith(".tsx")) {
|
|
559
|
+
return `import * as __epubook_core from '@epubook/core';
|
|
560
|
+
` + code;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
exports.BundleError = BundleError;
|
|
567
|
+
exports.Epub = Epub;
|
|
568
|
+
exports.Fragment = Fragment;
|
|
569
|
+
exports.HTML = HTML;
|
|
570
|
+
exports.Image = Image;
|
|
571
|
+
exports.ImageGif = ImageGif;
|
|
572
|
+
exports.ImageJpeg = ImageJpeg;
|
|
573
|
+
exports.ImagePng = ImagePng;
|
|
574
|
+
exports.ImageSvg = ImageSvg;
|
|
575
|
+
exports.ImageWebp = ImageWebp;
|
|
576
|
+
exports.Item = Item;
|
|
577
|
+
exports.MIMETYPE = MIMETYPE;
|
|
578
|
+
exports.ManifestItem = ManifestItem;
|
|
579
|
+
exports.ManifestItemRef = ManifestItemRef;
|
|
580
|
+
exports.PackageDocument = PackageDocument;
|
|
581
|
+
exports.Rollup = Rollup;
|
|
582
|
+
exports.Style = Style;
|
|
583
|
+
exports.TextCSS = TextCSS;
|
|
584
|
+
exports.TextXHTML = TextXHTML;
|
|
585
|
+
exports.Toc = Toc;
|
|
586
|
+
exports.UnbuildPreset = UnbuildPreset;
|
|
587
|
+
exports.XHTML = XHTML;
|
|
588
|
+
exports.XHTMLBuilder = XHTMLBuilder;
|
|
589
|
+
exports.getImageMediaType = getImageMediaType;
|
|
590
|
+
exports.h = h;
|