@epubook/core 0.0.4 → 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/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # @epubook/core
2
2
 
3
- The fundamental module of [epubook](https://github.com/yjl9903/epubook) for generating EPUB books.
3
+ [![version](https://img.shields.io/npm/v/@epubook/core?color=rgb%2850%2C203%2C86%29&label=@epubook/core)](https://www.npmjs.com/package/@epubook/theme-core) [![CI](https://github.com/yjl9903/epubook/actions/workflows/ci.yml/badge.svg)](https://github.com/yjl9903/epubook/actions/workflows/ci.yml)
4
4
 
5
- > 👷‍♂️ Still work in progress.
5
+ The fundamental module of [epubook](https://github.com/yjl9903/epubook). It provides low-level API for generating EPUB books.
6
6
 
7
7
  ## Installation
8
8
 
package/dist/index.cjs CHANGED
@@ -242,6 +242,7 @@ class XHTMLBuilder {
242
242
  };
243
243
  this._head = [];
244
244
  this._body = [];
245
+ this._bodyAttrs = {};
245
246
  this._filename = filename;
246
247
  this._meta.title = path__namespace$1.basename(filename);
247
248
  }
@@ -287,8 +288,17 @@ class XHTMLBuilder {
287
288
  this._body.push(...node);
288
289
  return this;
289
290
  }
291
+ bodyAttrs(attrs = {}) {
292
+ const a = Object.entries(attrs).map(([key, value]) => [`@_${key}`, value]);
293
+ this._bodyAttrs = {
294
+ ...this._bodyAttrs,
295
+ ...Object.fromEntries(a)
296
+ };
297
+ return this;
298
+ }
290
299
  build() {
291
- const content = builder.build({
300
+ const replacer = {};
301
+ let content = builder.build({
292
302
  html: {
293
303
  "@_xmlns": "http://www.w3.org/1999/xhtml",
294
304
  "@_xmlns:epub": "http://www.idpf.org/2007/ops",
@@ -298,26 +308,38 @@ class XHTMLBuilder {
298
308
  title: this._meta.title,
299
309
  ...list(this._head)
300
310
  },
301
- body: list(this._body)
311
+ body: {
312
+ ...this._bodyAttrs,
313
+ ...list(this._body)
314
+ }
302
315
  }
303
316
  });
317
+ for (const [key, value] of Object.entries(replacer)) {
318
+ content = content.replace(key, value);
319
+ }
304
320
  return new XHTML(this._filename, this._meta, content);
305
321
  function build(node) {
306
322
  const attrs = Object.fromEntries(
307
323
  Object.entries(node.attrs ?? {}).map(([key, value]) => ["@_" + key, value])
308
324
  );
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];
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));
317
340
  }
318
- Object.assign(obj, list(nodes));
341
+ return obj;
319
342
  }
320
- return obj;
321
343
  }
322
344
  function list(list2) {
323
345
  const obj = {};
package/dist/index.d.ts CHANGED
@@ -88,22 +88,27 @@ interface XHTMLNode {
88
88
  attrs?: Record<string, string>;
89
89
  children?: Array<string | XHTMLNode>;
90
90
  }
91
+ interface BaseElementAttrs {
92
+ class?: string;
93
+ id?: string;
94
+ html?: string;
95
+ }
91
96
  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: {
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 & {
107
112
  href: string;
108
113
  };
109
114
  }
@@ -134,12 +139,14 @@ declare class XHTMLBuilder {
134
139
  private _filename;
135
140
  private _head;
136
141
  private _body;
142
+ private _bodyAttrs;
137
143
  constructor(filename: string);
138
144
  language(value: string): this;
139
145
  title(value: string): this;
140
146
  style(...list: Array<string | Style>): this;
141
147
  head(...node: XHTMLNode[]): this;
142
148
  body(...node: XHTMLNode[]): this;
149
+ bodyAttrs(attrs?: Record<string, string>): this;
143
150
  build(): XHTML;
144
151
  }
145
152
 
package/dist/index.mjs CHANGED
@@ -226,6 +226,7 @@ class XHTMLBuilder {
226
226
  };
227
227
  this._head = [];
228
228
  this._body = [];
229
+ this._bodyAttrs = {};
229
230
  this._filename = filename;
230
231
  this._meta.title = path$1.basename(filename);
231
232
  }
@@ -271,8 +272,17 @@ class XHTMLBuilder {
271
272
  this._body.push(...node);
272
273
  return this;
273
274
  }
275
+ bodyAttrs(attrs = {}) {
276
+ const a = Object.entries(attrs).map(([key, value]) => [`@_${key}`, value]);
277
+ this._bodyAttrs = {
278
+ ...this._bodyAttrs,
279
+ ...Object.fromEntries(a)
280
+ };
281
+ return this;
282
+ }
274
283
  build() {
275
- const content = builder.build({
284
+ const replacer = {};
285
+ let content = builder.build({
276
286
  html: {
277
287
  "@_xmlns": "http://www.w3.org/1999/xhtml",
278
288
  "@_xmlns:epub": "http://www.idpf.org/2007/ops",
@@ -282,26 +292,38 @@ class XHTMLBuilder {
282
292
  title: this._meta.title,
283
293
  ...list(this._head)
284
294
  },
285
- body: list(this._body)
295
+ body: {
296
+ ...this._bodyAttrs,
297
+ ...list(this._body)
298
+ }
286
299
  }
287
300
  });
301
+ for (const [key, value] of Object.entries(replacer)) {
302
+ content = content.replace(key, value);
303
+ }
288
304
  return new XHTML(this._filename, this._meta, content);
289
305
  function build(node) {
290
306
  const attrs = Object.fromEntries(
291
307
  Object.entries(node.attrs ?? {}).map(([key, value]) => ["@_" + key, value])
292
308
  );
293
- const obj = {
294
- ...attrs
295
- };
296
- if (Array.isArray(node.children)) {
297
- const text = node.children.filter((c) => typeof c === "string");
298
- const nodes = node.children.filter((c) => typeof c !== "string");
299
- if (text.length > 0) {
300
- 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));
301
324
  }
302
- Object.assign(obj, list(nodes));
325
+ return obj;
303
326
  }
304
- return obj;
305
327
  }
306
328
  function list(list2) {
307
329
  const obj = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@epubook/core",
3
- "version": "0.0.4",
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",