wp-epub-gen 0.5.0 → 0.6.1

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
@@ -15,122 +15,124 @@ npm install wp-epub-gen --save
15
15
  ### JavaScript (CommonJS)
16
16
 
17
17
  ```javascript
18
- const { epubGen } = require("wp-epub-gen");
18
+ const { epubGen } = require('wp-epub-gen')
19
19
 
20
20
  epubGen({
21
- title: "我的电子书",
22
- author: "作者名",
23
- output: "./my-book.epub",
21
+ title: '我的电子书',
22
+ author: '作者名',
23
+ output: './my-book.epub',
24
24
  content: [
25
25
  {
26
- title: "第一章",
27
- data: "<h1>第一章</h1><p>这是第一章的内容...</p>"
28
- }
29
- ]
26
+ title: '第一章',
27
+ data: '<h1>第一章</h1><p>这是第一章的内容...</p>',
28
+ },
29
+ ],
30
30
  }).then(
31
- () => console.log("电子书生成成功!"),
32
- err => console.error("生成失败:", err)
33
- );
31
+ () => console.log('电子书生成成功!'),
32
+ (err) => console.error('生成失败:', err),
33
+ )
34
34
  ```
35
35
 
36
36
  ### TypeScript / ES 模块
37
37
 
38
38
  ```typescript
39
- import { epubGen, type IEpubGenOptions } from 'wp-epub-gen';
39
+ import { epubGen, type IEpubGenOptions } from 'wp-epub-gen'
40
40
 
41
41
  const options: IEpubGenOptions = {
42
- title: "我的电子书",
43
- author: "作者名",
44
- output: "./my-book.epub",
42
+ title: '我的电子书',
43
+ author: '作者名',
44
+ output: './my-book.epub',
45
45
  content: [
46
46
  {
47
- title: "第一章",
48
- data: "<h1>第一章</h1><p>这是第一章的内容...</p>"
49
- }
50
- ]
51
- };
47
+ title: '第一章',
48
+ data: '<h1>第一章</h1><p>这是第一章的内容...</p>',
49
+ },
50
+ ],
51
+ }
52
52
 
53
53
  try {
54
- const result = await epubGen(options);
54
+ const result = await epubGen(options)
55
55
  if (result.success) {
56
- console.log("电子书生成成功!");
56
+ console.log('电子书生成成功!')
57
57
  } else {
58
- console.error("生成失败:", result.message);
58
+ console.error('生成失败:', result.message)
59
59
  }
60
60
  } catch (error) {
61
- console.error("发生错误:", error);
61
+ console.error('发生错误:', error)
62
62
  }
63
63
  ```
64
64
 
65
65
  ### 完整示例
66
66
 
