epub-optimizer 0.0.0-stage → 3.16.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/LICENSE +21 -0
- package/README.md +1434 -2
- package/dist/src/batch.d.ts +34 -0
- package/dist/src/batch.js +137 -0
- package/dist/src/cli.d.ts +7 -0
- package/dist/src/cli.js +275 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.js +24 -0
- package/dist/src/optimization.d.ts +7 -0
- package/dist/src/optimization.js +207 -0
- package/dist/src/pipeline.d.ts +2 -0
- package/dist/src/pipeline.js +48 -0
- package/dist/src/processors/archive-processor.d.ts +26 -0
- package/dist/src/processors/archive-processor.js +170 -0
- package/dist/src/processors/epub-processor.d.ts +17 -0
- package/dist/src/processors/epub-processor.js +133 -0
- package/dist/src/processors/font-characters.d.ts +2 -0
- package/dist/src/processors/font-characters.js +227 -0
- package/dist/src/processors/font-engine.d.ts +6 -0
- package/dist/src/processors/font-engine.js +101 -0
- package/dist/src/processors/font-limits.d.ts +3 -0
- package/dist/src/processors/font-limits.js +3 -0
- package/dist/src/processors/font-processor.d.ts +4 -0
- package/dist/src/processors/font-processor.js +121 -0
- package/dist/src/processors/font-runner.d.ts +8 -0
- package/dist/src/processors/font-runner.js +94 -0
- package/dist/src/processors/font-worker.d.ts +1 -0
- package/dist/src/processors/font-worker.js +12 -0
- package/dist/src/processors/html-processor.d.ts +22 -0
- package/dist/src/processors/html-processor.js +190 -0
- package/dist/src/processors/image-converter.d.ts +14 -0
- package/dist/src/processors/image-converter.js +481 -0
- package/dist/src/processors/image-processor.d.ts +24 -0
- package/dist/src/processors/image-processor.js +165 -0
- package/dist/src/processors/js-processor.d.ts +7 -0
- package/dist/src/processors/js-processor.js +59 -0
- package/dist/src/processors/lazy-img.d.ts +5 -0
- package/dist/src/processors/lazy-img.js +48 -0
- package/dist/src/processors/svg-optimizer.d.ts +5 -0
- package/dist/src/processors/svg-optimizer.js +49 -0
- package/dist/src/scripts/create-epub.d.ts +2 -0
- package/dist/src/scripts/create-epub.js +29 -0
- package/dist/src/scripts/fix/fix-span-tags.d.ts +2 -0
- package/dist/src/scripts/fix/fix-span-tags.js +36 -0
- package/dist/src/scripts/fix/fix-xml.d.ts +3 -0
- package/dist/src/scripts/fix/fix-xml.js +59 -0
- package/dist/src/scripts/fix/index.d.ts +5 -0
- package/dist/src/scripts/fix/index.js +20 -0
- package/dist/src/scripts/fix/remove-empty-styles.d.ts +10 -0
- package/dist/src/scripts/fix/remove-empty-styles.js +53 -0
- package/dist/src/scripts/fix/xhtml-files.d.ts +2 -0
- package/dist/src/scripts/fix/xhtml-files.js +34 -0
- package/dist/src/scripts/ops/add-chapter-sections-to-toc.d.ts +5 -0
- package/dist/src/scripts/ops/add-chapter-sections-to-toc.js +242 -0
- package/dist/src/scripts/ops/add-cover-image-property.d.ts +2 -0
- package/dist/src/scripts/ops/add-cover-image-property.js +27 -0
- package/dist/src/scripts/ops/ncx.d.ts +6 -0
- package/dist/src/scripts/ops/ncx.js +19 -0
- package/dist/src/scripts/ops/update-cover-linear.d.ts +11 -0
- package/dist/src/scripts/ops/update-cover-linear.js +45 -0
- package/dist/src/scripts/ops/update-structure.d.ts +5 -0
- package/dist/src/scripts/ops/update-structure.js +22 -0
- package/dist/src/scripts/ops/update-summary-page.d.ts +2 -0
- package/dist/src/scripts/ops/update-summary-page.js +53 -0
- package/dist/src/scripts/ops/update-toc-with-cover.d.ts +2 -0
- package/dist/src/scripts/ops/update-toc-with-cover.js +140 -0
- package/dist/src/scripts/utils.d.ts +53 -0
- package/dist/src/scripts/utils.js +92 -0
- package/dist/src/scripts/validate-epub.d.ts +12 -0
- package/dist/src/scripts/validate-epub.js +185 -0
- package/dist/src/storage.d.ts +30 -0
- package/dist/src/storage.js +286 -0
- package/dist/src/types.d.ts +51 -0
- package/dist/src/types.js +1 -0
- package/dist/src/utils/author-workflow-config.d.ts +17 -0
- package/dist/src/utils/author-workflow-config.js +95 -0
- package/dist/src/utils/buffered-process.d.ts +8 -0
- package/dist/src/utils/buffered-process.js +36 -0
- package/dist/src/utils/cli-config.d.ts +4 -0
- package/dist/src/utils/cli-config.js +84 -0
- package/dist/src/utils/config.d.ts +49 -0
- package/dist/src/utils/config.js +58 -0
- package/dist/src/utils/diagnostics.d.ts +11 -0
- package/dist/src/utils/diagnostics.js +38 -0
- package/dist/src/utils/epub-doctor.d.ts +78 -0
- package/dist/src/utils/epub-doctor.js +301 -0
- package/dist/src/utils/epub-features.d.ts +20 -0
- package/dist/src/utils/epub-features.js +105 -0
- package/dist/src/utils/epub-integrity.d.ts +25 -0
- package/dist/src/utils/epub-integrity.js +197 -0
- package/dist/src/utils/epub-text.d.ts +13 -0
- package/dist/src/utils/epub-text.js +106 -0
- package/dist/src/utils/epub-utils.d.ts +79 -0
- package/dist/src/utils/epub-utils.js +207 -0
- package/dist/src/utils/epubcheck-report.d.ts +35 -0
- package/dist/src/utils/epubcheck-report.js +68 -0
- package/dist/src/utils/file-size.d.ts +13 -0
- package/dist/src/utils/file-size.js +40 -0
- package/dist/src/utils/files.d.ts +14 -0
- package/dist/src/utils/files.js +39 -0
- package/dist/src/utils/html-report.d.ts +17 -0
- package/dist/src/utils/html-report.js +95 -0
- package/dist/src/utils/i18n.d.ts +5 -0
- package/dist/src/utils/i18n.js +9 -0
- package/dist/src/utils/output-transaction.d.ts +8 -0
- package/dist/src/utils/output-transaction.js +146 -0
- package/dist/src/utils/package-root.d.ts +2 -0
- package/dist/src/utils/package-root.js +15 -0
- package/dist/src/utils/path-safety.d.ts +2 -0
- package/dist/src/utils/path-safety.js +30 -0
- package/dist/src/utils/pipeline-options.d.ts +34 -0
- package/dist/src/utils/pipeline-options.js +49 -0
- package/dist/src/utils/png-animation.d.ts +7 -0
- package/dist/src/utils/png-animation.js +44 -0
- package/dist/src/utils/run-report.d.ts +49 -0
- package/dist/src/utils/run-report.js +71 -0
- package/dist/src/utils/temp-dir.d.ts +11 -0
- package/dist/src/utils/temp-dir.js +80 -0
- package/dist/src/utils/watch-lock.d.ts +8 -0
- package/dist/src/utils/watch-lock.js +100 -0
- package/dist/src/utils/watch-summary.d.ts +13 -0
- package/dist/src/utils/watch-summary.js +130 -0
- package/dist/src/utils/xhtml-structure.d.ts +7 -0
- package/dist/src/utils/xhtml-structure.js +53 -0
- package/dist/src/utils/xhtml.d.ts +27 -0
- package/dist/src/utils/xhtml.js +205 -0
- package/dist/src/utils/xml-dtd.d.ts +6 -0
- package/dist/src/utils/xml-dtd.js +133 -0
- package/dist/src/watch.d.ts +21 -0
- package/dist/src/watch.js +328 -0
- package/package.json +99 -3
- package/scripts/install-epubcheck.sh +44 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface ImageOpts {
|
|
2
|
+
/** JPEG quality 0-100 */
|
|
3
|
+
jpegQuality?: number;
|
|
4
|
+
/** PNG quality 0-1 scale */
|
|
5
|
+
pngQuality?: number;
|
|
6
|
+
/** Max concurrent image encodes. Default 8 — libvips is thread-safe. */
|
|
7
|
+
concurrency?: number;
|
|
8
|
+
/** Max width/height in px; larger images are shrunk. Skip when unset. */
|
|
9
|
+
maxDim?: number;
|
|
10
|
+
/** Absolute paths that should be skipped (e.g. freshly converted elsewhere). */
|
|
11
|
+
skip?: ReadonlySet<string>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Optimize images in a directory recursively, in parallel.
|
|
15
|
+
* Combines resize (if `maxDim` is set) and re-encode into a single sharp pass.
|
|
16
|
+
* @param opts Quality overrides; falls back to config defaults.
|
|
17
|
+
*/
|
|
18
|
+
declare function optimizeImages(dir: string, opts?: ImageOpts): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Compress a single image using Sharp.
|
|
21
|
+
* @param opts Quality overrides; falls back to config defaults.
|
|
22
|
+
*/
|
|
23
|
+
declare function compressImage(imagePath: string, opts?: ImageOpts): Promise<void>;
|
|
24
|
+
export { optimizeImages, compressImage };
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { reportError, reportInfo } from "../utils/diagnostics.js";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import sharp from "sharp";
|
|
5
|
+
import pLimit from "p-limit";
|
|
6
|
+
import config from "../utils/config.js";
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import { DEFAULT_FILE_CONCURRENCY } from "../utils/files.js";
|
|
9
|
+
import { isAnimatedPng } from "../utils/png-animation.js";
|
|
10
|
+
// Bound libvips so parallel encodes don't oversubscribe CPU threads or retain
|
|
11
|
+
// decoded-image memory. File-level parallelism is handled by p-limit below, so
|
|
12
|
+
// each sharp op runs single-threaded and the operation cache is disabled.
|
|
13
|
+
sharp.concurrency(1);
|
|
14
|
+
sharp.cache(false);
|
|
15
|
+
const IMAGE_EXT_RE = /\.(jpe?g|png|webp|gif|avif|svg)$/i;
|
|
16
|
+
async function collectImages(dir) {
|
|
17
|
+
const out = [];
|
|
18
|
+
const entries = await fs.readdir(dir);
|
|
19
|
+
await Promise.all(entries.map(async (entry) => {
|
|
20
|
+
const fullPath = path.join(dir, entry);
|
|
21
|
+
const stat = await fs.stat(fullPath);
|
|
22
|
+
if (stat.isDirectory()) {
|
|
23
|
+
out.push(...(await collectImages(fullPath)));
|
|
24
|
+
}
|
|
25
|
+
else if (IMAGE_EXT_RE.test(entry)) {
|
|
26
|
+
out.push(fullPath);
|
|
27
|
+
}
|
|
28
|
+
}));
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Optimize images in a directory recursively, in parallel.
|
|
33
|
+
* Combines resize (if `maxDim` is set) and re-encode into a single sharp pass.
|
|
34
|
+
* @param opts Quality overrides; falls back to config defaults.
|
|
35
|
+
*/
|
|
36
|
+
async function optimizeImages(dir, opts = {}) {
|
|
37
|
+
try {
|
|
38
|
+
const files = await collectImages(dir);
|
|
39
|
+
const skip = opts.skip;
|
|
40
|
+
const targets = skip ? files.filter((f) => !skip.has(f)) : files;
|
|
41
|
+
const limit = pLimit(opts.concurrency ?? DEFAULT_FILE_CONCURRENCY);
|
|
42
|
+
await Promise.all(targets.map((file) => limit(() => compressImage(file, opts))));
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
reportError(`Error processing directory ${dir}: ${error instanceof Error ? error.message : String(error)}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Compress a single image using Sharp.
|
|
50
|
+
* @param opts Quality overrides; falls back to config defaults.
|
|
51
|
+
*/
|
|
52
|
+
async function compressImage(imagePath, opts = {}) {
|
|
53
|
+
const filename = path.basename(imagePath);
|
|
54
|
+
const jpegQuality = opts.jpegQuality ?? config.jpegOptions.quality;
|
|
55
|
+
const pngQuality = opts.pngQuality ?? config.pngOptions.quality;
|
|
56
|
+
let tempPath;
|
|
57
|
+
try {
|
|
58
|
+
const extension = path.extname(imagePath).toLowerCase();
|
|
59
|
+
// Skip SVG files as they're already text/XML
|
|
60
|
+
if (extension === ".svg") {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// Read the size cheaply first and skip tiny images without loading them.
|
|
64
|
+
const originalSize = (await fs.stat(imagePath)).size;
|
|
65
|
+
if (originalSize < 10 * 1024) {
|
|
66
|
+
reportInfo(`⏩ Skipping small image: ${filename}`);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// Sharp's PNG decoder exposes only the default image of an APNG. Keep its
|
|
70
|
+
// original bytes, including animation control, timing and disposal modes.
|
|
71
|
+
if (extension === ".png" && (await isAnimatedPng(imagePath))) {
|
|
72
|
+
reportInfo(`Keeping animated PNG unchanged: ${filename}`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// Preserve every frame of multi-frame formats instead of collapsing to the
|
|
76
|
+
// first one. `animated` is a harmless no-op for single-frame images.
|
|
77
|
+
const isMultiFrame = extension === ".gif" || extension === ".webp" || extension === ".avif";
|
|
78
|
+
// Read straight from the path (sharp streams it) instead of buffering the
|
|
79
|
+
// whole compressed file in memory on top of the decoded raster.
|
|
80
|
+
let processedImage = sharp(imagePath, isMultiFrame ? { animated: true } : undefined);
|
|
81
|
+
const isJpeg = extension === ".jpg" || extension === ".jpeg";
|
|
82
|
+
// JPEG encoding strips EXIF. Apply its orientation to the pixels first so
|
|
83
|
+
// the displayed image stays unchanged, including mirrored orientations.
|
|
84
|
+
if (isJpeg)
|
|
85
|
+
processedImage = processedImage.autoOrient();
|
|
86
|
+
// Resize step (merged from the old image-downscale pass). Sharp with
|
|
87
|
+
// `fit: inside, withoutEnlargement: true` is a no-op for already-small
|
|
88
|
+
// images, so we can apply it unconditionally — saves a separate pass.
|
|
89
|
+
if (opts.maxDim && extension !== ".gif") {
|
|
90
|
+
processedImage = processedImage.resize({
|
|
91
|
+
width: opts.maxDim,
|
|
92
|
+
height: opts.maxDim,
|
|
93
|
+
fit: "inside",
|
|
94
|
+
withoutEnlargement: true,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
// Convert PNG quality from 0-1 scale to 0-100 scale for sharp
|
|
98
|
+
const pngQualityValue = Math.round(pngQuality * 100);
|
|
99
|
+
// Configure compression based on file type
|
|
100
|
+
switch (extension) {
|
|
101
|
+
case ".jpg":
|
|
102
|
+
case ".jpeg":
|
|
103
|
+
processedImage = processedImage.jpeg({
|
|
104
|
+
quality: jpegQuality, // Use jpegQuality from CLI args or config
|
|
105
|
+
mozjpeg: true,
|
|
106
|
+
});
|
|
107
|
+
break;
|
|
108
|
+
case ".png":
|
|
109
|
+
processedImage = processedImage.png({
|
|
110
|
+
quality: pngQualityValue,
|
|
111
|
+
compressionLevel: 9,
|
|
112
|
+
palette: true,
|
|
113
|
+
});
|
|
114
|
+
break;
|
|
115
|
+
case ".webp":
|
|
116
|
+
processedImage = processedImage.webp({
|
|
117
|
+
quality: jpegQuality, // Use jpegQuality from CLI args or config
|
|
118
|
+
lossless: false,
|
|
119
|
+
});
|
|
120
|
+
break;
|
|
121
|
+
case ".gif":
|
|
122
|
+
processedImage = processedImage.gif();
|
|
123
|
+
break;
|
|
124
|
+
case ".avif":
|
|
125
|
+
processedImage = processedImage.avif({
|
|
126
|
+
quality: jpegQuality, // Use jpegQuality from CLI args or config
|
|
127
|
+
});
|
|
128
|
+
break;
|
|
129
|
+
default:
|
|
130
|
+
// Unknown format, skip processing
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
// Write optimized image back to the same path
|
|
134
|
+
tempPath = `${imagePath}.${randomUUID()}.tmp`;
|
|
135
|
+
const outputInfo = await processedImage.toFile(tempPath);
|
|
136
|
+
const newSize = outputInfo.size;
|
|
137
|
+
let wasResized = false;
|
|
138
|
+
if (opts.maxDim && extension !== ".gif") {
|
|
139
|
+
const metadata = await sharp(imagePath, isMultiFrame ? { animated: true } : undefined).metadata();
|
|
140
|
+
const dimensions = isJpeg
|
|
141
|
+
? metadata.autoOrient
|
|
142
|
+
: { width: metadata.width, height: metadata.pageHeight ?? metadata.height };
|
|
143
|
+
const outputHeight = outputInfo.pageHeight ?? outputInfo.height;
|
|
144
|
+
wasResized = dimensions.width > outputInfo.width || dimensions.height > outputHeight;
|
|
145
|
+
}
|
|
146
|
+
if (newSize >= originalSize && !wasResized) {
|
|
147
|
+
reportInfo(`Keeping original ${filename}: recompression would not reduce size`);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
await fs.rename(tempPath, imagePath);
|
|
151
|
+
// Log optimization result
|
|
152
|
+
const savings = (((originalSize - newSize) / originalSize) * 100).toFixed(1);
|
|
153
|
+
if (newSize < originalSize) {
|
|
154
|
+
reportInfo(`💾 Optimized ${filename}: ${savings}% smaller`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
reportError(`⚠️ Error processing ${filename}: ${error instanceof Error ? error.message : String(error)}`);
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
if (tempPath)
|
|
162
|
+
await fs.remove(tempPath);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
export { optimizeImages, compressImage };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { reportInfo, reportWarning } from "../utils/diagnostics.js";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { minify as terserMinify } from "terser";
|
|
5
|
+
import { collectFiles, forEachFileLimited, hasExtension } from "../utils/files.js";
|
|
6
|
+
const JS_EXTENSIONS = new Set([".js"]);
|
|
7
|
+
/**
|
|
8
|
+
* Minify JavaScript files in the EPUB
|
|
9
|
+
* @param dir Directory containing JS files
|
|
10
|
+
* @throws Error if minification fails
|
|
11
|
+
*/
|
|
12
|
+
async function minifyJavaScript(dir) {
|
|
13
|
+
try {
|
|
14
|
+
reportInfo("Minifying JavaScript files...");
|
|
15
|
+
const jsFiles = await collectFiles(dir, (filePath) => hasExtension(filePath, JS_EXTENSIONS));
|
|
16
|
+
if (jsFiles.length === 0) {
|
|
17
|
+
reportInfo("No JavaScript files found");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
reportInfo(`Found ${jsFiles.length} JavaScript files to minify`);
|
|
21
|
+
await forEachFileLimited(jsFiles, async (jsFile) => {
|
|
22
|
+
try {
|
|
23
|
+
// Read original file
|
|
24
|
+
const content = await fs.readFile(jsFile, "utf8");
|
|
25
|
+
const originalSize = Buffer.from(content).length;
|
|
26
|
+
// Skip empty files
|
|
27
|
+
if (content.trim() === "") {
|
|
28
|
+
reportInfo(`Skipping empty file: ${path.basename(jsFile)}`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// Minify with Terser
|
|
32
|
+
const result = await terserMinify(content, {
|
|
33
|
+
compress: true,
|
|
34
|
+
mangle: true,
|
|
35
|
+
output: {
|
|
36
|
+
comments: false,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
if (result.code) {
|
|
40
|
+
// Write minified file
|
|
41
|
+
await fs.writeFile(jsFile, result.code);
|
|
42
|
+
const newSize = Buffer.from(result.code).length;
|
|
43
|
+
const reduction = Math.round(((originalSize - newSize) / originalSize) * 100);
|
|
44
|
+
reportInfo(`Minified ${path.basename(jsFile)}: ${originalSize} bytes → ${newSize} bytes (${reduction}% smaller)`);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
reportInfo(`Skipping ${path.basename(jsFile)}: No output from minifier`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
reportWarning(`Skipping ${path.basename(jsFile)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
throw new Error(`Failed to minify JavaScript: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export { minifyJavaScript };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { reportError, reportInfo, reportWarning } from "../utils/diagnostics.js";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { getContentPath } from "../utils/epub-utils.js";
|
|
5
|
+
import { collectFiles, hasExtension } from "../utils/files.js";
|
|
6
|
+
const XHTML_EXTENSIONS = new Set([".xhtml"]);
|
|
7
|
+
/**
|
|
8
|
+
* Add loading="lazy" to all <img> tags in XHTML files
|
|
9
|
+
* @param epubDir Directory containing the extracted EPUB
|
|
10
|
+
*/
|
|
11
|
+
export async function addLazyLoadingToImages(epubDir) {
|
|
12
|
+
try {
|
|
13
|
+
const contentDir = await getContentPath(epubDir);
|
|
14
|
+
const xhtmlFiles = await collectFiles(contentDir, (filePath) => hasExtension(filePath, XHTML_EXTENSIONS));
|
|
15
|
+
if (xhtmlFiles.length === 0) {
|
|
16
|
+
reportInfo("No XHTML files found for lazy loading");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
for (const file of xhtmlFiles) {
|
|
20
|
+
try {
|
|
21
|
+
let content = await fs.readFile(file, "utf8");
|
|
22
|
+
let changed = false;
|
|
23
|
+
// Use regex to add loading="lazy" to <img> tags without it
|
|
24
|
+
// Match quoted values as a whole: `>` inside an attribute is not the
|
|
25
|
+
// tag boundary. Keep the original quoting and self-closing syntax.
|
|
26
|
+
content = content.replace(/<img\s+((?:"[^"]*"|'[^']*'|[^'">])*?)(\s*\/?>)/gi, (match, attrs, closing) => {
|
|
27
|
+
// Check if loading attribute already exists in the matched attributes
|
|
28
|
+
if (/loading\s*=\s*["'][^"']*["']/i.test(match)) {
|
|
29
|
+
return match; // Already has loading attribute
|
|
30
|
+
}
|
|
31
|
+
changed = true;
|
|
32
|
+
// Add loading="lazy" before the closing tag
|
|
33
|
+
return `<img ${attrs} loading="lazy"${closing}`;
|
|
34
|
+
});
|
|
35
|
+
if (changed) {
|
|
36
|
+
await fs.writeFile(file, content);
|
|
37
|
+
reportInfo(`Added loading="lazy" to images in ${path.basename(file)}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
reportWarning(`Failed to add lazy loading to ${path.basename(file)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
reportError(`Lazy loading image update failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { reportError, reportInfo, reportWarning } from "../utils/diagnostics.js";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { optimize as svgoOptimize } from "svgo";
|
|
5
|
+
import { collectManifestResources } from "../utils/epub-utils.js";
|
|
6
|
+
import { forEachFileLimited } from "../utils/files.js";
|
|
7
|
+
const SVG_MEDIA_TYPES = new Set(["image/svg+xml"]);
|
|
8
|
+
const SVG_EXTENSIONS = new Set([".svg"]);
|
|
9
|
+
/**
|
|
10
|
+
* Optimize SVG files in the EPUB images directory
|
|
11
|
+
* @param epubDir Directory containing the extracted EPUB
|
|
12
|
+
*/
|
|
13
|
+
export async function optimizeSVGs(epubDir) {
|
|
14
|
+
try {
|
|
15
|
+
// Discover SVGs via the OPF manifest (any folder/case), not a hardcoded
|
|
16
|
+
// `images/` folder — Sigil/EDRLab use capitalized `Images/`, etc.
|
|
17
|
+
const svgFiles = await collectManifestResources(epubDir, {
|
|
18
|
+
mediaTypes: SVG_MEDIA_TYPES,
|
|
19
|
+
extensions: SVG_EXTENSIONS,
|
|
20
|
+
});
|
|
21
|
+
if (svgFiles.length === 0) {
|
|
22
|
+
reportInfo("No SVG files found");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
reportInfo(`Optimizing ${svgFiles.length} SVG files...`);
|
|
26
|
+
await forEachFileLimited(svgFiles, async (svgFile) => {
|
|
27
|
+
try {
|
|
28
|
+
const original = await fs.readFile(svgFile, "utf8");
|
|
29
|
+
const result = svgoOptimize(original, {
|
|
30
|
+
multipass: true,
|
|
31
|
+
plugins: [{ name: "preset-default", params: { overrides: { cleanupIds: false } } }],
|
|
32
|
+
});
|
|
33
|
+
if (result.data && result.data.length < original.length) {
|
|
34
|
+
await fs.writeFile(svgFile, result.data);
|
|
35
|
+
reportInfo(`Optimized ${path.basename(svgFile)}: ${original.length} → ${result.data.length} bytes`);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
reportInfo(`No optimization for ${path.basename(svgFile)}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
reportWarning(`Failed to optimize ${path.basename(svgFile)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
reportError(`SVG optimization failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { reportError, reportInfo } from "../utils/diagnostics.js";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import config from "../utils/config.js";
|
|
5
|
+
import { parseArgs, getTempDir, isEntryPoint } from "./utils.js";
|
|
6
|
+
import { compressEPUB } from "../processors/archive-processor.js";
|
|
7
|
+
export async function run(opts = {}) {
|
|
8
|
+
const extractedDir = opts.tempDir ?? getTempDir();
|
|
9
|
+
let outputEpub = opts.output;
|
|
10
|
+
if (!outputEpub) {
|
|
11
|
+
const argv = parseArgs(true, true);
|
|
12
|
+
outputEpub = argv.output ?? config.outputEPUB;
|
|
13
|
+
}
|
|
14
|
+
const outputEpubPath = path.isAbsolute(outputEpub)
|
|
15
|
+
? outputEpub
|
|
16
|
+
: path.join(process.cwd(), outputEpub);
|
|
17
|
+
if (!fs.existsSync(extractedDir)) {
|
|
18
|
+
throw new Error(`Directory ${extractedDir} does not exist.`);
|
|
19
|
+
}
|
|
20
|
+
reportInfo("Creating new EPUB...");
|
|
21
|
+
await compressEPUB(outputEpubPath, extractedDir);
|
|
22
|
+
reportInfo(`Created optimized EPUB: ${outputEpub}`);
|
|
23
|
+
}
|
|
24
|
+
if (isEntryPoint(import.meta.url)) {
|
|
25
|
+
run().catch((err) => {
|
|
26
|
+
reportError(err);
|
|
27
|
+
process.exit(1);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { reportError, reportInfo } from "../../utils/diagnostics.js";
|
|
2
|
+
// This script fixes the span tags in the XHTML files after the book is built.
|
|
3
|
+
// There were some issues like <span> tags that were not closed.
|
|
4
|
+
import fs from "fs-extra";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import * as cheerio from "cheerio";
|
|
7
|
+
import { getTempDir, isEntryPoint } from "../utils.js";
|
|
8
|
+
import { normalizeNamedEntities } from "../../utils/xhtml.js";
|
|
9
|
+
import { collectRepairFiles } from "./xhtml-files.js";
|
|
10
|
+
async function fixFile(file) {
|
|
11
|
+
try {
|
|
12
|
+
reportInfo(`Processing ${file}`);
|
|
13
|
+
const content = await fs.readFile(file, "utf8");
|
|
14
|
+
const $ = cheerio.load(normalizeNamedEntities(content), { xml: { decodeEntities: false } });
|
|
15
|
+
// Cheerio auto-closes tags on serialization
|
|
16
|
+
await fs.writeFile(file, $.xml());
|
|
17
|
+
reportInfo(`Fixed ${path.basename(file)}`);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
reportError(`Error processing ${file}:`, error);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function run(opts = {}) {
|
|
24
|
+
const extractedDir = opts.tempDir ?? getTempDir();
|
|
25
|
+
const files = await collectRepairFiles(extractedDir);
|
|
26
|
+
for (const file of files) {
|
|
27
|
+
await fixFile(file);
|
|
28
|
+
}
|
|
29
|
+
reportInfo("All XHTML files fixed.");
|
|
30
|
+
}
|
|
31
|
+
if (isEntryPoint(import.meta.url)) {
|
|
32
|
+
run().catch((err) => {
|
|
33
|
+
reportError(err);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { reportError, reportInfo } from "../../utils/diagnostics.js";
|
|
2
|
+
// This script fixes the XML/XHTML files.
|
|
3
|
+
// It is used to fix the XML/XHTML files after the book is built.
|
|
4
|
+
// There were some issues like <br> tags that were not self-closed or invalid tags.
|
|
5
|
+
import fs from "fs-extra";
|
|
6
|
+
import * as cheerio from "cheerio";
|
|
7
|
+
import { getTempDir, isEntryPoint } from "../utils.js";
|
|
8
|
+
import { normalizeNamedEntities, normalizeVoidElements } from "../../utils/xhtml.js";
|
|
9
|
+
import { collectRepairFiles } from "./xhtml-files.js";
|
|
10
|
+
// Properly format self-closing tags in XML/XHTML files
|
|
11
|
+
export function fixXml(originalContent) {
|
|
12
|
+
// Self-close every HTML5 void element (link, meta, img, br, hr, input, …).
|
|
13
|
+
// Remove invalid </br> markup without touching strings in attributes/scripts.
|
|
14
|
+
// Named references become numeric so the XML round trip below keeps them.
|
|
15
|
+
let processedContent = normalizeNamedEntities(normalizeVoidElements(originalContent, { removeInvalidBrClosings: true }));
|
|
16
|
+
// Ensure XML declaration is immediately followed by <html>
|
|
17
|
+
processedContent = processedContent.replace(/(<\?xml[^>]+>)[\s\r\n]+<html/, "$1<html");
|
|
18
|
+
// Use cheerio for DOM manipulation
|
|
19
|
+
// Keep existing XML references and UTF-8 characters as written when serializing.
|
|
20
|
+
const $ = cheerio.load(processedContent, { xml: { decodeEntities: false } });
|
|
21
|
+
// Microdata <meta itemprop="..." content="..."/> is valid in body content.
|
|
22
|
+
// Other metadata still belongs directly in <head>.
|
|
23
|
+
$("meta").each((_, el) => {
|
|
24
|
+
const parent = $(el).parent();
|
|
25
|
+
if (!parent.is("head") && !$(el).is("[itemprop][content]")) {
|
|
26
|
+
$(el).remove();
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
// Remove any text nodes that are direct children of <html>
|
|
30
|
+
$("html")
|
|
31
|
+
.contents()
|
|
32
|
+
.filter((_, node) => node.type === "text")
|
|
33
|
+
.remove();
|
|
34
|
+
// Text directly inside body and scripts are valid EPUB content. Repairing
|
|
35
|
+
// XML structure must not delete them or change the scripted manifest flag.
|
|
36
|
+
// Serialize back to XML
|
|
37
|
+
return $.xml();
|
|
38
|
+
}
|
|
39
|
+
export async function run(opts = {}) {
|
|
40
|
+
const extractedDir = opts.tempDir ?? getTempDir();
|
|
41
|
+
const xhtmlFiles = await collectRepairFiles(extractedDir);
|
|
42
|
+
for (const file of xhtmlFiles) {
|
|
43
|
+
try {
|
|
44
|
+
reportInfo(`Processing ${file}`);
|
|
45
|
+
const content = fs.readFileSync(file, "utf8");
|
|
46
|
+
fs.writeFileSync(file, fixXml(content));
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
reportError(`Error processing ${file}:`, error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
reportInfo("All files processed.");
|
|
53
|
+
}
|
|
54
|
+
if (isEntryPoint(import.meta.url)) {
|
|
55
|
+
run().catch((err) => {
|
|
56
|
+
reportError(err);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { reportError, reportInfo } from "../../utils/diagnostics.js";
|
|
2
|
+
import { isEntryPoint } from "../utils.js";
|
|
3
|
+
import { run as fixSpanTags } from "./fix-span-tags.js";
|
|
4
|
+
import { run as fixXml } from "./fix-xml.js";
|
|
5
|
+
import { run as removeEmptyStyles } from "./remove-empty-styles.js";
|
|
6
|
+
/**
|
|
7
|
+
* Run all general XHTML fixes in sequence, in-process.
|
|
8
|
+
*/
|
|
9
|
+
export async function runFixes(opts = {}) {
|
|
10
|
+
await fixSpanTags(opts);
|
|
11
|
+
await fixXml(opts);
|
|
12
|
+
await removeEmptyStyles(opts);
|
|
13
|
+
reportInfo("All general fixes applied.");
|
|
14
|
+
}
|
|
15
|
+
if (isEntryPoint(import.meta.url)) {
|
|
16
|
+
runFixes().catch((err) => {
|
|
17
|
+
reportError(err);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type RunOpts } from "../utils.js";
|
|
2
|
+
/**
|
|
3
|
+
* Remove all empty `style=""` attributes from an XHTML string.
|
|
4
|
+
* Pure function — easy to unit test.
|
|
5
|
+
*/
|
|
6
|
+
export declare function removeEmptyStyles(content: string): {
|
|
7
|
+
xml: string;
|
|
8
|
+
removed: number;
|
|
9
|
+
};
|
|
10
|
+
export declare function run(opts?: RunOpts): Promise<void>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { reportError, reportInfo } from "../../utils/diagnostics.js";
|
|
2
|
+
// This script removes empty style="" attributes from XHTML files.
|
|
3
|
+
// Empty style attributes add unnecessary bytes without providing any styling.
|
|
4
|
+
import fs from "fs-extra";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import * as cheerio from "cheerio";
|
|
7
|
+
import { getTempDir, isEntryPoint } from "../utils.js";
|
|
8
|
+
import { normalizeNamedEntities } from "../../utils/xhtml.js";
|
|
9
|
+
import { collectRepairFiles } from "./xhtml-files.js";
|
|
10
|
+
/**
|
|
11
|
+
* Remove all empty `style=""` attributes from an XHTML string.
|
|
12
|
+
* Pure function — easy to unit test.
|
|
13
|
+
*/
|
|
14
|
+
export function removeEmptyStyles(content) {
|
|
15
|
+
const $ = cheerio.load(normalizeNamedEntities(content), { xml: { decodeEntities: false } });
|
|
16
|
+
let removed = 0;
|
|
17
|
+
$('[style=""]').each((_index, element) => {
|
|
18
|
+
$(element).removeAttr("style");
|
|
19
|
+
removed++;
|
|
20
|
+
});
|
|
21
|
+
return { xml: $.xml(), removed };
|
|
22
|
+
}
|
|
23
|
+
async function fixFile(file) {
|
|
24
|
+
try {
|
|
25
|
+
reportInfo(`Processing ${file}`);
|
|
26
|
+
const content = await fs.readFile(file, "utf8");
|
|
27
|
+
const { xml, removed } = removeEmptyStyles(content);
|
|
28
|
+
if (removed > 0) {
|
|
29
|
+
await fs.writeFile(file, xml);
|
|
30
|
+
reportInfo(`Removed ${removed} empty style attribute(s) from ${path.basename(file)}`);
|
|
31
|
+
}
|
|
32
|
+
return removed;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
reportError(`Error processing ${file}:`, error);
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export async function run(opts = {}) {
|
|
40
|
+
const extractedDir = opts.tempDir ?? getTempDir();
|
|
41
|
+
const files = await collectRepairFiles(extractedDir);
|
|
42
|
+
let totalRemoved = 0;
|
|
43
|
+
for (const file of files) {
|
|
44
|
+
totalRemoved += await fixFile(file);
|
|
45
|
+
}
|
|
46
|
+
reportInfo(`Removed ${totalRemoved} empty style attributes from ${files.length} files.`);
|
|
47
|
+
}
|
|
48
|
+
if (isEntryPoint(import.meta.url)) {
|
|
49
|
+
run().catch((err) => {
|
|
50
|
+
reportError(err);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { collectManifestResources, getContentPath } from "../../utils/epub-utils.js";
|
|
4
|
+
import { collectFiles, hasExtension } from "../../utils/files.js";
|
|
5
|
+
import { isSameOrInside } from "../../utils/path-safety.js";
|
|
6
|
+
const XHTML_EXTENSIONS = new Set([".xhtml"]);
|
|
7
|
+
/** Discover repairable XHTML without following symbolic links. */
|
|
8
|
+
export async function collectRepairFiles(extractedDir) {
|
|
9
|
+
if (!(await fs.pathExists(extractedDir))) {
|
|
10
|
+
throw new Error(`Directory ${extractedDir} does not exist.`);
|
|
11
|
+
}
|
|
12
|
+
const root = path.resolve(extractedDir);
|
|
13
|
+
const contentDir = await getContentPath(root);
|
|
14
|
+
if (!(await fs.pathExists(contentDir))) {
|
|
15
|
+
throw new Error(`Content directory ${contentDir} does not exist.`);
|
|
16
|
+
}
|
|
17
|
+
const manifestFiles = new Set(await collectManifestResources(root, {
|
|
18
|
+
mediaTypes: new Set(["application/xhtml+xml"]),
|
|
19
|
+
extensions: XHTML_EXTENSIONS,
|
|
20
|
+
}));
|
|
21
|
+
// Keep the legacy extension-based discovery, including undeclared files,
|
|
22
|
+
// while finding nested chapters and manifest resources outside the OPF folder.
|
|
23
|
+
// Walking from the archive root never descends into a symlinked directory.
|
|
24
|
+
const candidates = await collectFiles(root, (file) => manifestFiles.has(file) ||
|
|
25
|
+
(isSameOrInside(file, contentDir) && hasExtension(file, XHTML_EXTENSIONS)));
|
|
26
|
+
const files = [];
|
|
27
|
+
for (const file of candidates) {
|
|
28
|
+
// collectFiles can also return symlinks and special files; repair only
|
|
29
|
+
// regular files reached through the archive's real directory entries.
|
|
30
|
+
if ((await fs.lstat(file)).isFile())
|
|
31
|
+
files.push(file);
|
|
32
|
+
}
|
|
33
|
+
return files.sort();
|
|
34
|
+
}
|