wp-epub-gen 0.1.3 → 0.1.4

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.
Files changed (42) hide show
  1. package/{dist → build}/downloadImage.d.ts +0 -0
  2. package/{dist → build}/downloadImage.js +0 -0
  3. package/build/downloadImage.js.map +1 -0
  4. package/{dist → build}/errors.d.ts +0 -0
  5. package/{dist → build}/errors.js +0 -0
  6. package/build/errors.js.map +1 -0
  7. package/{dist → build}/generateTempFile.d.ts +0 -0
  8. package/{dist → build}/generateTempFile.js +0 -0
  9. package/build/generateTempFile.js.map +1 -0
  10. package/{dist → build}/index.d.ts +0 -0
  11. package/{dist → build}/index.js +0 -0
  12. package/build/index.js.map +1 -0
  13. package/{dist → build}/makeCover.d.ts +0 -0
  14. package/{dist → build}/makeCover.js +0 -0
  15. package/build/makeCover.js.map +1 -0
  16. package/{dist → build}/parseContent.d.ts +0 -0
  17. package/{dist → build}/parseContent.js +0 -0
  18. package/build/parseContent.js.map +1 -0
  19. package/{dist → build}/render.d.ts +0 -0
  20. package/{dist → build}/render.js +0 -0
  21. package/build/render.js.map +1 -0
  22. package/{dist → build}/utils.d.ts +0 -0
  23. package/{dist → build}/utils.js +0 -0
  24. package/build/utils.js.map +1 -0
  25. package/cpi.epub +0 -0
  26. package/package.json +16 -20
  27. package/test.epub +0 -0
  28. package/tsconfig.json +1 -1
  29. package/src/downloadImage.ts +0 -83
  30. package/src/errors.ts +0 -11
  31. package/src/generateTempFile.ts +0 -144
  32. package/src/index.ts +0 -152
  33. package/src/makeCover.ts +0 -52
  34. package/src/parseContent.ts +0 -351
  35. package/src/render.ts +0 -84
  36. package/src/types.d.ts +0 -79
  37. package/src/utils.ts +0 -39
  38. package/test/basic.test.js +0 -28
  39. package/test/child-page-image.test.js +0 -21
  40. package/test/data/1.json +0 -21
  41. package/test/tt.js +0 -16
  42. package/webpack.config.js +0 -75