67
67
  ```typescript
68
- import { epubGen } from 'wp-epub-gen';
68
+ import { epubGen } from 'wp-epub-gen'
69
69
 
70
70
  const options = {
71
- title: "完整示例电子书",
72
- author: ["张三", "李四"],
73
- publisher: "我的出版社",
74
- cover: "https://example.com/cover.jpg",
75
- output: "./complete-book.epub",
71
+ title: '完整示例电子书',
72
+ author: ['张三', '李四'],
73
+ publisher: '我的出版社',
74
+ cover: 'https://example.com/cover.jpg',
75
+ output: './complete-book.epub',
76
76
  version: 3,
77
- lang: "zh-cn",
77
+ lang: 'zh-cn',
78
78
  css: "body { font-family: 'Microsoft YaHei', sans-serif; }",
79
- tocTitle: "目录",
79
+ tocTitle: '目录',
80
80
  appendChapterTitles: true,
81
81
  tocAutoNumber: true,
82
82
  verbose: true,
83
83
  timeoutSeconds: 60,
84
84
  content: [
85
85
  {
86
- title: "前言",
87
- data: "<h1>前言</h1><p>这是前言内容...</p>",
88
- beforeToc: true
86
+ title: '前言',
87
+ data: '<h1>前言</h1><p>这是前言内容...</p>',
88
+ beforeToc: true,
89
89
  },
90
90
  {
91
- title: "第一部分",
92
- data: "<h1>第一部分</h1>",
91
+ title: '第一部分',
92
+ data: '<h1>第一部分</h1>',
93
93
  children: [
94
94
  {
95
- title: "第一章",
96
- data: "<h2>第一章</h2><p>第一章内容...</p>"
95
+ title: '第一章',
96
+ data: '<h2>第一章</h2><p>第一章内容...</p>',
97
97
  },
98
98
  {
99
- title: "第二章",
100
- data: "<h2>第二章</h2><p>第二章内容...</p>"
101
- }
102
- ]
99
+ title: '第二章',
100
+ data: '<h2>第二章</h2><p>第二章内容...</p>',
101
+ },
102
+ ],
103
103
  },
104
104
  {
105
- title: "第二部分",
106
- data: "<h1>第二部分</h1><p>第二部分内容...</p>"
105
+ title: '第二部分',
106
+ data: '<h1>第二部分</h1><p>第二部分内容...</p>',
107
107
  },
108
108
  {
109
- title: "附录",
110
- data: "<h1>附录</h1><p>附录内容...</p>",
111
- excludeFromToc: true
112
- }
113
- ]
114
- };
115
-
116
- epubGen(options).then(result => {
109
+ title: '附录',
110
+ data: '<h1>附录</h1><p>附录内容...</p>',
111
+ excludeFromToc: true,
112
+ },
113
+ ],
114
+ }
115
+
116
+ epubGen(options).then((result) => {
117
117
  if (result.success) {
118
- console.log("电子书生成成功!");
118
+ console.log('电子书生成成功!')
119
119
  }
120
- });
120
+ })
121
121
  ```
122
122
 
123
123
  ## API 参考
124
124
 
125
- ### epubGen(options, output?)
125
+ ### epubGen(options, configs?)
126
126
 
127
127
  主要的 EPUB 生成函数。
128
128
 
129
129
  **参数:**
