wp-epub-gen 0.2.3 → 0.4.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/.vscode/settings.json +26 -0
- package/README.md +261 -75
- package/build/downloadImage.d.ts +0 -5
- package/build/errors.d.ts +0 -1
- package/build/generateTempFile.d.ts +0 -5
- package/build/index.cjs +1279 -0
- package/build/index.d.ts +3 -7
- package/build/index.js +927 -425
- package/build/libs/safeFineName.d.ts +0 -1
- package/build/libs/utils.d.ts +1 -7
- package/build/logger.d.ts +40 -0
- package/build/makeCover.d.ts +0 -5
- package/build/parseContent.d.ts +0 -5
- package/build/parseContent.test.d.ts +1 -0
- package/build/render.d.ts +0 -5
- package/build/templates.d.ts +11 -0
- package/build/types.d.ts +9 -1
- package/eslint.config.js +33 -0
- package/package.json +37 -35
- package/templates/epub3/toc.xhtml.ejs +1 -1
- package/tsconfig.json +8 -7
- package/vite.config.ts +76 -3
- package/vitest.config.ts +15 -0
- package/.eslintrc.js +0 -24
- package/.mocharc.json +0 -3
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"files.exclude": {
|
|
3
|
+
".idea": true,
|
|
4
|
+
"*.iml": true,
|
|
5
|
+
"**/node_modules": true,
|
|
6
|
+
"**/*.log": true,
|
|
7
|
+
"build": true,
|
|
8
|
+
},
|
|
9
|
+
"[javascript]": {
|
|
10
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
11
|
+
},
|
|
12
|
+
"[javascriptreact]": {
|
|
13
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
14
|
+
},
|
|
15
|
+
"[typescript]": {
|
|
16
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
17
|
+
},
|
|
18
|
+
"[typescriptreact]": {
|
|
19
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
20
|
+
},
|
|
21
|
+
"editor.codeActionsOnSave": {
|
|
22
|
+
"source.fixAll.eslint": "explicit",
|
|
23
|
+
"source.organizeImports": "explicit"
|
|
24
|
+
},
|
|
25
|
+
"editor.formatOnSave": true
|
|
26
|
+
}
|
package/README.md
CHANGED
|
@@ -1,87 +1,273 @@
|
|
|
1
|
-
# epub-gen
|
|
1
|
+
# wp-epub-gen
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
基于 [epub-gen](https://github.com/cyrilis/epub-gen/) 改进的 EPUB 生成器,支持 TypeScript 和现代 ES 模块。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 安装
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
使用 npm 安装:
|
|
8
8
|
|
|
9
|
-
```
|
|
9
|
+
```bash
|
|
10
10
|
npm install wp-epub-gen --save
|
|
11
11
|
```
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
|
|
13
|
+
## 基本使用
|
|
14
|
+
|
|
15
|
+
### JavaScript (CommonJS)
|
|
14
16
|
|
|
15
17
|
```javascript
|
|
16
|
-
const {epubGen} = require("wp-epub-gen");
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const { epubGen } = require("wp-epub-gen");
|
|
19
|
+
|
|
20
|
+
epubGen({
|
|
21
|
+
title: "我的电子书",
|
|
22
|
+
author: "作者名",
|
|
23
|
+
output: "./my-book.epub",
|
|
24
|
+
content: [
|
|
25
|
+
{
|
|
26
|
+
title: "第一章",
|
|
27
|
+
data: "<h1>第一章</h1><p>这是第一章的内容...</p>"
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
}).then(
|
|
31
|
+
() => console.log("电子书生成成功!"),
|
|
32
|
+
err => console.error("生成失败:", err)
|
|
20
33
|
);
|
|
21
34
|
```
|
|
22
35
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
You can specify the version of the generated EPUB, `3` the latest version (http://idpf.org/epub/30) or `2` the previous version (http://idpf.org/epub/201, for better compatibility with older readers). If not specified, will fallback to `3`.
|
|
37
|
-
- `css`:
|
|
38
|
-
If you really hate our css, you can pass css string to replace our default style. eg: `"body {background: #000}"`
|
|
39
|
-
- `fonts`:
|
|
40
|
-
Array of (absolute) paths to custom fonts to include on the book so they can be used on custom css. Ex: if you configure the array to `fonts: ['/path/to/Merriweather.ttf']` you can use the following on the custom CSS:
|
|
41
|
-
```
|
|
42
|
-
@font-face {
|
|
43
|
-
font-family: "Merriweather";
|
|
44
|
-
font-style: normal;
|
|
45
|
-
font-weight: normal;
|
|
46
|
-
src : url("./fonts/Merriweather.ttf");
|
|
36
|
+
### TypeScript / ES 模块
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { epubGen, type IEpubGenOptions } from 'wp-epub-gen';
|
|
40
|
+
|
|
41
|
+
const options: IEpubGenOptions = {
|
|
42
|
+
title: "我的电子书",
|
|
43
|
+
author: "作者名",
|
|
44
|
+
output: "./my-book.epub",
|
|
45
|
+
content: [
|
|
46
|
+
{
|
|
47
|
+
title: "第一章",
|
|
48
|
+
data: "<h1>第一章</h1><p>这是第一章的内容...</p>"
|
|
47
49
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
50
|
+
]
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const result = await epubGen(options);
|
|
55
|
+
if (result.success) {
|
|
56
|
+
console.log("电子书生成成功!");
|
|
57
|
+
} else {
|
|
58
|
+
console.error("生成失败:", result.message);
|
|
59
|
+
}
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.error("发生错误:", error);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 完整示例
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { epubGen } from 'wp-epub-gen';
|
|
69
|
+
|
|
70
|
+
const options = {
|
|
71
|
+
title: "完整示例电子书",
|
|
72
|
+
author: ["张三", "李四"],
|
|
73
|
+
publisher: "我的出版社",
|
|
74
|
+
cover: "https://example.com/cover.jpg",
|
|
75
|
+
output: "./complete-book.epub",
|
|
76
|
+
version: 3,
|
|
77
|
+
lang: "zh-cn",
|
|
78
|
+
css: "body { font-family: 'Microsoft YaHei', sans-serif; }",
|
|
79
|
+
tocTitle: "目录",
|
|
80
|
+
appendChapterTitles: true,
|
|
81
|
+
tocAutoNumber: true,
|
|
82
|
+
verbose: true,
|
|
83
|
+
timeoutSeconds: 60,
|
|
84
|
+
content: [
|
|
85
|
+
{
|
|
86
|
+
title: "前言",
|
|
87
|
+
data: "<h1>前言</h1><p>这是前言内容...</p>",
|
|
88
|
+
beforeToc: true
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
title: "第一部分",
|
|
92
|
+
data: "<h1>第一部分</h1>",
|
|
93
|
+
children: [
|
|
94
|
+
{
|
|
95
|
+
title: "第一章",
|
|
96
|
+
data: "<h2>第一章</h2><p>第一章内容...</p>"
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
title: "第二章",
|
|
100
|
+
data: "<h2>第二章</h2><p>第二章内容...</p>"
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
title: "第二部分",
|
|
106
|
+
data: "<h1>第二部分</h1><p>第二部分内容...</p>"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
title: "附录",
|
|
110
|
+
data: "<h1>附录</h1><p>附录内容...</p>",
|
|
111
|
+
excludeFromToc: true
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
epubGen(options).then(result => {
|
|
117
|
+
if (result.success) {
|
|
118
|
+
console.log("电子书生成成功!");
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## API 参考
|
|
124
|
+
|
|
125
|
+
### epubGen(options, output?)
|
|
126
|
+
|
|
127
|
+
主要的 EPUB 生成函数。
|
|
128
|
+
|
|
129
|
+
**参数:**
|
|
130
|
+
- `options: IEpubGenOptions` - 配置选项对象
|
|
131
|
+
- `output?: string` - 可选的输出路径,会覆盖 options.output
|
|
132
|
+
|
|
133
|
+
**返回值:**
|
|
134
|
+
- `Promise<IOut>` - 包含生成结果的 Promise
|
|
135
|
+
|
|
136
|
+
### IEpubGenOptions 配置选项
|
|
137
|
+
|
|
138
|
+
#### 必需参数
|
|
139
|
+
|
|
140
|
+
- **`title: string`** - 电子书标题
|
|
141
|
+
- **`output: string`** - 输出文件路径(绝对路径)
|
|
142
|
+
- **`content: IChapter[]`** - 章节内容数组
|
|
143
|
+
|
|
144
|
+
#### 可选参数
|
|
145
|
+
|
|
146
|
+
- **`author?: string | string[]`** - 作者名称,可以是字符串或字符串数组
|
|
147
|
+
- 示例:`"张三"` 或 `["张三", "李四"]`
|
|
148
|
+
|
|
149
|
+
- **`publisher?: string`** - 出版社名称
|
|
150
|
+
|
|
151
|
+
- **`cover?: string`** - 封面图片
|
|
152
|
+
- 支持网络 URL:`"https://example.com/cover.jpg"`
|
|
153
|
+
- 支持本地文件:`"/path/to/cover.jpg"`
|
|
154
|
+
|
|
155
|
+
- **`version?: 2 | 3`** - EPUB 版本
|
|
156
|
+
- `3`:最新版本(默认)
|
|
157
|
+
- `2`:兼容老设备
|
|
158
|
+
|
|
159
|
+
- **`lang?: string`** - 语言代码
|
|
160
|
+
- 默认:`"en"`
|
|
161
|
+
- 中文:`"zh-cn"`
|
|
162
|
+
|
|
163
|
+
- **`css?: string`** - 自定义 CSS 样式
|
|
164
|
+
- 示例:`"body { font-family: 'Microsoft YaHei'; }"`
|
|
165
|
+
|
|
166
|
+
- **`fonts?: string[]`** - 自定义字体文件路径数组
|
|
167
|
+
- 示例:`["/path/to/font.ttf"]`
|
|
168
|
+
|
|
169
|
+
使用方法:
|
|
170
|
+
```css
|
|
171
|
+
@font-face {
|
|
172
|
+
font-family: "CustomFont";
|
|
173
|
+
src: url("./fonts/font.ttf");
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
- **`tocTitle?: string`** - 目录标题
|
|
178
|
+
- 默认:`"Table Of Contents"`
|
|
179
|
+
|
|
180
|
+
- **`appendChapterTitles?: boolean`** - 是否在章节开头自动添加标题
|
|
181
|
+
- 默认:`true`
|
|
182
|
+
|
|
183
|
+
- **`tocAutoNumber?: boolean`** - 目录是否自动编号
|
|
184
|
+
- 默认:`false`
|
|
185
|
+
|
|
186
|
+
- **`verbose?: boolean`** - 是否输出详细日志
|
|
187
|
+
- 默认:`false`
|
|
188
|
+
|
|
189
|
+
- **`timeoutSeconds?: number`** - 超时时间(秒)
|
|
190
|
+
- `0` 表示无超时
|
|
191
|
+
- 默认:`900`(15分钟)
|
|
192
|
+
|
|
193
|
+
- **`description?: string`** - 电子书描述
|
|
194
|
+
|
|
195
|
+
- **`date?: string`** - 出版日期(ISO 格式)
|
|
196
|
+
|
|
197
|
+
- **`tmpDir?: string`** - 临时目录路径
|
|
198
|
+
|
|
199
|
+
#### 高级自定义选项
|
|
200
|
+
|
|
201
|
+
- **`customOpfTemplatePath?: string`** - 自定义 OPF 模板文件路径
|
|
202
|
+
- **`customNcxTocTemplatePath?: string`** - 自定义 NCX 目录模板文件路径
|
|
203
|
+
- **`customHtmlTocTemplatePath?: string`** - 自定义 HTML 目录模板文件路径
|
|
204
|
+
|
|
205
|
+
### IChapter 章节对象
|
|
206
|
+
|
|
207
|
+
每个章节对象可以包含以下属性:
|
|
208
|
+
|
|
209
|
+
#### 必需属性
|
|
210
|
+
|
|
211
|
+
- **`data: string`** - 章节的 HTML 内容
|
|
212
|
+
- 网络图片:`<img src="https://example.com/image.jpg" />`
|
|
213
|
+
- 本地图片:`<img src="file:///path/to/image.jpg" />`
|
|
214
|
+
|
|
215
|
+
#### 可选属性
|
|
216
|
+
|
|
217
|
+
- **`id?: string`** - 唯一标识符
|
|
218
|
+
- **`title?: string`** - 章节标题
|
|
219
|
+
- **`author?: string | string[]`** - 章节作者(覆盖全局作者)
|
|
220
|
+
- **`filename?: string`** - 自定义文件名
|
|
221
|
+
- **`excludeFromToc?: boolean`** - 是否从目录中排除(默认:`false`)
|
|
222
|
+
- **`beforeToc?: boolean`** - 是否显示在目录之前(如版权页)(默认:`false`)
|
|
223
|
+
- **`appendChapterTitle?: boolean`** - 覆盖全局的 `appendChapterTitles` 设置
|
|
224
|
+
- **`children?: IChapter[]`** - 子章节数组(用于创建层级结构)
|
|
225
|
+
|
|
226
|
+
### 返回值类型 IOut
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
interface IOut {
|
|
230
|
+
success?: boolean; // 是否成功
|
|
231
|
+
message?: string; // 错误信息(如果失败)
|
|
232
|
+
options?: IEpubGenOptions; // 使用的配置选项
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## 导出的类型
|
|
237
|
+
|
|
238
|
+
库导出了所有 TypeScript 类型定义:
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
import type {
|
|
242
|
+
IEpubGenOptions,
|
|
243
|
+
IChapter,
|
|
244
|
+
IChapterData,
|
|
245
|
+
IEpubData,
|
|
246
|
+
IEpubImage,
|
|
247
|
+
IOut
|
|
248
|
+
} from 'wp-epub-gen';
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## 错误处理
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
import { epubGen, errors } from 'wp-epub-gen';
|
|
255
|
+
|
|
256
|
+
const result = await epubGen(options);
|
|
257
|
+
|
|
258
|
+
if (!result.success) {
|
|
259
|
+
switch (result.message) {
|
|
260
|
+
case errors.no_title:
|
|
261
|
+
console.error("缺少标题");
|
|
262
|
+
break;
|
|
263
|
+
case errors.no_output_path:
|
|
264
|
+
console.error("缺少输出路径");
|
|
265
|
+
break;
|
|
266
|
+
case errors.no_content:
|
|
267
|
+
console.error("缺少内容");
|
|
268
|
+
break;
|
|
269
|
+
default:
|
|
270
|
+
console.error("未知错误:", result.message);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
```
|
package/build/downloadImage.d.ts
CHANGED
package/build/errors.d.ts
CHANGED