wp-epub-gen 0.6.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 +93 -90
- package/build/index.cjs +1060 -1020
- package/build/index.js +1028 -990
- package/package.json +23 -31
package/README.md
CHANGED
|
@@ -15,109 +15,109 @@ npm install wp-epub-gen --save
|
|
|
15
15
|
### JavaScript (CommonJS)
|
|
16
16
|
|
|
17
17
|
```javascript
|
|
18
|
-
const { epubGen } = require(
|
|
18
|
+
const { epubGen } = require('wp-epub-gen')
|
|
19
19
|
|
|
20
20
|
epubGen({
|
|
21
|
-
title:
|
|
22
|
-
author:
|
|
23
|
-
output:
|
|
21
|
+
title: '我的电子书',
|
|
22
|
+
author: '作者名',
|
|
23
|
+
output: './my-book.epub',
|
|
24
24
|
content: [
|
|
25
25
|
{
|
|
26
|
-
title:
|
|
27
|
-
data:
|
|
28
|
-
}
|
|
29
|
-
]
|
|
26
|
+
title: '第一章',
|
|
27
|
+
data: '<h1>第一章</h1><p>这是第一章的内容...</p>',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
30
|
}).then(
|
|
31
|
-
() => console.log(
|
|
32
|
-
err => console.error(
|
|
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:
|
|
42
|
+
title: '我的电子书',
|
|
43
|
+
author: '作者名',
|
|
44
|
+
output: './my-book.epub',
|
|
45
45
|
content: [
|
|
46
46
|
{
|
|
47
|
-
title:
|
|
48
|
-
data:
|
|
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(
|
|
58
|
+
console.error('生成失败:', result.message)
|
|
59
59
|
}
|
|
60
60
|
} catch (error) {
|
|
61
|
-
console.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:
|
|
75
|
-
output:
|
|
71
|
+
title: '完整示例电子书',
|
|
72
|
+
author: ['张三', '李四'],
|
|
73
|
+
publisher: '我的出版社',
|
|
74
|
+
cover: 'https://example.com/cover.jpg',
|
|
75
|
+
output: './complete-book.epub',
|
|
76
76
|
version: 3,
|
|
77
|
-
lang:
|
|
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:
|
|
88
|
-
beforeToc: true
|
|
86
|
+
title: '前言',
|
|
87
|
+
data: '<h1>前言</h1><p>这是前言内容...</p>',
|
|
88
|
+
beforeToc: true,
|
|
89
89
|
},
|
|
90
90
|
{
|
|
91
|
-
title:
|
|
92
|
-
data:
|
|
91
|
+
title: '第一部分',
|
|
92
|
+
data: '<h1>第一部分</h1>',
|
|
93
93
|
children: [
|
|
94
94
|
{
|
|
95
|
-
title:
|
|
96
|
-
data:
|
|
95
|
+
title: '第一章',
|
|
96
|
+
data: '<h2>第一章</h2><p>第一章内容...</p>',
|
|
97
97
|
},
|
|
98
98
|
{
|
|
99
|
-
title:
|
|
100
|
-
data:
|
|
101
|
-
}
|
|
102
|
-
]
|
|
99
|
+
title: '第二章',
|
|
100
|
+
data: '<h2>第二章</h2><p>第二章内容...</p>',
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
103
|
},
|
|
104
104
|
{
|
|
105
|
-
title:
|
|
106
|
-
data:
|
|
105
|
+
title: '第二部分',
|
|
106
|
+
data: '<h1>第二部分</h1><p>第二部分内容...</p>',
|
|
107
107
|
},
|
|
108
108
|
{
|
|
109
|
-
title:
|
|
110
|
-
data:
|
|
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 参考
|
|
@@ -127,10 +127,12 @@ epubGen(options).then(result => {
|
|
|
127
127
|
主要的 EPUB 生成函数。
|
|
128
128
|
|
|
129
129
|
**参数:**
|
|
130
|
+
|
|
130
131
|
- `options: IEpubGenOptions` - 配置选项对象(标题、作者、封面、章节内容等)
|
|
131
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:
|
|
173
|
-
src: url(
|
|
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,9 +230,9 @@ 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 // 使用的配置选项
|
|
233
236
|
}
|
|
234
237
|
```
|
|
235
238
|
|
|
@@ -239,9 +242,9 @@ interface IOut {
|
|
|
239
242
|
|
|
240
243
|
```typescript
|
|
241
244
|
interface IGenConfigs {
|
|
242
|
-
logger?: ILogger
|
|
243
|
-
onProgress?: (e: IProgressEvent) => void
|
|
244
|
-
concurrency?: number
|
|
245
|
+
logger?: ILogger
|
|
246
|
+
onProgress?: (e: IProgressEvent) => void
|
|
247
|
+
concurrency?: number
|
|
245
248
|
}
|
|
246
249
|
```
|
|
247
250
|
|
|
@@ -251,10 +254,10 @@ interface IGenConfigs {
|
|
|
251
254
|
|
|
252
255
|
```typescript
|
|
253
256
|
interface ILogger {
|
|
254
|
-
log: (msg: any) => void
|
|
255
|
-
info: (msg: any) => void
|
|
256
|
-
warn: (msg: any) => void
|
|
257
|
-
error: (msg: any) => void
|
|
257
|
+
log: (msg: any) => void
|
|
258
|
+
info: (msg: any) => void
|
|
259
|
+
warn: (msg: any) => void
|
|
260
|
+
error: (msg: any) => void
|
|
258
261
|
}
|
|
259
262
|
```
|
|
260
263
|
|
|
@@ -264,17 +267,17 @@ interface ILogger {
|
|
|
264
267
|
|
|
265
268
|
```typescript
|
|
266
269
|
type ProgressPhase =
|
|
267
|
-
| 'parseContent'
|
|
268
|
-
| 'writeChapters'
|
|
269
|
-
| 'buildToc'
|
|
270
|
-
| 'downloadImage'
|
|
271
|
-
| 'zip'
|
|
270
|
+
| 'parseContent' // 解析章节 HTML
|
|
271
|
+
| 'writeChapters' // 写章节临时文件
|
|
272
|
+
| 'buildToc' // 渲染 OPF / NCX / TOC
|
|
273
|
+
| 'downloadImage' // 下载图片(仅当存在图片时)
|
|
274
|
+
| 'zip' // 打包 .epub
|
|
272
275
|
|
|
273
276
|
interface IProgressEvent {
|
|
274
|
-
phase: ProgressPhase
|
|
275
|
-
current: number
|
|
276
|
-
total: number
|
|
277
|
-
label?: string
|
|
277
|
+
phase: ProgressPhase
|
|
278
|
+
current: number // 已完成数量
|
|
279
|
+
total: number // 总数量
|
|
280
|
+
label?: string // 当前条目标签(章节标题 / 图片 URL)
|
|
278
281
|
}
|
|
279
282
|
```
|
|
280
283
|
|
|
@@ -283,9 +286,9 @@ interface IProgressEvent {
|
|
|
283
286
|
```typescript
|
|
284
287
|
await epubGen(options, {
|
|
285
288
|
onProgress: (e) => {
|
|
286
|
-
console.log(`[${e.phase}] ${e.current}/${e.total}`)
|
|
289
|
+
console.log(`[${e.phase}] ${e.current}/${e.total}`)
|
|
287
290
|
},
|
|
288
|
-
})
|
|
291
|
+
})
|
|
289
292
|
```
|
|
290
293
|
|
|
291
294
|
回调中抛出的异常会被库静默吞掉,不会中断 EPUB 生成。
|
|
@@ -310,29 +313,29 @@ import type {
|
|
|
310
313
|
IProgressEvent,
|
|
311
314
|
ProgressPhase,
|
|
312
315
|
IOut,
|
|
313
|
-
} from 'wp-epub-gen'
|
|
316
|
+
} from 'wp-epub-gen'
|
|
314
317
|
```
|
|
315
318
|
|
|
316
319
|
## 错误处理
|
|
317
320
|
|
|
318
321
|
```typescript
|
|
319
|
-
import { epubGen, errors } from 'wp-epub-gen'
|
|
322
|
+
import { epubGen, errors } from 'wp-epub-gen'
|
|
320
323
|
|
|
321
|
-
const result = await epubGen(options)
|
|
324
|
+
const result = await epubGen(options)
|
|
322
325
|
|
|
323
326
|
if (!result.success) {
|
|
324
327
|
switch (result.message) {
|
|
325
328
|
case errors.no_title:
|
|
326
|
-
console.error(
|
|
327
|
-
break
|
|
329
|
+
console.error('缺少标题')
|
|
330
|
+
break
|
|
328
331
|
case errors.no_output_path:
|
|
329
|
-
console.error(
|
|
330
|
-
break
|
|
332
|
+
console.error('缺少输出路径')
|
|
333
|
+
break
|
|
331
334
|
case errors.no_content:
|
|
332
|
-
console.error(
|
|
333
|
-
break
|
|
335
|
+
console.error('缺少内容')
|
|
336
|
+
break
|
|
334
337
|
default:
|
|
335
|
-
console.error(
|
|
338
|
+
console.error('未知错误:', result.message)
|
|
336
339
|
}
|
|
337
340
|
}
|
|
338
341
|
```
|