130
- - `options: IEpubGenOptions` - 配置选项对象
131
- - `output?: string` - 可选的输出路径,会覆盖 options.output
130
+
131
+ - `options: IEpubGenOptions` - 配置选项对象(标题、作者、封面、章节内容等)
132
+ - `configs?: IGenConfigs` - 可选的运行时回调(logger、onProgress、concurrency),见下文 [IGenConfigs](#igenconfigs-运行时回调)
132
133
 
133
134
  **返回值:**
135
+
134
136
  - `Promise<IOut>` - 包含生成结果的 Promise
135
137
 
136
138
  ### IEpubGenOptions 配置选项
@@ -165,12 +167,13 @@ epubGen(options).then(result => {
165
167
 
166
168
  - **`fonts?: string[]`** - 自定义字体文件路径数组
167
169
  - 示例:`["/path/to/font.ttf"]`
168
-
170
+
169
171
  使用方法:
172
+
170
173
  ```css
171
174
  @font-face {
172
- font-family: "CustomFont";
173
- src: url("./fonts/font.ttf");
175
+ font-family: 'CustomFont';
176
+ src: url('./fonts/font.ttf');
174
177
  }
175
178
  ```
176
179
 
@@ -199,7 +202,7 @@ epubGen(options).then(result => {
199
202
  #### 高级自定义选项
200
203
 
201
204
  - **`customOpfTemplatePath?: string`** - 自定义 OPF 模板文件路径
202
- - **`customNcxTocTemplatePath?: string`** - 自定义 NCX 目录模板文件路径
205
+ - **`customNcxTocTemplatePath?: string`** - 自定义 NCX 目录模板文件路径
203
206
  - **`customHtmlTocTemplatePath?: string`** - 自定义 HTML 目录模板文件路径
204
207
 
205
208
  ### IChapter 章节对象
@@ -227,47 +230,112 @@ epubGen(options).then(result => {
227
230
 
228
231
  ```typescript
229
232
  interface IOut {
230
- success?: boolean; // 是否成功
231
- message?: string; // 错误信息(如果失败)
232
- options?: IEpubGenOptions; // 使用的配置选项
233
+ success?: boolean // 是否成功
234
+ message?: string // 错误信息(如果失败)
235
+ options?: IEpubGenOptions // 使用的配置选项
236
+ }
237
+ ```
238
+
239
+ ### IGenConfigs 运行时回调
240
+
241
+ `epubGen` 的第二个参数,用于注入宿主侧的回调和并发配置。所有字段都是可选的。
242
+
243
+ ```typescript
244
+ interface IGenConfigs {
245
+ logger?: ILogger
246
+ onProgress?: (e: IProgressEvent) => void
247
+ concurrency?: number
248
+ }
249
+ ```
250
+
251
+ #### `logger?: ILogger`
252
+
253
+ 注入自定义日志记录器(替代默认 `console`)。常用于 Electron 主进程把日志转发到渲染进程。
254
+
255
+ ```typescript
256
+ interface ILogger {
257
+ log: (msg: any) => void
258
+ info: (msg: any) => void
259
+ warn: (msg: any) => void
260
+ error: (msg: any) => void
261
+ }
262
+ ```
263
+
264
+ #### `onProgress?: (e: IProgressEvent) => void`
265
+
266
+ 进度回调。生成过程会在 5 个阶段中分别推送事件,宿主可据此渲染进度条或转发 IPC 给 UI。
267
+
268
+ ```typescript
269
+ type ProgressPhase =
270
+ | 'parseContent' // 解析章节 HTML
271
+ | 'writeChapters' // 写章节临时文件
272
+ | 'buildToc' // 渲染 OPF / NCX / TOC
273
+ | 'downloadImage' // 下载图片(仅当存在图片时)
274
+ | 'zip' // 打包 .epub
275
+
276
+ interface IProgressEvent {
277
+ phase: ProgressPhase
278
+ current: number // 已完成数量
279
+ total: number // 总数量
280
+ label?: string // 当前条目标签(章节标题 / 图片 URL)
233
281
  }
234
282
  ```
235
283
 
284
+ 最小用法:
285
+
286
+ ```typescript
287
+ await epubGen(options, {
288
+ onProgress: (e) => {
289
+ console.log(`[${e.phase}] ${e.current}/${e.total}`)
290
+ },
291
+ })
292
+ ```
293
+
294
+ 回调中抛出的异常会被库静默吞掉,不会中断 EPUB 生成。
295
+
296
+ #### `concurrency?: number`
297
+
298
+ 写章节文件和下载图片所共用的并发上限。默认 `16`,机械硬盘或带宽受限场景可调小(如 `4`)。非有限正整数(NaN、负数等)会被自动归一化为默认值。
299
+
236
300
  ## 导出的类型
237
301
 
238
302
  库导出了所有 TypeScript 类型定义:
239
303
 
240
304
  ```typescript
241
- import type {
242
- IEpubGenOptions,
243
- IChapter,
305
+ import type {
306
+ IEpubGenOptions,
307
+ IChapter,
244
308
  IChapterData,
245
309
  IEpubData,
246
310
  IEpubImage,
247
- IOut
248
- } from 'wp-epub-gen';
311
+ IGenConfigs,
312
+ ILogger,
313
+ IProgressEvent,
314
+ ProgressPhase,
315
+ IOut,
316
+ } from 'wp-epub-gen'
249
317
  ```
250
318
 
251
319
  ## 错误处理
252
320
 
253
321
  ```typescript
254
- import { epubGen, errors } from 'wp-epub-gen';
322
+ import { epubGen, errors } from 'wp-epub-gen'
255
323
 
256
- const result = await epubGen(options);
324
+ const result = await epubGen(options)
257
325
 
258
326
  if (!result.success) {
259
327
  switch (result.message) {
260
328
  case errors.no_title:
261
- console.error("缺少标题");
262
- break;
329
+ console.error('缺少标题')
330
+ break
263
331
  case errors.no_output_path:
264
- console.error("缺少输出路径");
265
- break;
332
+ console.error('缺少输出路径')
333
+ break
266
334
  case errors.no_content:
267
- console.error("缺少内容");
268
- break;
335
+ console.error('缺少内容')
336
+ break
269
337
  default:
270
- console.error("未知错误:", result.message);
338
+ console.error('未知错误:', result.message)
271
339
  }
272
340
  }
273
341
  ```