@epubook/core 0.0.11 → 0.0.12-beta.2

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