wp-epub-gen 0.2.3 → 0.3.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
@@ -1,87 +1,273 @@
1
- # epub-gen
1
+ # wp-epub-gen
2
2
 
3
- Forked from https://github.com/cyrilis/epub-gen/ .
3
+ 基于 [epub-gen](https://github.com/cyrilis/epub-gen/) 改进的 EPUB 生成器,支持 TypeScript 和现代 ES 模块。
4
4
 
5
- ## Usage
5
+ ## 安装
6
6
 
7
- Install the lib and add it as a dependency (recommended), run on your project dir:
7
+ 使用 npm 安装:
8
8
 
9
- ```shell script
9
+ ```bash
10
10
  npm install wp-epub-gen --save
11
11
  ```
12
-
13
- Then put this in your code:
12
+
13
+ ## 基本使用
14
+
15
+ ### JavaScript (CommonJS)
14
16
 
15
17
  ```javascript
16
- const {epubGen} = require("wp-epub-gen");
17
- epubGen(option).then(
18
- () => console.log("Ebook Generated Successfully!"),
19
- err => console.error("Failed to generate Ebook because of ", err)
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
- #### Options
24
-
25
- - `title`:
26
- Title of the book
27
- - `author`:
28
- Name of the author for the book, string or array, eg. `"Alice"` or `["Alice", "Bob"]`
29
- - `publisher`:
30
- Publisher name (optional)
31
- - `cover`:
32
- Book cover image (optional), File path (absolute path) or web url, eg. `"http://abc.com/book-cover.jpg"` or `"/User/Alice/images/book-cover.jpg"`
33
- - `output`
34
- Out put path (absolute path), you can also path output as the second argument, eg: `epubGen(options, output)`
35
- - `version`:
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
- - `lang`:
50
- Language of the book in 2 letters code (optional). If not specified, will fallback to `en`.
51
- - `tocTitle`:
52
- Title of the table of contents. If not specified, will fallback to `Table Of Contents`.
53
- - `appendChapterTitles`:
54
- Automatically append the chapter title at the beginning of each contents. You can disable that by specifying `false`.
55
- - `customOpfTemplatePath`:
56
- Optional. For advanced customizations: absolute path to an OPF template.
57
- - `customNcxTocTemplatePath`:
58
- Optional. For advanced customizations: absolute path to a NCX toc template.
59
- - `customHtmlTocTemplatePath`:
60
- Optional. For advanced customizations: absolute path to a HTML toc template.
61
- - `content`:
62
- Book Chapters content. It's should be an array of objects. eg. `[{title: "Chapter 1",data: "<div>..."}, {data: ""},...]`
63
- **Within each chapter object:**
64
- - `id`:
65
- optional, a unique ID.
66
- - `title`:
67
- optional, Chapter title.
68
- - `author`:
69
- optional, if each book author is different, you can fill it.
70
- - `data`:
71
- required, HTML String of the chapter content. image paths should be absolute path (should start with "http" or "https"), so that they could be downloaded. With the upgrade is possible to use local images (for this the path must start with file: //)
72
- - `excludeFromToc`:
73
- optional, if is not shown on Table of content, default: false.
74
- - `beforeToc`:
75
- optional, if is shown before Table of content, such like copyright pages. default: false.
76
- - `filename`:
77
- optional, specify filename for each chapter, default: undefined.
78
- - `children`:
79
- optional, an Array contains children contents.
80
- - `appendChapterTitle`:
81
- optional, Automatically append the chapter title at the beginning of current chapter, this value will overwrite the global `appendChapterTitles`.
82
- - `verbose`:
83
- specify whether to console.log progress messages, default: false.
84
- - `timeoutSeconds`:
85
- specify timeout in seconds, `0` means no timeout, default: 0.
86
- - `tocAutoNumber`:
87
- auto number for TOC, default: false.
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
+ ```
@@ -1,7 +1,2 @@
1
- /**
2
- * downloadImage
3
- * @author: oldj
4
- * @homepage: https://oldj.net
5
- */
6
1
  import { IEpubData } from './types';
7
2
  export declare const downloadAllImages: (epubData: IEpubData) => Promise<void>;
package/build/errors.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  /**
2
- * errors
3
2
  * @author: oldj
4
3
  * @homepage: https://oldj.net
5
4
  */
@@ -1,7 +1,2 @@
1
- /**
2
- * generateTempFile
3
- * @author: oldj
4
- * @homepage: https://oldj.net
5
- */
6
1
  import { IEpubData } from './types';
7
2
  export declare const generateTempFile: (epubData: IEpubData) => Promise<void>;
package/build/index.d.ts CHANGED
@@ -1,9 +1,5 @@
1
- /**
2
- * index.ts
3
- * @author: oldj
4
- * @homepage: https://oldj.net
5
- */
6
1
  import { IEpubGenOptions, IOut } from './types';
7
2
  export declare function epubGen(options: IEpubGenOptions, output?: string): Promise<IOut>;
8
3
  export declare const gen: typeof epubGen;
9
4
  export { errors } from './errors';
5
+ export type { IEpubImage, IEpubGenOptions, IEpubData, IChapter, IChapterData, IOut } from './types';