wp-epub-gen 0.4.2 → 0.6.0

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
@@ -122,13 +122,13 @@ epubGen(options).then(result => {
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
+ - `options: IEpubGenOptions` - 配置选项对象(标题、作者、封面、章节内容等)
131
+ - `configs?: IGenConfigs` - 可选的运行时回调(logger、onProgress、concurrency),见下文 [IGenConfigs](#igenconfigs-运行时回调)
132
132
 
133
133
  **返回值:**
134
134
  - `Promise<IOut>` - 包含生成结果的 Promise
@@ -233,18 +233,83 @@ interface IOut {
233
233
  }
234
234
  ```
235
235
 
236
+ ### IGenConfigs 运行时回调
237
+
238
+ `epubGen` 的第二个参数,用于注入宿主侧的回调和并发配置。所有字段都是可选的。
239
+
240
+ ```typescript
241
+ interface IGenConfigs {
242
+ logger?: ILogger;
243
+ onProgress?: (e: IProgressEvent) => void;
244
+ concurrency?: number;
245
+ }
246
+ ```
247
+
248
+ #### `logger?: ILogger`
249
+
250
+ 注入自定义日志记录器(替代默认 `console`)。常用于 Electron 主进程把日志转发到渲染进程。
251
+
252
+ ```typescript
253
+ interface ILogger {
254
+ log: (msg: any) => void;
255
+ info: (msg: any) => void;
256
+ warn: (msg: any) => void;
257
+ error: (msg: any) => void;
258
+ }
259
+ ```
260
+
261
+ #### `onProgress?: (e: IProgressEvent) => void`
262
+
263
+ 进度回调。生成过程会在 5 个阶段中分别推送事件,宿主可据此渲染进度条或转发 IPC 给 UI。
264
+
265
+ ```typescript
266
+ type ProgressPhase =
267
+ | 'parseContent' // 解析章节 HTML
268
+ | 'writeChapters' // 写章节临时文件
269
+ | 'buildToc' // 渲染 OPF / NCX / TOC
270
+ | 'downloadImage' // 下载图片(仅当存在图片时)
271
+ | 'zip' // 打包 .epub
272
+
273
+ interface IProgressEvent {
274
+ phase: ProgressPhase;
275
+ current: number; // 已完成数量
276
+ total: number; // 总数量
277
+ label?: string; // 当前条目标签(章节标题 / 图片 URL)
278
+ }
279
+ ```
280
+
281
+ 最小用法:
282
+
283
+ ```typescript
284
+ await epubGen(options, {
285
+ onProgress: (e) => {
286
+ console.log(`[${e.phase}] ${e.current}/${e.total}`);
287
+ },
288
+ });
289
+ ```
290
+
291
+ 回调中抛出的异常会被库静默吞掉,不会中断 EPUB 生成。
292
+
293
+ #### `concurrency?: number`
294
+
295
+ 写章节文件和下载图片所共用的并发上限。默认 `16`,机械硬盘或带宽受限场景可调小(如 `4`)。非有限正整数(NaN、负数等)会被自动归一化为默认值。
296
+
236
297
  ## 导出的类型
237
298
 
238
299
  库导出了所有 TypeScript 类型定义:
239
300
 
240
301
  ```typescript
241
- import type {
242
- IEpubGenOptions,
243
- IChapter,
302
+ import type {
303
+ IEpubGenOptions,
304
+ IChapter,
244
305
  IChapterData,
245
306
  IEpubData,
246
307
  IEpubImage,
247
- IOut
308
+ IGenConfigs,
309
+ ILogger,
310
+ IProgressEvent,
311
+ ProgressPhase,
312
+ IOut,
248
313
  } from 'wp-epub-gen';
249
314
  ```
250
315