@epubook/core 0.0.5 → 0.0.6

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 CHANGED
@@ -297,7 +297,8 @@ class XHTMLBuilder {
297
297
  return this;
298
298
  }
299
299
  build() {
300
- const content = builder.build({
300
+ const replacer = {};
301
+ let content = builder.build({
301
302
  html: {
302
303
  "@_xmlns": "http://www.w3.org/1999/xhtml",
303
304
  "@_xmlns:epub": "http://www.idpf.org/2007/ops",
@@ -313,23 +314,32 @@ class XHTMLBuilder {
313
314
  }
314
315
  }
315
316
  });
317
+ for (const [key, value] of Object.entries(replacer)) {
318
+ content = content.replace(key, value);
319
+ }
316
320
  return new XHTML(this._filename, this._meta, content);
317
321
  function build(node) {
318
322
  const attrs = Object.fromEntries(
319
323
  Object.entries(node.attrs ?? {}).map(([key, value]) => ["@_" + key, value])
320
324
  );
321
- const obj = {
322
- ...attrs
323
- };
324
- if (Array.isArray(node.children)) {
325
- const text = node.children.filter((c) => typeof c === "string");
326
- const nodes = node.children.filter((c) => typeof c !== "string");
327
- if (text.length > 0) {
328
- obj["#text"] = text[0];
325
+ if (node.attrs && "html" in node.attrs) {
326
+ const id = `____${node_crypto.randomUUID()}____`;
327
+ replacer[id] = node.attrs.html;
328
+ return { ...attrs, "@_html": void 0, "#text": id };
329
+ } else {
330
+ const obj = {
331
+ ...attrs
332
+ };
333
+ if (Array.isArray(node.children)) {
334
+ const text = node.children.filter((c) => typeof c === "string");
335
+ const nodes = node.children.filter((c) => typeof c !== "string");
336
+ if (text.length > 0) {
337
+ obj["#text"] = text[0];
338
+ }
339
+ Object.assign(obj, list(nodes));
329
340
  }
330
- Object.assign(obj, list(nodes));
341
+ return obj;
331
342
  }
332
- return obj;
333
343
  }
334
344
  function list(list2) {
335
345
  const obj = {};
package/dist/index.d.ts CHANGED
@@ -1,268 +1,273 @@
1
1
  import { Plugin } from 'rollup';
2
2
  import { BuildConfig } from 'unbuild';
3
3
 
4
- declare const MIMETYPE = "application/epub+zip";
5
- declare const ImageGif = "image/gif";
6
- declare const ImageJpeg = "image/jpeg";
7
- declare const ImagePng = "image/png";
8
- declare const ImageSvg = "image/svg+xml";
9
- declare const ImageWebp = "image/webp";
10
- type ImageMediaType = typeof ImageGif | typeof ImageJpeg | typeof ImagePng | typeof ImageSvg | typeof ImageWebp;
11
- type ImageExtension = 'gif' | 'jpg' | 'jpeg' | 'png' | 'svg' | 'webp';
12
- declare const TextCSS = "text/css";
13
- declare const TextXHTML = "application/xhtml+xml";
14
- type MediaType = ImageMediaType | typeof TextCSS | typeof TextXHTML;
4
+ declare const MIMETYPE = "application/epub+zip";
5
+ declare const ImageGif = "image/gif";
6
+ declare const ImageJpeg = "image/jpeg";
7
+ declare const ImagePng = "image/png";
8
+ declare const ImageSvg = "image/svg+xml";
9
+ declare const ImageWebp = "image/webp";
10
+ type ImageMediaType = typeof ImageGif | typeof ImageJpeg | typeof ImagePng | typeof ImageSvg | typeof ImageWebp;
11
+ type ImageExtension = 'gif' | 'jpg' | 'jpeg' | 'png' | 'svg' | 'webp';
12
+ declare const TextCSS = "text/css";
13
+ declare const TextXHTML = "application/xhtml+xml";
14
+ type MediaType = ImageMediaType | typeof TextCSS | typeof TextXHTML;
15
15
  declare function getImageMediaType(file: string): ImageMediaType | undefined;
16
16
 
17
- declare class ManifestItem {
18
- private _href;
19
- private _id;
20
- private optional;
21
- constructor(href: string, id: string);
22
- update(info: typeof this$1.optional): this;
23
- href(): string;
24
- id(): string;
25
- fallback(): string | undefined;
26
- mediaOverlay(): string | undefined;
27
- mediaType(): MediaType | undefined;
28
- properties(): string | undefined;
29
- ref(): ManifestItemRef;
30
- }
31
- declare class ManifestItemRef {
32
- private _idref;
33
- private optional;
34
- constructor(idref: string);
35
- update(info: typeof this$1.optional): this;
36
- idref(): string;
37
- id(): string | undefined;
38
- linear(): string | undefined;
39
- properties(): string | undefined;
17
+ declare class ManifestItem {
18
+ private _href;
19
+ private _id;
20
+ private optional;
21
+ constructor(href: string, id: string);
22
+ update(info: typeof this$1.optional): this;
23
+ href(): string;
24
+ id(): string;
25
+ fallback(): string | undefined;
26
+ mediaOverlay(): string | undefined;
27
+ mediaType(): MediaType | undefined;
28
+ properties(): string | undefined;
29
+ ref(): ManifestItemRef;
30
+ }
31
+ declare class ManifestItemRef {
32
+ private _idref;
33
+ private optional;
34
+ constructor(idref: string);
35
+ update(info: typeof this$1.optional): this;
36
+ idref(): string;
37
+ id(): string | undefined;
38
+ linear(): string | undefined;
39
+ properties(): string | undefined;
40
40
  }
41
41
 
42
- declare abstract class Item {
43
- private readonly file;
44
- private readonly mediaType;
45
- private _properties?;
46
- constructor(file: string, mediaType: MediaType);
47
- filename(): string;
48
- relative(from: string): string;
49
- update(info: Partial<{
50
- properties: string;
51
- }>): this;
52
- id(): string;
53
- manifest(): ManifestItem;
54
- itemref(): ManifestItemRef;
55
- abstract bundle(): Promise<Uint8Array>;
56
- }
57
- declare class Style extends Item {
58
- private content;
59
- constructor(file: string, content: string);
60
- static read(src: string, dst: string): Promise<Style | undefined>;
61
- bundle(): Promise<Uint8Array>;
62
- }
63
- declare class Image extends Item {
64
- private data;
65
- constructor(file: string, type: ImageMediaType, data: Uint8Array);
66
- static read(file: string, src: string): Promise<Image | undefined>;
67
- bundle(): Promise<Uint8Array>;
68
- }
69
- declare class HTML extends Item {
70
- private content;
71
- constructor(file: string, content: string);
72
- static read(src: string, dst: string): Promise<HTML | undefined>;
73
- bundle(): Promise<Uint8Array>;
42
+ declare abstract class Item {
43
+ private readonly file;
44
+ private readonly mediaType;
45
+ private _properties?;
46
+ constructor(file: string, mediaType: MediaType);
47
+ filename(): string;
48
+ relative(from: string): string;
49
+ update(info: Partial<{
50
+ properties: string;
51
+ }>): this;
52
+ id(): string;
53
+ manifest(): ManifestItem;
54
+ itemref(): ManifestItemRef;
55
+ abstract bundle(): Promise<Uint8Array>;
56
+ }
57
+ declare class Style extends Item {
58
+ private content;
59
+ constructor(file: string, content: string);
60
+ static read(src: string, dst: string): Promise<Style | undefined>;
61
+ bundle(): Promise<Uint8Array>;
62
+ }
63
+ declare class Image extends Item {
64
+ private data;
65
+ constructor(file: string, type: ImageMediaType, data: Uint8Array);
66
+ static read(file: string, src: string): Promise<Image | undefined>;
67
+ bundle(): Promise<Uint8Array>;
68
+ }
69
+ declare class HTML extends Item {
70
+ private content;
71
+ constructor(file: string, content: string);
72
+ static read(src: string, dst: string): Promise<HTML | undefined>;
73
+ bundle(): Promise<Uint8Array>;
74
74
  }
75
75
 
76
- type Prettify<T> = {
77
- [K in keyof T]: T[K];
76
+ type Prettify<T> = {
77
+ [K in keyof T]: T[K];
78
78
  } & {};
79
79
 
80
- declare global {
81
- namespace JSX {
82
- interface IntrinsicElements extends EpubIntrinsicElements {
83
- }
84
- }
85
- }
86
- interface XHTMLNode {
87
- tag: string;
88
- attrs?: Record<string, string>;
89
- children?: Array<string | XHTMLNode>;
90
- }
91
- interface EpubIntrinsicElements {
92
- nav: {};
93
- ul: {};
94
- ol: {};
95
- li: {};
96
- h1: {};
97
- h2: {};
98
- h3: {};
99
- h4: {};
100
- h5: {};
101
- h6: {};
102
- p: {};
103
- pre: {};
104
- span: {};
105
- code: {};
106
- a: {
107
- href: string;
108
- };
80
+ declare global {
81
+ namespace JSX {
82
+ interface IntrinsicElements extends EpubIntrinsicElements {
83
+ }
84
+ }
85
+ }
86
+ interface XHTMLNode {
87
+ tag: string;
88
+ attrs?: Record<string, string>;
89
+ children?: Array<string | XHTMLNode>;
90
+ }
91
+ interface BaseElementAttrs {
92
+ class?: string;
93
+ id?: string;
94
+ html?: string;
95
+ }
96
+ interface EpubIntrinsicElements {
97
+ nav: BaseElementAttrs & {};
98
+ ul: BaseElementAttrs & {};
99
+ ol: BaseElementAttrs & {};
100
+ li: BaseElementAttrs & {};
101
+ h1: BaseElementAttrs & {};
102
+ h2: BaseElementAttrs & {};
103
+ h3: BaseElementAttrs & {};
104
+ h4: BaseElementAttrs & {};
105
+ h5: BaseElementAttrs & {};
106
+ h6: BaseElementAttrs & {};
107
+ p: BaseElementAttrs & {};
108
+ pre: BaseElementAttrs & {};
109
+ span: BaseElementAttrs & {};
110
+ code: BaseElementAttrs & {};
111
+ a: BaseElementAttrs & {
112
+ href: string;
113
+ };
109
114
  }
110
115
 
111
- declare const Fragment = "Fragment";
112
- declare function h(tag: string, attrs?: Record<string, string>, ...children: Array<string | XHTMLNode | Array<string | XHTMLNode>>): {
113
- tag: string;
114
- attrs: Record<string, string>;
115
- children: (string | XHTMLNode)[];
116
+ declare const Fragment = "Fragment";
117
+ declare function h(tag: string, attrs?: Record<string, string>, ...children: Array<string | XHTMLNode | Array<string | XHTMLNode>>): {
118
+ tag: string;
119
+ attrs: Record<string, string>;
120
+ children: (string | XHTMLNode)[];
116
121
  };
117
122
 
118
- interface HTMLMeta {
119
- language: string;
120
- title: string;
121
- }
122
- declare class XHTML extends Item {
123
- private _meta;
124
- private _content;
125
- constructor(file: string, meta: HTMLMeta, content: string);
126
- meta(): HTMLMeta;
127
- title(): string;
128
- language(): string;
129
- content(): string;
130
- bundle(): Promise<Uint8Array>;
131
- }
132
- declare class XHTMLBuilder {
133
- private _meta;
134
- private _filename;
135
- private _head;
136
- private _body;
137
- private _bodyAttrs;
138
- constructor(filename: string);
139
- language(value: string): this;
140
- title(value: string): this;
141
- style(...list: Array<string | Style>): this;
142
- head(...node: XHTMLNode[]): this;
143
- body(...node: XHTMLNode[]): this;
144
- bodyAttrs(attrs?: Record<string, string>): this;
145
- build(): XHTML;
123
+ interface HTMLMeta {
124
+ language: string;
125
+ title: string;
126
+ }
127
+ declare class XHTML extends Item {
128
+ private _meta;
129
+ private _content;
130
+ constructor(file: string, meta: HTMLMeta, content: string);
131
+ meta(): HTMLMeta;
132
+ title(): string;
133
+ language(): string;
134
+ content(): string;
135
+ bundle(): Promise<Uint8Array>;
136
+ }
137
+ declare class XHTMLBuilder {
138
+ private _meta;
139
+ private _filename;
140
+ private _head;
141
+ private _body;
142
+ private _bodyAttrs;
143
+ constructor(filename: string);
144
+ language(value: string): this;
145
+ title(value: string): this;
146
+ style(...list: Array<string | Style>): this;
147
+ head(...node: XHTMLNode[]): this;
148
+ body(...node: XHTMLNode[]): this;
149
+ bodyAttrs(attrs?: Record<string, string>): this;
150
+ build(): XHTML;
146
151
  }
147
152
 
148
- type NavItem = {
149
- title: string;
150
- attrs?: Record<string, string>;
151
- };
152
- type NavLink = Prettify<NavItem & {
153
- page: HTML | XHTML;
154
- }>;
155
- type NavSubList = Prettify<NavItem & {
156
- list: NavLink[];
157
- }>;
158
- type NavList = Array<NavLink | NavSubList>;
159
- interface NavOption {
160
- title: string;
161
- heading: 1 | 2 | 3 | 4 | 5 | 6;
162
- titleAttrs: Record<string, string>;
163
- builder?: XHTMLBuilder;
164
- }
165
- declare class Toc extends XHTML {
166
- constructor(file: string, meta: HTMLMeta, content: string);
167
- static from(xhtml: XHTML): Toc;
168
- static generate(file: string, nav: NavList, { title, heading, titleAttrs, builder }?: Partial<NavOption>): XHTMLBuilder;
153
+ type NavItem = {
154
+ title: string;
155
+ attrs?: Record<string, string>;
156
+ };
157
+ type NavLink = Prettify<NavItem & {
158
+ page: HTML | XHTML;
159
+ }>;
160
+ type NavSubList = Prettify<NavItem & {
161
+ list: NavLink[];
162
+ }>;
163
+ type NavList = Array<NavLink | NavSubList>;
164
+ interface NavOption {
165
+ title: string;
166
+ heading: 1 | 2 | 3 | 4 | 5 | 6;
167
+ titleAttrs: Record<string, string>;
168
+ builder?: XHTMLBuilder;
169
+ }
170
+ declare class Toc extends XHTML {
171
+ constructor(file: string, meta: HTMLMeta, content: string);
172
+ static from(xhtml: XHTML): Toc;
173
+ static generate(file: string, nav: NavList, { title, heading, titleAttrs, builder }?: Partial<NavOption>): XHTMLBuilder;
169
174
  }
170
175
 
171
- interface Author {
172
- name: string;
173
- fileAs?: string;
174
- role?: string;
175
- uid?: string;
176
- }
177
- interface PackageDocumentMeta {
178
- title: string;
179
- language: string;
180
- contributor: Author[];
181
- coverage: string;
182
- creator: Author;
183
- date: Date;
184
- description: string;
185
- format: string;
186
- publisher: string;
187
- relation: string;
188
- rights: string;
189
- source: string;
190
- subject: string;
191
- type: string;
192
- lastModified: Date;
193
- }
194
- declare class PackageDocument {
195
- private readonly file;
196
- private readonly SpecVersion;
197
- private _uniqueIdentifier;
198
- private _identifier;
199
- private _metadata;
200
- private _toc;
201
- private _items;
202
- private _spine;
203
- constructor(file: string);
204
- filename(): string;
205
- version(): "3.0";
206
- update(info: Partial<PackageDocumentMeta>): this;
207
- title(): string;
208
- language(): string;
209
- creator(): Author;
210
- metadata(): PackageDocumentMeta;
211
- addItem(item: Item): this;
212
- items(): Item[];
213
- manifest(): ManifestItem[];
214
- spine(): ManifestItemRef[];
215
- setSpine(items: Item[]): this;
216
- toc(): Toc | undefined;
217
- setToc(nav: NavList, option?: Partial<NavOption>): this;
218
- uniqueIdentifier(): string;
219
- identifier(): string;
220
- setIdentifier(identifier: string, uniqueIdentifier?: string): void;
176
+ interface Author {
177
+ name: string;
178
+ fileAs?: string;
179
+ role?: string;
180
+ uid?: string;
181
+ }
182
+ interface PackageDocumentMeta {
183
+ title: string;
184
+ language: string;
185
+ contributor: Author[];
186
+ coverage: string;
187
+ creator: Author;
188
+ date: Date;
189
+ description: string;
190
+ format: string;
191
+ publisher: string;
192
+ relation: string;
193
+ rights: string;
194
+ source: string;
195
+ subject: string;
196
+ type: string;
197
+ lastModified: Date;
198
+ }
199
+ declare class PackageDocument {
200
+ private readonly file;
201
+ private readonly SpecVersion;
202
+ private _uniqueIdentifier;
203
+ private _identifier;
204
+ private _metadata;
205
+ private _toc;
206
+ private _items;
207
+ private _spine;
208
+ constructor(file: string);
209
+ filename(): string;
210
+ version(): "3.0";
211
+ update(info: Partial<PackageDocumentMeta>): this;
212
+ title(): string;
213
+ language(): string;
214
+ creator(): Author;
215
+ metadata(): PackageDocumentMeta;
216
+ addItem(item: Item): this;
217
+ items(): Item[];
218
+ manifest(): ManifestItem[];
219
+ spine(): ManifestItemRef[];
220
+ setSpine(items: Item[]): this;
221
+ toc(): Toc | undefined;
222
+ setToc(nav: NavList, option?: Partial<NavOption>): this;
223
+ uniqueIdentifier(): string;
224
+ identifier(): string;
225
+ setIdentifier(identifier: string, uniqueIdentifier?: string): void;
221
226
  }
222
227
 
223
- declare class Epub {
224
- /**
225
- * See: https://www.w3.org/TR/epub-33/#sec-package-doc
226
- *
227
- * Now, it only supports single opf (OEBPS/content.opf)
228
- *
229
- * @returns list of package documents
230
- */
231
- private opfs;
232
- constructor(meta?: Partial<PackageDocumentMeta>);
233
- packages(): PackageDocument[];
234
- main(): PackageDocument;
235
- item(...items: Item[]): this;
236
- toc(nav: NavList, option?: Partial<NavOption>): this;
237
- spine(...items: Item[]): this;
238
- bundle(): Promise<Uint8Array>;
239
- writeFile(file: string): Promise<void>;
228
+ declare class Epub {
229
+ /**
230
+ * See: https://www.w3.org/TR/epub-33/#sec-package-doc
231
+ *
232
+ * Now, it only supports single opf (OEBPS/content.opf)
233
+ *
234
+ * @returns list of package documents
235
+ */
236
+ private opfs;
237
+ constructor(meta?: Partial<PackageDocumentMeta>);
238
+ packages(): PackageDocument[];
239
+ main(): PackageDocument;
240
+ item(...items: Item[]): this;
241
+ toc(nav: NavList, option?: Partial<NavOption>): this;
242
+ spine(...items: Item[]): this;
243
+ bundle(): Promise<Uint8Array>;
244
+ writeFile(file: string): Promise<void>;
240
245
  }
241
246
 
242
- declare class BundleError extends Error {
243
- constructor(msg: string);
247
+ declare class BundleError extends Error {
248
+ constructor(msg: string);
244
249
  }
245
250
 
246
- type PageTemplate<T = any> = (file: string, props: T) => XHTMLBuilder;
247
- interface Theme<P extends Record<string, PageTemplate<any>>> {
248
- name: string;
249
- styles: string[];
250
- images: string[];
251
- pages: Prettify<{
252
- cover(file: string, props: {
253
- image: Image;
254
- title?: string;
255
- }): XHTMLBuilder;
256
- nav(file: string, props: {
257
- nav: NavList;
258
- option: Partial<NavOption>;
259
- }): XHTMLBuilder;
260
- } & P>;
251
+ type PageTemplate<T = any> = (file: string, props: T) => XHTMLBuilder;
252
+ interface Theme<P extends Record<string, PageTemplate<any>>> {
253
+ name: string;
254
+ styles: string[];
255
+ images: string[];
256
+ pages: Prettify<{
257
+ cover(file: string, props: {
258
+ image: Image;
259
+ title?: string;
260
+ }): XHTMLBuilder;
261
+ nav(file: string, props: {
262
+ nav: NavList;
263
+ option: Partial<NavOption>;
264
+ }): XHTMLBuilder;
265
+ } & P>;
261
266
  }
262
267
 
263
- declare const UnbuildPreset: (options?: {
264
- inject?: boolean;
265
- }) => BuildConfig;
268
+ declare const UnbuildPreset: (options?: {
269
+ inject?: boolean;
270
+ }) => BuildConfig;
266
271
  declare function Rollup(): Plugin;
267
272
 
268
273
  export { Author, BundleError, Epub, EpubIntrinsicElements, Fragment, HTML, HTMLMeta, Image, ImageExtension, ImageGif, ImageJpeg, ImageMediaType, ImagePng, ImageSvg, ImageWebp, Item, MIMETYPE, ManifestItem, ManifestItemRef, MediaType, NavList, NavOption, PackageDocument, PackageDocumentMeta, PageTemplate, Rollup, Style, TextCSS, TextXHTML, Theme, Toc, UnbuildPreset, XHTML, XHTMLBuilder, XHTMLNode, getImageMediaType, h };
package/dist/index.mjs CHANGED
@@ -281,7 +281,8 @@ class XHTMLBuilder {
281
281
  return this;
282
282
  }
283
283
  build() {
284
- const content = builder.build({
284
+ const replacer = {};
285
+ let content = builder.build({
285
286
  html: {
286
287
  "@_xmlns": "http://www.w3.org/1999/xhtml",
287
288
  "@_xmlns:epub": "http://www.idpf.org/2007/ops",
@@ -297,23 +298,32 @@ class XHTMLBuilder {
297
298
  }
298
299
  }
299
300
  });
301
+ for (const [key, value] of Object.entries(replacer)) {
302
+ content = content.replace(key, value);
303
+ }
300
304
  return new XHTML(this._filename, this._meta, content);
301
305
  function build(node) {
302
306
  const attrs = Object.fromEntries(
303
307
  Object.entries(node.attrs ?? {}).map(([key, value]) => ["@_" + key, value])
304
308
  );
305
- const obj = {
306
- ...attrs
307
- };
308
- if (Array.isArray(node.children)) {
309
- const text = node.children.filter((c) => typeof c === "string");
310
- const nodes = node.children.filter((c) => typeof c !== "string");
311
- if (text.length > 0) {
312
- obj["#text"] = text[0];
309
+ if (node.attrs && "html" in node.attrs) {
310
+ const id = `____${randomUUID()}____`;
311
+ replacer[id] = node.attrs.html;
312
+ return { ...attrs, "@_html": void 0, "#text": id };
313
+ } else {
314
+ const obj = {
315
+ ...attrs
316
+ };
317
+ if (Array.isArray(node.children)) {
318
+ const text = node.children.filter((c) => typeof c === "string");
319
+ const nodes = node.children.filter((c) => typeof c !== "string");
320
+ if (text.length > 0) {
321
+ obj["#text"] = text[0];
322
+ }
323
+ Object.assign(obj, list(nodes));
313
324
  }
314
- Object.assign(obj, list(nodes));
325
+ return obj;
315
326
  }
316
- return obj;
317
327
  }
318
328
  function list(list2) {
319
329
  const obj = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@epubook/core",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ebook",
@@ -43,7 +43,7 @@
43
43
  "vitest": "^0.29.2"
44
44
  },
45
45
  "engines": {
46
- "node": ">=v18.14.2"
46
+ "node": ">=v18.15.0"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "unbuild",