wp-epub-gen 0.4.0 → 0.4.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/build/index.cjs +24 -18
- package/build/index.d.ts +10 -0
- package/build/index.js +23 -17
- package/package.json +10 -3
- package/vite.config.ts +8 -4
package/build/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
3
|
const os = require("os");
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const url = require("url");
|
|
@@ -316,13 +316,13 @@ const ALLOWED_XHTML11_TAGS = [
|
|
|
316
316
|
const ALLOWED_ATTRIBUTES_SET = new Set(ALLOWED_ATTRIBUTES);
|
|
317
317
|
const ALLOWED_XHTML11_TAGS_SET = new Set(ALLOWED_XHTML11_TAGS);
|
|
318
318
|
const SELF_CLOSING_TAGS = /* @__PURE__ */ new Set(["img", "br", "hr"]);
|
|
319
|
-
function initializeChapterInfo(content,
|
|
319
|
+
function initializeChapterInfo(content, index2, epubConfigs) {
|
|
320
320
|
const chapter = { ...content };
|
|
321
321
|
let { filename } = chapter;
|
|
322
322
|
if (!filename) {
|
|
323
323
|
let titleSlug = uslug(diacritics.remove(chapter.title || "no title"));
|
|
324
324
|
titleSlug = titleSlug.replace(/[/\\]/g, "_");
|
|
325
|
-
chapter.href = `${
|
|
325
|
+
chapter.href = `${index2}_${titleSlug}.xhtml`;
|
|
326
326
|
chapter.filePath = path.join(epubConfigs.dir, "OEBPS", chapter.href);
|
|
327
327
|
} else {
|
|
328
328
|
filename = safeFineName(filename);
|
|
@@ -334,7 +334,7 @@ function initializeChapterInfo(content, index, epubConfigs) {
|
|
|
334
334
|
chapter.filePath = path.join(epubConfigs.dir, "OEBPS", `${filename}.xhtml`);
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
|
-
chapter.id = `item_${
|
|
337
|
+
chapter.id = `item_${index2}`;
|
|
338
338
|
chapter.dir = path.dirname(chapter.filePath);
|
|
339
339
|
chapter.excludeFromToc = chapter.excludeFromToc || false;
|
|
340
340
|
chapter.beforeToc = chapter.beforeToc || false;
|
|
@@ -387,7 +387,7 @@ function loadAndProcessHtml(data) {
|
|
|
387
387
|
);
|
|
388
388
|
}
|
|
389
389
|
}
|
|
390
|
-
function processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfigs,
|
|
390
|
+
function processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfigs, index2) {
|
|
391
391
|
const allowedAttrsSet = ALLOWED_ATTRIBUTES_SET;
|
|
392
392
|
const allowedTagsSet = ALLOWED_XHTML11_TAGS_SET;
|
|
393
393
|
const selfClosingTags = SELF_CLOSING_TAGS;
|
|
@@ -417,7 +417,7 @@ function processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfi
|
|
|
417
417
|
if (!allowedTagsSet.has(tagName)) {
|
|
418
418
|
if (epubConfigs.verbose) {
|
|
419
419
|
logger.warn(
|
|
420
|
-
`Warning (content[${
|
|
420
|
+
`Warning (content[${index2}]): ${tagName} tag isn't allowed on EPUB 2/XHTML 1.1 DTD.`
|
|
421
421
|
);
|
|
422
422
|
}
|
|
423
423
|
const child = $elem.html();
|
|
@@ -427,10 +427,10 @@ function processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfi
|
|
|
427
427
|
});
|
|
428
428
|
}
|
|
429
429
|
function processImages($, chapter, epubConfigs) {
|
|
430
|
-
$("img").each((
|
|
430
|
+
$("img").each((index2, elem) => {
|
|
431
431
|
const url2 = $(elem).attr("src") || "";
|
|
432
432
|
if (!url2 || url2.trim().length === 0) {
|
|
433
|
-
logger.warn(`Image at index ${
|
|
433
|
+
logger.warn(`Image at index ${index2} in chapter has empty src attribute, removing element`);
|
|
434
434
|
$(elem).remove();
|
|
435
435
|
return;
|
|
436
436
|
}
|
|
@@ -542,41 +542,41 @@ function extractAndCleanHtmlContent($, originalData) {
|
|
|
542
542
|
"<$1$2/>"
|
|
543
543
|
);
|
|
544
544
|
}
|
|
545
|
-
function processChildrenChapters(chapter,
|
|
545
|
+
function processChildrenChapters(chapter, index2, epubConfigs) {
|
|
546
546
|
if (Array.isArray(chapter.children)) {
|
|
547
547
|
chapter.children = chapter.children.map(
|
|
548
|
-
(content, idx) => parseContent(content, `${
|
|
548
|
+
(content, idx) => parseContent(content, `${index2}_${idx}`, epubConfigs)
|
|
549
549
|
);
|
|
550
550
|
}
|
|
551
551
|
}
|
|
552
|
-
function parseContent(content,
|
|
552
|
+
function parseContent(content, index2, epubConfigs) {
|
|
553
553
|
if (!content) {
|
|
554
554
|
throw new Error("Content cannot be null or undefined");
|
|
555
555
|
}
|
|
556
556
|
if (!content.data) {
|
|
557
|
-
logger.warn(`Chapter at index ${
|
|
557
|
+
logger.warn(`Chapter at index ${index2} has no data, using empty string`);
|
|
558
558
|
content.data = "";
|
|
559
559
|
}
|
|
560
|
-
const chapter = initializeChapterInfo(content,
|
|
560
|
+
const chapter = initializeChapterInfo(content, index2, epubConfigs);
|
|
561
561
|
normalizeAuthorInfo(chapter);
|
|
562
562
|
const allowedAttributes = getAllowedAttributes();
|
|
563
563
|
const allowedXhtml11Tags = getAllowedXhtml11Tags();
|
|
564
564
|
if (!chapter.data || chapter.data.trim().length === 0) {
|
|
565
|
-
logger.warn(`Chapter at index ${
|
|
565
|
+
logger.warn(`Chapter at index ${index2} has empty data, setting empty content`);
|
|
566
566
|
chapter.data = "";
|
|
567
567
|
} else {
|
|
568
568
|
let $;
|
|
569
569
|
try {
|
|
570
570
|
$ = loadAndProcessHtml(chapter.data);
|
|
571
571
|
} catch (error) {
|
|
572
|
-
logger.error(`Failed to process HTML for chapter ${
|
|
572
|
+
logger.error(`Failed to process HTML for chapter ${index2}: ${error}`);
|
|
573
573
|
$ = cheerio__namespace.load(`<div>${chapter.data}</div>`);
|
|
574
574
|
}
|
|
575
|
-
processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfigs,
|
|
575
|
+
processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfigs, index2);
|
|
576
576
|
processImages($, chapter, epubConfigs);
|
|
577
577
|
chapter.data = extractAndCleanHtmlContent($, content.data);
|
|
578
578
|
}
|
|
579
|
-
processChildrenChapters(chapter,
|
|
579
|
+
processChildrenChapters(chapter, index2, epubConfigs);
|
|
580
580
|
return chapter;
|
|
581
581
|
}
|
|
582
582
|
util.promisify(fs.readFile);
|
|
@@ -1234,7 +1234,7 @@ function parseOptions(options) {
|
|
|
1234
1234
|
if (typeof data.author === "string") {
|
|
1235
1235
|
data.author = [data.author];
|
|
1236
1236
|
}
|
|
1237
|
-
data.content = options.content.map((content,
|
|
1237
|
+
data.content = options.content.map((content, index2) => parseContent(content, index2, data));
|
|
1238
1238
|
if (data.cover) {
|
|
1239
1239
|
data._coverMediaType = mime.getType(data.cover) || "";
|
|
1240
1240
|
data._coverExtension = mime.getExtension(data._coverMediaType) || "";
|
|
@@ -1274,6 +1274,12 @@ async function epubGen(options, configs) {
|
|
|
1274
1274
|
}
|
|
1275
1275
|
}
|
|
1276
1276
|
const gen = epubGen;
|
|
1277
|
+
const index = {
|
|
1278
|
+
epubGen,
|
|
1279
|
+
gen,
|
|
1280
|
+
errors
|
|
1281
|
+
};
|
|
1282
|
+
exports.default = index;
|
|
1277
1283
|
exports.epubGen = epubGen;
|
|
1278
1284
|
exports.errors = errors;
|
|
1279
1285
|
exports.gen = gen;
|
package/build/index.d.ts
CHANGED
|
@@ -2,4 +2,14 @@ import { IEpubGenOptions, IGenConfigs, IOut } from './types';
|
|
|
2
2
|
export declare function epubGen(options: IEpubGenOptions, configs?: IGenConfigs): Promise<IOut>;
|
|
3
3
|
export declare const gen: typeof epubGen;
|
|
4
4
|
export { errors } from './errors';
|
|
5
|
+
declare const _default: {
|
|
6
|
+
epubGen: typeof epubGen;
|
|
7
|
+
gen: typeof epubGen;
|
|
8
|
+
errors: {
|
|
9
|
+
no_output_path: string;
|
|
10
|
+
no_title: string;
|
|
11
|
+
no_content: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export default _default;
|
|
5
15
|
export type { IChapter, IChapterData, IEpubData, IEpubGenOptions, IEpubImage, IOut } from './types';
|
package/build/index.js
CHANGED
|
@@ -295,13 +295,13 @@ const ALLOWED_XHTML11_TAGS = [
|
|
|
295
295
|
const ALLOWED_ATTRIBUTES_SET = new Set(ALLOWED_ATTRIBUTES);
|
|
296
296
|
const ALLOWED_XHTML11_TAGS_SET = new Set(ALLOWED_XHTML11_TAGS);
|
|
297
297
|
const SELF_CLOSING_TAGS = /* @__PURE__ */ new Set(["img", "br", "hr"]);
|
|
298
|
-
function initializeChapterInfo(content,
|
|
298
|
+
function initializeChapterInfo(content, index2, epubConfigs) {
|
|
299
299
|
const chapter = { ...content };
|
|
300
300
|
let { filename } = chapter;
|
|
301
301
|
if (!filename) {
|
|
302
302
|
let titleSlug = uslug(remove(chapter.title || "no title"));
|
|
303
303
|
titleSlug = titleSlug.replace(/[/\\]/g, "_");
|
|
304
|
-
chapter.href = `${
|
|
304
|
+
chapter.href = `${index2}_${titleSlug}.xhtml`;
|
|
305
305
|
chapter.filePath = path.join(epubConfigs.dir, "OEBPS", chapter.href);
|
|
306
306
|
} else {
|
|
307
307
|
filename = safeFineName(filename);
|
|
@@ -313,7 +313,7 @@ function initializeChapterInfo(content, index, epubConfigs) {
|
|
|
313
313
|
chapter.filePath = path.join(epubConfigs.dir, "OEBPS", `${filename}.xhtml`);
|
|
314
314
|
}
|
|
315
315
|
}
|
|
316
|
-
chapter.id = `item_${
|
|
316
|
+
chapter.id = `item_${index2}`;
|
|
317
317
|
chapter.dir = path.dirname(chapter.filePath);
|
|
318
318
|
chapter.excludeFromToc = chapter.excludeFromToc || false;
|
|
319
319
|
chapter.beforeToc = chapter.beforeToc || false;
|
|
@@ -366,7 +366,7 @@ function loadAndProcessHtml(data) {
|
|
|
366
366
|
);
|
|
367
367
|
}
|
|
368
368
|
}
|
|
369
|
-
function processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfigs,
|
|
369
|
+
function processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfigs, index2) {
|
|
370
370
|
const allowedAttrsSet = ALLOWED_ATTRIBUTES_SET;
|
|
371
371
|
const allowedTagsSet = ALLOWED_XHTML11_TAGS_SET;
|
|
372
372
|
const selfClosingTags = SELF_CLOSING_TAGS;
|
|
@@ -396,7 +396,7 @@ function processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfi
|
|
|
396
396
|
if (!allowedTagsSet.has(tagName)) {
|
|
397
397
|
if (epubConfigs.verbose) {
|
|
398
398
|
logger.warn(
|
|
399
|
-
`Warning (content[${
|
|
399
|
+
`Warning (content[${index2}]): ${tagName} tag isn't allowed on EPUB 2/XHTML 1.1 DTD.`
|
|
400
400
|
);
|
|
401
401
|
}
|
|
402
402
|
const child = $elem.html();
|
|
@@ -406,10 +406,10 @@ function processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfi
|
|
|
406
406
|
});
|
|
407
407
|
}
|
|
408
408
|
function processImages($, chapter, epubConfigs) {
|
|
409
|
-
$("img").each((
|
|
409
|
+
$("img").each((index2, elem) => {
|
|
410
410
|
const url = $(elem).attr("src") || "";
|
|
411
411
|
if (!url || url.trim().length === 0) {
|
|
412
|
-
logger.warn(`Image at index ${
|
|
412
|
+
logger.warn(`Image at index ${index2} in chapter has empty src attribute, removing element`);
|
|
413
413
|
$(elem).remove();
|
|
414
414
|
return;
|
|
415
415
|
}
|
|
@@ -521,41 +521,41 @@ function extractAndCleanHtmlContent($, originalData) {
|
|
|
521
521
|
"<$1$2/>"
|
|
522
522
|
);
|
|
523
523
|
}
|
|
524
|
-
function processChildrenChapters(chapter,
|
|
524
|
+
function processChildrenChapters(chapter, index2, epubConfigs) {
|
|
525
525
|
if (Array.isArray(chapter.children)) {
|
|
526
526
|
chapter.children = chapter.children.map(
|
|
527
|
-
(content, idx) => parseContent(content, `${
|
|
527
|
+
(content, idx) => parseContent(content, `${index2}_${idx}`, epubConfigs)
|
|
528
528
|
);
|
|
529
529
|
}
|
|
530
530
|
}
|
|
531
|
-
function parseContent(content,
|
|
531
|
+
function parseContent(content, index2, epubConfigs) {
|
|
532
532
|
if (!content) {
|
|
533
533
|
throw new Error("Content cannot be null or undefined");
|
|
534
534
|
}
|
|
535
535
|
if (!content.data) {
|
|
536
|
-
logger.warn(`Chapter at index ${
|
|
536
|
+
logger.warn(`Chapter at index ${index2} has no data, using empty string`);
|
|
537
537
|
content.data = "";
|
|
538
538
|
}
|
|
539
|
-
const chapter = initializeChapterInfo(content,
|
|
539
|
+
const chapter = initializeChapterInfo(content, index2, epubConfigs);
|
|
540
540
|
normalizeAuthorInfo(chapter);
|
|
541
541
|
const allowedAttributes = getAllowedAttributes();
|
|
542
542
|
const allowedXhtml11Tags = getAllowedXhtml11Tags();
|
|
543
543
|
if (!chapter.data || chapter.data.trim().length === 0) {
|
|
544
|
-
logger.warn(`Chapter at index ${
|
|
544
|
+
logger.warn(`Chapter at index ${index2} has empty data, setting empty content`);
|
|
545
545
|
chapter.data = "";
|
|
546
546
|
} else {
|
|
547
547
|
let $;
|
|
548
548
|
try {
|
|
549
549
|
$ = loadAndProcessHtml(chapter.data);
|
|
550
550
|
} catch (error) {
|
|
551
|
-
logger.error(`Failed to process HTML for chapter ${
|
|
551
|
+
logger.error(`Failed to process HTML for chapter ${index2}: ${error}`);
|
|
552
552
|
$ = cheerio.load(`<div>${chapter.data}</div>`);
|
|
553
553
|
}
|
|
554
|
-
processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfigs,
|
|
554
|
+
processHtmlElements($, allowedAttributes, allowedXhtml11Tags, epubConfigs, index2);
|
|
555
555
|
processImages($, chapter, epubConfigs);
|
|
556
556
|
chapter.data = extractAndCleanHtmlContent($, content.data);
|
|
557
557
|
}
|
|
558
|
-
processChildrenChapters(chapter,
|
|
558
|
+
processChildrenChapters(chapter, index2, epubConfigs);
|
|
559
559
|
return chapter;
|
|
560
560
|
}
|
|
561
561
|
promisify(fs.readFile);
|
|
@@ -1213,7 +1213,7 @@ function parseOptions(options) {
|
|
|
1213
1213
|
if (typeof data.author === "string") {
|
|
1214
1214
|
data.author = [data.author];
|
|
1215
1215
|
}
|
|
1216
|
-
data.content = options.content.map((content,
|
|
1216
|
+
data.content = options.content.map((content, index2) => parseContent(content, index2, data));
|
|
1217
1217
|
if (data.cover) {
|
|
1218
1218
|
data._coverMediaType = mime.getType(data.cover) || "";
|
|
1219
1219
|
data._coverExtension = mime.getExtension(data._coverMediaType) || "";
|
|
@@ -1253,7 +1253,13 @@ async function epubGen(options, configs) {
|
|
|
1253
1253
|
}
|
|
1254
1254
|
}
|
|
1255
1255
|
const gen = epubGen;
|
|
1256
|
+
const index = {
|
|
1257
|
+
epubGen,
|
|
1258
|
+
gen,
|
|
1259
|
+
errors
|
|
1260
|
+
};
|
|
1256
1261
|
export {
|
|
1262
|
+
index as default,
|
|
1257
1263
|
epubGen,
|
|
1258
1264
|
errors,
|
|
1259
1265
|
gen
|
package/package.json
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wp-epub-gen",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Epub generator.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "build/index.
|
|
7
|
-
"module": "build/index.
|
|
6
|
+
"main": "build/index.cjs",
|
|
7
|
+
"module": "build/index.js",
|
|
8
8
|
"types": "build/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./build/index.d.ts",
|
|
12
|
+
"import": "./build/index.js",
|
|
13
|
+
"require": "./build/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
9
16
|
"scripts": {
|
|
10
17
|
"tsc": "tsc",
|
|
11
18
|
"test": "vitest --watch=false run",
|
package/vite.config.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from 'fs'
|
|
1
2
|
import * as path from 'path'
|
|
2
3
|
import { defineConfig } from 'vite'
|
|
3
4
|
import dts from 'vite-plugin-dts'
|
|
4
5
|
import tsconfigPaths from 'vite-tsconfig-paths'
|
|
5
|
-
import { readFileSync, writeFileSync, existsSync, readdirSync, statSync } from 'fs'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* 生成模板文件的 TypeScript 代码
|
|
@@ -76,15 +76,19 @@ export default defineConfig({
|
|
|
76
76
|
base: './',
|
|
77
77
|
build: {
|
|
78
78
|
rollupOptions: {
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
output: {
|
|
80
|
+
exports: 'named',
|
|
81
81
|
},
|
|
82
82
|
},
|
|
83
83
|
lib: {
|
|
84
84
|
entry: path.join(__dirname, 'src', 'index.ts'),
|
|
85
85
|
name: 'index',
|
|
86
86
|
formats: ['cjs', 'es'],
|
|
87
|
-
fileName: (format) =>
|
|
87
|
+
// fileName: (format) => {
|
|
88
|
+
// if (format === 'es') return 'index.mjs'
|
|
89
|
+
// if (format === 'cjs') return 'index.js'
|
|
90
|
+
// return `index.${format}`
|
|
91
|
+
// },
|
|
88
92
|
},
|
|
89
93
|
outDir: path.join(__dirname, 'build'),
|
|
90
94
|
minify: false,
|