@@ -1,351 +0,0 @@
1
- /**
2
- * parseContent
3
- * @author: oldj
4
- * @homepage: https://oldj.net
5
- */
6
-
7
- import * as cheerio from 'cheerio'
8
- import { remove as removeDiacritics } from 'diacritics'
9
- import * as mime from 'mime'
10
- import * as path from 'path'
11
- import * as uslug from 'uslug'
12
- import { v4 as uuidv4 } from 'uuid'
13
- import { IChapter, IChapterData, IEpubData, IEpubImage } from './types'
14
-
15
- export default function parseContent(
16
- content: IChapter,
17
- index: number | string,
18
- epubConfigs: IEpubData,
19
- ): IChapterData {
20
- let chapter: IChapterData = { ...content } as IChapterData
21
-
22
- if (!chapter.filename) {
23
- let titleSlug = uslug(removeDiacritics(chapter.title || 'no title'))
24
- titleSlug = titleSlug.replace(/[\/\\]/g, '_')
25
- chapter.href = `${index}_${titleSlug}.xhtml`
26
- chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', chapter.href)
27
- } else {
28
- let is_xhtml = chapter.filename.endsWith('.xhtml')
29
- chapter.href = is_xhtml ? chapter.filename : `${chapter.filename}.xhtml`
30
- if (is_xhtml) {
31
- chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', chapter.filename)
32
- } else {
33
- chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', `${chapter.filename}.xhtml`)
34
- }
35
- }
36
-
37
- chapter.id = `item_${index}`
38
- chapter.dir = path.dirname(chapter.filePath)
39
- chapter.excludeFromToc = chapter.excludeFromToc || false
40
- chapter.beforeToc = chapter.beforeToc || false
41
-
42
- // fix author array
43
- if (chapter.author && typeof chapter.author === 'string') {
44
- chapter.author = [chapter.author]
45
- } else if (!chapter.author || !Array.isArray(chapter.author)) {
46
- chapter.author = []
47
- }
48
-
49
- let allowedAttributes = [
50
- 'content',
51
- 'alt',
52
- 'id',
53
- 'title',
54
- 'src',
55
- 'href',
56
- 'about',
57
- 'accesskey',
58
- 'aria-activedescendant',
59
- 'aria-atomic',
60
- 'aria-autocomplete',
61
- 'aria-busy',
62
- 'aria-checked',
63
- 'aria-controls',
64
- 'aria-describedat',
65
- 'aria-describedby',
66
- 'aria-disabled',
67
- 'aria-dropeffect',
68
- 'aria-expanded',
69
- 'aria-flowto',
70
- 'aria-grabbed',
71
- 'aria-haspopup',
72
- 'aria-hidden',
73
- 'aria-invalid',
74
- 'aria-label',
75
- 'aria-labelledby',
76
- 'aria-level',
77
- 'aria-live',
78
- 'aria-multiline',
79
- 'aria-multiselectable',
80
- 'aria-orientation',
81
- 'aria-owns',
82
- 'aria-posinset',
83
- 'aria-pressed',
84
- 'aria-readonly',
85
- 'aria-relevant',
86
- 'aria-required',
87
- 'aria-selected',
88
- 'aria-setsize',
89
- 'aria-sort',
90
- 'aria-valuemax',
91
- 'aria-valuemin',
92
- 'aria-valuenow',
93
- 'aria-valuetext',
94
- 'class',
95
- 'content',
96
- 'contenteditable',
97
- 'contextmenu',
98
- 'datatype',
99
- 'dir',
100
- 'draggable',
101
- 'dropzone',
102
- 'hidden',
103
- 'hreflang',
104
- 'id',
105
- 'inlist',
106
- 'itemid',
107
- 'itemref',
108
- 'itemscope',
109
- 'itemtype',
110
- 'lang',
111
- 'media',
112
- 'ns1:type',
113
- 'ns2:alphabet',
114
- 'ns2:ph',
115
- 'onabort',
116
- 'onblur',
117
- 'oncanplay',
118
- 'oncanplaythrough',
119
- 'onchange',
120
- 'onclick',
121
- 'oncontextmenu',
122
- 'ondblclick',
123
- 'ondrag',
124
- 'ondragend',
125
- 'ondragenter',
126
- 'ondragleave',
127
- 'ondragover',
128
- 'ondragstart',
129
- 'ondrop',
130
- 'ondurationchange',
131
- 'onemptied',
132
- 'onended',
133
- 'onerror',
134
- 'onfocus',
135
- 'oninput',
136
- 'oninvalid',
137
- 'onkeydown',
138
- 'onkeypress',
139
- 'onkeyup',
140
- 'onload',
141
- 'onloadeddata',
142
- 'onloadedmetadata',
143
- 'onloadstart',
144
- 'onmousedown',
145
- 'onmousemove',
146
- 'onmouseout',
147
- 'onmouseover',
148
- 'onmouseup',
149
- 'onmousewheel',
150
- 'onpause',
151
- 'onplay',
152
- 'onplaying',
153
- 'onprogress',
154
- 'onratechange',
155
- 'onreadystatechange',
156
- 'onreset',
157
- 'onscroll',
158
- 'onseeked',
159
- 'onseeking',
160
- 'onselect',
161
- 'onshow',
162
- 'onstalled',
163
- 'onsubmit',
164
- 'onsuspend',
165
- 'ontimeupdate',
166
- 'onvolumechange',
167
- 'onwaiting',
168
- 'prefix',
169
- 'property',
170
- 'rel',
171
- 'resource',
172
- 'rev',
173
- 'role',
174
- 'spellcheck',
175
- 'style',
176
- 'tabindex',
177
- 'target',
178
- 'title',
179
- 'type',
180
- 'typeof',
181
- 'vocab',
182
- 'xml:base',
183
- 'xml:lang',
184
- 'xml:space',
185
- 'colspan',
186
- 'rowspan',
187
- 'epub:type',
188
- 'epub:prefix',
189
- ]
190
- let allowedXhtml11Tags = [
191
- 'div',
192
- 'p',
193
- 'h1',
194
- 'h2',
195
- 'h3',
196
- 'h4',
197
- 'h5',
198
- 'h6',
199
- 'ul',
200
- 'ol',
201
- 'li',
202
- 'dl',
203
- 'dt',
204
- 'dd',
205
- 'address',
206
- 'hr',
207
- 'pre',
208
- 'blockquote',
209
- 'center',
210
- 'ins',
211
- 'del',
212
- 'a',
213
- 'span',
214
- 'bdo',
215
- 'br',
216
- 'em',
217
- 'strong',
218
- 'dfn',
219
- 'code',
220
- 'samp',
221
- 'kbd',
222
- 'bar',
223
- 'cite',
224
- 'abbr',
225
- 'acronym',
226
- 'q',
227
- 'sub',
228
- 'sup',
229
- 'tt',
230
- 'i',
231
- 'b',
232
- 'big',
233
- 'small',
234
- 'u',
235
- 's',
236
- 'strike',
237
- 'basefont',
238
- 'font',
239
- 'object',
240
- 'param',
241
- 'img',
242
- 'table',
243
- 'caption',
244
- 'colgroup',
245
- 'col',
246
- 'thead',
247
- 'tfoot',
248
- 'tbody',
249
- 'tr',
250
- 'th',
251
- 'td',
252
- 'embed',
253
- 'applet',
254
- 'iframe',
255
- 'img',
256
- 'map',
257
- 'noscript',
258
- 'ns:svg',
259
- 'object',
260
- 'script',
261
- 'table',
262
- 'tt',
263
- 'var',
264
- ]
265
-
266
- let $ = cheerio.load(chapter.data, {
267
- lowerCaseTags: true,
268
- recognizeSelfClosing: true,
269
- })
270
-
271
- // only body innerHTML is allowed
272
- let body = $('body')
273
- if (body.length) {
274
- let html = body.html()
275
- if (html) {
276
- $ = cheerio.load(html, {
277
- lowerCaseTags: true,
278
- recognizeSelfClosing: true,
279
- })
280
- }
281
- }
282
-
283
- $($('*').get().reverse()).each(function (elemIndex, elem) {
284
- // @ts-ignore
285
- let attrs = elem.attribs
286
- // @ts-ignore
287
- let that: CheerioElement = this
288
- let tags = ['img', 'br', 'hr']
289
- if (tags.includes(that.name)) {
290
- if (that.name === 'img' && !$(that).attr('alt')) {
291
- $(that).attr('alt', 'image-placeholder')
292
- }
293
- }
294
-
295
- Object.entries(attrs).map(([k, v]) => {
296
- if (allowedAttributes.includes(k)) {
297
- if (k === 'type' && that.name !== 'script') {
298
- $(that).removeAttr(k)
299
- }
300
- } else {
301
- $(that).removeAttr(k)
302
- }
303
- })
304
-
305
- if (epubConfigs.version === 2) {
306
- if (!allowedXhtml11Tags.includes(that.name)) {
307
- if (epubConfigs.verbose) {
308
- console.log(
309
- 'Warning (content[' + index + ']):',
310
- that.name,
311
- "tag isn't allowed on EPUB 2/XHTML 1.1 DTD.",
312
- )
313
- }
314
- let child = $(that).html()
315
- $(that).replaceWith($('<div>' + child + '</div>'))
316
- }
317
- }
318
- })
319
-
320
- $('img').each((index, elem) => {
321
- let url = $(elem).attr('src') || ''
322
- let image = epubConfigs.images.find((el) => el.url === url)
323
- let id: string
324
- let extension: string
325
-
326
- if (image) {
327
- id = image.id
328
- extension = image.extension
329
- } else {
330
- id = uuidv4()
331
- let mediaType: string = mime.getType(url.replace(/\?.*/, '')) || ''
332
- extension = mime.getExtension(mediaType) || ''
333
- let dir = chapter.dir || ''
334
-
335
- let img: IEpubImage = { id, url, dir, mediaType, extension }
336
- epubConfigs.images.push(img)
337
- }
338
-
339
- $(elem).attr('src', `images/${id}.${extension}`)
340
- })
341
-
342
- chapter.data = $.xml()
343
-
344
- if (Array.isArray(chapter.children)) {
345
- chapter.children = chapter.children.map((content, idx) =>
346
- parseContent(content, `${index}_${idx}`, epubConfigs),
347
- )
348
- }
349
-
350
- return chapter
351
- }
package/src/render.ts DELETED
@@ -1,84 +0,0 @@
1
- /**
2
- * render
3
- * @author: oldj
4
- * @homepage: https://oldj.net
5
- */
6
-
7
- import * as archiver from 'archiver'
8
- import * as fs from 'fs-extra'
9
- import * as path from 'path'
10
- import { downloadAllImages } from './downloadImage'
11
- import { generateTempFile } from './generateTempFile'
12
- import makeCover from './makeCover'
13
- import { IEpubData } from './types'
14
- import { fileIsStable } from './utils'
15
-
16
- export async function render(data: IEpubData): Promise<void> {
17
- let { log } = data
18
-
19
- log('Generating Template Files...')
20
- await generateTempFile(data)
21
- log('Downloading Images...')
22
- await downloadAllImages(data)
23
- log('Making Cover...')
24
- await makeCover(data)
25
- log('Generating Epub Files...')
26
- await genEpub(data)
27
- if (fs.existsSync(data.output)) {
28
- log('Output: ' + data.output)
29
- log('Done.')
30
- } else {
31
- log('Output fail!')
32
- }
33
- }
34
-
35
- async function genEpub(epubData: IEpubData): Promise<void> {
36
- let { log, dir, output } = epubData
37
-
38
- let archive = archiver('zip', { zlib: { level: 9 } })
39
- let outputStream = fs.createWriteStream(epubData.output)
40
- log('Zipping temp dir to ' + output)
41
-
42
- return new Promise((resolve, reject) => {
43
- archive.on('end', async () => {
44
- log('Done zipping, clearing temp dir...')
45
-
46
- // log(fs.statSync(epubData.output).size)
47
- // setTimeout(() => log(fs.statSync(epubData.output).size), 1)
48
- // setTimeout(() => log(fs.statSync(epubData.output).size), 100)
49
- // setTimeout(() => log(fs.statSync(epubData.output).size), 1000)
50
- let stable = await fileIsStable(epubData.output)
51
- if (!stable) {
52
- log('Output epub file is not stable!')
53
- }
54
-
55
- try {
56
- fs.removeSync(dir)
57
- resolve()
58
- } catch (e) {
59
- log('[Error] ' + e.message)
60
- reject(e)
61
- }
62
- })
63
-
64
- archive.on('close', () => {
65
- log('Zip close..')
66
- })
67
-
68
- archive.on('finish', () => {
69
- log('Zip finish..')
70
- })
71
-
72
- archive.on('error', (err) => {
73
- log('[Archive Error] ' + err.message)
74
- reject(err)
75
- })
76
-
77
- archive.pipe(outputStream)
78
- archive.append('application/epub+zip', { store: true, name: 'mimetype' })
79
- archive.directory(path.join(dir, 'META-INF'), 'META-INF')
80
- archive.directory(path.join(dir, 'OEBPS'), 'OEBPS')
81
-
82
- archive.finalize()
83
- })
84
- }
package/src/types.d.ts DELETED
@@ -1,79 +0,0 @@
1
- /**
2
- * types.d.ts
3
- * @author: oldj
4
- * @homepage: https://oldj.net
5
- */
6
-
7
- export interface IEpubImage {
8
- id: string
9
- url: string
10
- dir: string
11
- mediaType: string
12
- extension: string
13
- }
14
-
15
- export interface IEpubGenOptions {
16
- title: string
17
- author?: string | string[]
18
- publisher?: string
19
- cover: string
20
- output: string
21
- version?: 2 | 3
22
- css?: string
23
- fonts?: string[]
24
- lang?: 'en'
25
- tocTitle?: string
26
- appendChapterTitles?: boolean // For advanced customizations: absolute path to an OPF template.
27
- customOpfTemplatePath?: string // For advanced customizations: absolute path to a NCX toc template.
28
- customHtmlTocTemplatePath?: string // For advanced customizations: absolute path to a HTML toc template.
29
- customNcxTocTemplatePath?: string
30
- content: IChapter[] // Book Chapters content. It's should be an array of objects. eg. [{title: "Chapter 1",data: "<div>..."}, {data: ""},...]
31
- verbose?: boolean // specify whether or not to console.log progress messages, default: false
32
- description?: string
33
- date?: string
34
- tmpDir?: string
35
- timeoutSeconds?: number
36
- tocAutoNumber?: boolean
37
- }
38
-
39
- export interface IEpubData extends IEpubGenOptions {
40
- id: string
41
- docHeader: string
42
- dir: string
43
- images: IEpubImage[]
44
- tmpDir: string
45
- baseDir: string
46
- _coverMediaType?: string
47
- _coverExtension?: string
48
- log: (msg) => void
49
- content: IChapterData[]
50
- timeoutSeconds: number
51
- }
52
-
53
- export interface IChapter {
54
- id?: string
55
- title?: string
56
- author?: string | string[]
57
- data: string // 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: //)
58
- excludeFromToc?: boolean // default: false
59
- beforeToc?: boolean // if is shown before Table of content, such like copyright pages. default: false
60
- filename?: string // specify filename for each chapter
61
- url?: string
62
- children?: IChapter[]
63
- appendChapterTitle?: boolean
64
- }
65
-
66
- export interface IChapterData extends IChapter {
67
- id: string
68
- href?: string
69
- dir?: string
70
- children: IChapterData[]
71
- filePath: string
72
- author: string[]
73
- }
74
-
75
- export interface IOut {
76
- success?: boolean
77
- message?: string
78
- options?: IEpubGenOptions
79
- }
package/src/utils.ts DELETED
@@ -1,39 +0,0 @@
1
- /**
2
- * utils
3
- * @author: oldj
4
- * @homepage: https://oldj.net
5
- */
6
-
7
- import * as fs from 'fs'
8
- import { promisify } from 'util'
9
-
10
- export const readFile = promisify(fs.readFile)
11
- export const writeFile = promisify(fs.writeFile)
12
-
13
- export const USER_AGENT =
14
- 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36'
15
-
16
- export const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
17
-
18
- export async function fileIsStable(filename: string, max_wait: number = 30000): Promise<boolean> {
19
- let start_time = new Date().getTime()
20
- let last_size = fs.statSync(filename).size
21
-
22
- while (new Date().getTime() - start_time <= max_wait) {
23
- await wait(1000)
24
- let size = fs.statSync(filename).size
25
- if (size === last_size) return true
26
- last_size = size
27
- }
28
-
29
- return false
30
- }
31
-
32
- export function simpleMinifier(xhtml: string) {
33
- xhtml = xhtml
34
- .replace(/\s+<\/a>/gi, '</a>')
35
- .replace(/\n\s+</g, '\n<')
36
- .replace(/\n+/g, '\n')
37
-
38
- return xhtml
39
- }
@@ -1,28 +0,0 @@
1
- /**
2
- * @author: oldj
3
- * @homepage: https://oldj.net
4
- */
5
-
6
- import { assert } from 'chai'
7
- import { epubGen } from '../dist/index'
8
- import { errors } from '../dist/errors'
9
- import * as data from './data/1.json'
10
-
11
- describe('Basic', () => {
12
- it('no output path error', async () => {
13
- let out = await epubGen({ ...data, verbose: false })
14
- assert.equal(out.success, false)
15
- assert.equal(out.message, errors.no_output_path)
16
- })
17
-
18
- it('basic test', async () => {
19
- let out = { options: {} }
20
- try {
21
- out = await epubGen({ ...data }, 'test.epub')
22
- } catch (e) {
23
- console.error('Error 24: ' + e.message)
24
- }
25
- assert.equal(out.success, true)
26
- assert.equal(typeof out.options.tmpDir, 'string')
27
- }).timeout(60 * 1000)
28
- })
@@ -1,21 +0,0 @@
1
- /**
2
- * @author: oldj
3
- * @homepage: https://oldj.net
4
- */
5
-
6
- import { assert } from 'chai'
7
- import { epubGen } from '../dist/index'
8
- import * as data from './data/1.json'
9
-
10
- describe('Child page image', () => {
11
- it.only('should has image', async () => {
12
- let out = { options: {} }
13
- try {
14
- out = await epubGen({ ...data }, 'cpi.epub')
15
- } catch (e) {
16
- console.error('Error 24: ' + e.message)
17
- }
18
- assert.equal(out.success, true)
19
- assert.equal(typeof out.options.tmpDir, 'string')
20
- }).timeout(60 * 1000)
21
- })
package/test/data/1.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "title": "多层目录",
3
- "author": "Andy",
4
- "lang": "zh-cn",
5
- "content": [
6
- {
7
- "title": "aaa",
8
- "data": "<div>aa</div>"
9
- },
10
- {
11
- "title": "bbb",
12
- "data": "bb",
13
- "children": [
14
- {
15
- "title": "cc1",
16
- "data": "<div><img src=\"https://media.zine.la/booklet/81/a3/23/8b05c1faa4fb7ffe84727d99a2_1593030700_James-Turrell-2.jpg?imageMogr2/thumbnail/!400x520r/gravity/Center/crop/400x520\"/></div>"
17
- }
18
- ]
19
- }
20
- ]
21
- }
package/test/tt.js DELETED
@@ -1,16 +0,0 @@
1
- /**
2
- * t.js
3
- * @author: oldj
4
- * @homepage: https://oldj.net
5
- */
6
-
7
- const {epubGen} = require('../dist/index')
8
- const data = require('./data/1.json')
9
-
10
- ;(async () => {
11
- try {
12
- await epubGen({...data, tocAutoNumber: true}, 'test.epub')
13
- } catch (e) {
14
- console.error('Error 24: ' + e.message)
15
- }
16
- })()