cook-ssg 2.1.3 → 2.2.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 +48 -20
- package/package.json +2 -5
- package/scripts/build.js +91 -26
- package/scripts/dev.js +46 -16
- package/scripts/plugins/apply-template.js +8 -17
- package/scripts/plugins/auto-components.js +2 -2
- package/scripts/plugins/bundle.js +6 -3
- package/scripts/plugins/copy-src.js +1 -1
- package/scripts/plugins/create-dir-from-file.js +4 -2
- package/scripts/plugins/custom-plugins.js +1 -1
- package/scripts/plugins/declarative-shadow-dom.js +2 -2
- package/scripts/plugins/escape-code-blocks.js +22 -1
- package/scripts/plugins/fingerprint-assets.js +133 -13
- package/scripts/plugins/generate-formats.js +49 -39
- package/scripts/plugins/generate-fragments.js +24 -14
- package/scripts/plugins/generate-sitemap-xml.js +38 -15
- package/scripts/plugins/minify-src.js +98 -72
- package/scripts/plugins/optimize-images.js +80 -26
- package/scripts/plugins/process-markdown.js +12 -25
- package/scripts/plugins/repeat-collection.js +70 -45
- package/scripts/plugins/replace-components.js +82 -60
- package/scripts/plugins/replace-external-link-protocol.js +16 -31
- package/scripts/plugins/replace-include.js +34 -19
- package/scripts/plugins/replace-inline.js +6 -3
- package/scripts/plugins/replace-template-strings.js +2 -50
- package/scripts/plugins/set-active-links.js +57 -26
- package/scripts/utils/config/config.js +29 -5
- package/scripts/utils/get-src/get-src.js +9 -45
- package/scripts/utils/interpolate/interpolate.js +83 -0
- package/scripts/utils/logger/logger.js +8 -3
- package/scripts/utils/site-pages/site-pages.js +37 -0
- package/scripts/utils/source-file/source-file.js +64 -0
- package/scripts/utils/user-data/user-data.js +45 -0
- package/scripts/utils/util/util.js +88 -46
- package/patches/jsdom-fetch-fix.js +0 -52
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file minify-src.js
|
|
3
|
-
* @description
|
|
3
|
+
* @description Minify HTML, CSS, and JS sources in `/dist`
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
// IMPORT
|
|
@@ -15,11 +15,32 @@ import Util from '../utils/util/util.js';
|
|
|
15
15
|
// Config
|
|
16
16
|
import { srcPath, distPath, minifyHtmlConfigCustom } from '../utils/config/config.js';
|
|
17
17
|
|
|
18
|
+
// Inline <script> types that contain executable JavaScript.
|
|
19
|
+
// Anything else (application/ld+json, importmap, speculationrules,
|
|
20
|
+
// text/x-template, etc.) must be left untouched — terser rejects them.
|
|
21
|
+
const JS_SCRIPT_TYPES = new Set(['', 'text/javascript', 'application/javascript', 'module']);
|
|
22
|
+
|
|
23
|
+
// Content-keyed cache for minified inline JS/CSS. Sites repeat identical
|
|
24
|
+
// inline snippets across pages (shared includes/components: analytics,
|
|
25
|
+
// theme toggles, component styles) — minify each distinct snippet once
|
|
26
|
+
// per build process instead of once per page. Bounded to avoid growth
|
|
27
|
+
// in long-lived dev sessions.
|
|
28
|
+
const MINIFY_CACHE_MAX = 500;
|
|
29
|
+
const jsCache = new Map();
|
|
30
|
+
const cssCache = new Map();
|
|
31
|
+
function cacheGet(cache, key) {
|
|
32
|
+
return cache.get(key);
|
|
33
|
+
}
|
|
34
|
+
function cacheSet(cache, key, value) {
|
|
35
|
+
if (cache.size >= MINIFY_CACHE_MAX) cache.clear();
|
|
36
|
+
cache.set(key, value);
|
|
37
|
+
}
|
|
38
|
+
|
|
18
39
|
|
|
19
40
|
// DEFINE
|
|
20
41
|
// -----------------------------
|
|
21
42
|
/**
|
|
22
|
-
* @description
|
|
43
|
+
* @description Minify the current file's source based on its type
|
|
23
44
|
* @param {Object} obj - Deconstructed options object
|
|
24
45
|
* @property {Object} obj.file - The current file's info (name, extension, path, src, etc.)
|
|
25
46
|
* @property {Array} [obj.allowType] - Allowed file types (Opt-in)
|
|
@@ -88,13 +109,15 @@ class MinifySrc {
|
|
|
88
109
|
// Minify source differently based on the file type
|
|
89
110
|
let newSrc;
|
|
90
111
|
switch (file.ext) {
|
|
91
|
-
case 'css': newSrc = this.minCss(
|
|
92
|
-
case 'html': newSrc = await this.minHtml(
|
|
93
|
-
case 'js': newSrc = await this.minJs({file}); break;
|
|
112
|
+
case 'css': newSrc = this.minCss(file.src); break;
|
|
113
|
+
case 'html': newSrc = await this.minHtml(file.src, file.path); break;
|
|
114
|
+
case 'js': newSrc = await this.minJs(file.src, { label: file.path }); break;
|
|
94
115
|
}
|
|
95
116
|
|
|
96
|
-
// Store new source
|
|
97
|
-
|
|
117
|
+
// Store new source.
|
|
118
|
+
// Unrecognized types fall through with `newSrc` undefined and must
|
|
119
|
+
// keep their original source — never write `undefined` to disk.
|
|
120
|
+
if (typeof newSrc === 'string') file.src = newSrc;
|
|
98
121
|
|
|
99
122
|
// END LOGGING
|
|
100
123
|
this.endLog();
|
|
@@ -103,36 +126,54 @@ class MinifySrc {
|
|
|
103
126
|
|
|
104
127
|
// PRIMARY MINIFY METHODS
|
|
105
128
|
// -----------------------------
|
|
106
|
-
|
|
129
|
+
// All methods are string-in/string-out and NEVER mutate `file.src` —
|
|
130
|
+
// a failed minification must leave the page source untouched, not
|
|
131
|
+
// replace it with an intermediate fragment.
|
|
132
|
+
|
|
133
|
+
async minHtml(src, label) {
|
|
107
134
|
// MINIFY HTML — wrapped in try/catch so parse errors on pages with
|
|
108
135
|
// complex inline code examples don't crash the entire build
|
|
109
136
|
try {
|
|
110
|
-
|
|
111
|
-
// MINIFY INLINE CSS
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
file.src = await this.minifyInlineAsync(file, 'script', this.minJs.bind(this));
|
|
137
|
+
let out = await minifyHtml(src, this.minifyHtmlConfig);
|
|
138
|
+
// MINIFY INLINE CSS + SCRIPTS (single parse for both)
|
|
139
|
+
out = await this.minifyInlineAssets(out, label);
|
|
140
|
+
return out;
|
|
115
141
|
} catch (err) {
|
|
116
142
|
// Skip minification for this file — likely has unescaped code examples
|
|
117
143
|
// that confuse the HTML parser. The unminified output is still valid.
|
|
118
|
-
if (process.env.LOGGER) console.warn(` ⚠ Skipped minification for ${
|
|
144
|
+
if (process.env.LOGGER) console.warn(` ⚠ Skipped minification for ${label}: ${err.message.substring(0, 80)}`);
|
|
145
|
+
return src;
|
|
119
146
|
}
|
|
120
|
-
// Return new src
|
|
121
|
-
return file.src;
|
|
122
147
|
}
|
|
123
148
|
|
|
124
|
-
async minJs({
|
|
125
|
-
const
|
|
126
|
-
|
|
149
|
+
async minJs(src, { module = false, label } = {}) {
|
|
150
|
+
const cacheKey = (module ? 'm:' : 's:') + src;
|
|
151
|
+
const cached = cacheGet(jsCache, cacheKey);
|
|
152
|
+
if (cached !== undefined) return cached;
|
|
153
|
+
try {
|
|
154
|
+
const config = module ? { ...this.minifyJsConfig, module: true } : this.minifyJsConfig;
|
|
155
|
+
const result = await terser.minify(src, config);
|
|
156
|
+
const out = typeof result.code === 'string' ? result.code : src;
|
|
157
|
+
cacheSet(jsCache, cacheKey, out);
|
|
158
|
+
return out;
|
|
159
|
+
} catch (err) {
|
|
160
|
+
// Invalid or non-standard JS: ship it unminified rather than
|
|
161
|
+
// corrupting the file or killing the build.
|
|
162
|
+
if (process.env.LOGGER) console.warn(` ⚠ Skipped JS minification${label ? ` for ${label}` : ''}: ${err.message.substring(0, 80)}`);
|
|
163
|
+
return src;
|
|
164
|
+
}
|
|
127
165
|
}
|
|
128
166
|
|
|
129
|
-
minCss(
|
|
167
|
+
minCss(src) {
|
|
168
|
+
const cached = cacheGet(cssCache, src);
|
|
169
|
+
if (cached !== undefined) return cached;
|
|
130
170
|
// Minify source
|
|
131
|
-
|
|
171
|
+
let out = new CleanCSS(this.minifyCssConfig).minify(src).styles;
|
|
132
172
|
// Replace '@import' calls with their inlined source
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
173
|
+
out = this.replaceCssImports(out);
|
|
174
|
+
cacheSet(cssCache, src, out);
|
|
175
|
+
// Return modified source
|
|
176
|
+
return out;
|
|
136
177
|
}
|
|
137
178
|
|
|
138
179
|
|
|
@@ -140,21 +181,21 @@ class MinifySrc {
|
|
|
140
181
|
// -----------------------------
|
|
141
182
|
|
|
142
183
|
/**
|
|
143
|
-
* @description
|
|
144
|
-
* @param {
|
|
145
|
-
* @
|
|
184
|
+
* @description Inline `@import url(...)` sources into the CSS
|
|
185
|
+
* @param {String} src - The CSS source
|
|
186
|
+
* @returns {String}
|
|
146
187
|
* @private
|
|
147
188
|
*/
|
|
148
189
|
// TODO: The regex could likely be done a little nicer if we could use lookbehinds.
|
|
149
190
|
// Instead for now, the pattern matches: url(/css/variables.css
|
|
150
191
|
// so we just lop off the first four characters
|
|
151
|
-
replaceCssImports(
|
|
192
|
+
replaceCssImports(src) {
|
|
152
193
|
let path, replaceSrc;
|
|
153
194
|
// Check for @import and replace them with their source
|
|
154
195
|
const pattern = /@import\s*url\(([/.\_\-)a-z]*);/gim;
|
|
155
|
-
const matches =
|
|
196
|
+
const matches = src.match(pattern);
|
|
156
197
|
if (matches) {
|
|
157
|
-
matches.forEach(
|
|
198
|
+
matches.forEach(m => {
|
|
158
199
|
path = m.match(/url\(.*(?=\))/gim)[0].slice(4);
|
|
159
200
|
// If path has /src at the start, strip it off
|
|
160
201
|
path = path.replace(/^\/src/, '');
|
|
@@ -163,58 +204,43 @@ class MinifySrc {
|
|
|
163
204
|
// Minimize it
|
|
164
205
|
replaceSrc = new CleanCSS(this.minifyCssConfig).minify(replaceSrc).styles;
|
|
165
206
|
// Replace @import with source
|
|
166
|
-
|
|
167
|
-
file.src = file.src.replace(m, replaceSrc);
|
|
207
|
+
src = src.replace(m, () => replaceSrc);
|
|
168
208
|
});
|
|
169
209
|
}
|
|
170
|
-
return
|
|
210
|
+
return src;
|
|
171
211
|
}
|
|
172
212
|
|
|
173
213
|
/**
|
|
174
|
-
* @description Minify inline `<style>`
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* @param {
|
|
214
|
+
* @description Minify inline `<style>` and `<script>` contents in a
|
|
215
|
+
* single parse. Skips external scripts and non-JS script types
|
|
216
|
+
* (JSON-LD, import maps, etc.)
|
|
217
|
+
* @param {String} src - The page source
|
|
218
|
+
* @param {String} [label] - File path for warning messages
|
|
219
|
+
* @returns {String}
|
|
178
220
|
* @private
|
|
179
221
|
*/
|
|
180
|
-
|
|
181
|
-
const dom = Util.jsdom.dom({src
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
// Update tag with new minified source
|
|
189
|
-
el.textContent = minifiedSrc;
|
|
222
|
+
async minifyInlineAssets(src, label) {
|
|
223
|
+
const dom = Util.jsdom.dom({src});
|
|
224
|
+
const document = dom.window.document;
|
|
225
|
+
|
|
226
|
+
document.querySelectorAll('style').forEach(el => {
|
|
227
|
+
const content = el.textContent;
|
|
228
|
+
if (!content || !content.trim()) return;
|
|
229
|
+
el.textContent = this.minCss(content);
|
|
190
230
|
});
|
|
191
|
-
// Store updated file source
|
|
192
|
-
file.src = Util.setSrc({dom});
|
|
193
|
-
// Return updated file source
|
|
194
|
-
return file.src;
|
|
195
|
-
}
|
|
196
231
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const group = dom.window.document.querySelectorAll(selector);
|
|
207
|
-
for (const el of group) {
|
|
208
|
-
file.src = el.textContent;
|
|
209
|
-
// Minify content
|
|
210
|
-
const minifiedSrc = await type({file});
|
|
211
|
-
// Update tag with new minified source
|
|
212
|
-
el.textContent = minifiedSrc;
|
|
232
|
+
for (const el of document.querySelectorAll('script')) {
|
|
233
|
+
// Only minify executable JavaScript
|
|
234
|
+
const type = (el.getAttribute('type') || '').trim().toLowerCase();
|
|
235
|
+
if (!JS_SCRIPT_TYPES.has(type)) continue;
|
|
236
|
+
// External scripts have no inline content to minify
|
|
237
|
+
if (el.hasAttribute('src')) continue;
|
|
238
|
+
const content = el.textContent;
|
|
239
|
+
if (!content || !content.trim()) continue;
|
|
240
|
+
el.textContent = await this.minJs(content, { module: type === 'module', label });
|
|
213
241
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
// Return updated file source
|
|
217
|
-
return file.src;
|
|
242
|
+
|
|
243
|
+
return Util.setSrc({dom});
|
|
218
244
|
}
|
|
219
245
|
|
|
220
246
|
|
|
@@ -31,8 +31,10 @@ import { distPath, images as imagesConfig } from '../utils/config/config.js';
|
|
|
31
31
|
// ============================================================================
|
|
32
32
|
|
|
33
33
|
class OptimizeImagesGenerate {
|
|
34
|
-
constructor({ store }) {
|
|
34
|
+
constructor({ store, distPath: distPathOverride, images: imagesOverride }) {
|
|
35
35
|
this.store = store;
|
|
36
|
+
// Optional per-call overrides (used by tests and custom pipelines)
|
|
37
|
+
this.distPath = distPathOverride || distPath;
|
|
36
38
|
this.config = {
|
|
37
39
|
enabled: true,
|
|
38
40
|
widths: [320, 640, 960, 1280, 1920],
|
|
@@ -42,6 +44,7 @@ class OptimizeImagesGenerate {
|
|
|
42
44
|
loading: 'lazy',
|
|
43
45
|
cache: '.cache/images',
|
|
44
46
|
...imagesConfig,
|
|
47
|
+
...(imagesOverride || {}),
|
|
45
48
|
};
|
|
46
49
|
|
|
47
50
|
// Initialize manifest on store
|
|
@@ -56,7 +59,7 @@ class OptimizeImagesGenerate {
|
|
|
56
59
|
Logger.persist.header('\nOptimize Images');
|
|
57
60
|
|
|
58
61
|
// Find all raster images in dist/
|
|
59
|
-
const imageFiles = await this.findImages(distPath);
|
|
62
|
+
const imageFiles = await this.findImages(this.distPath);
|
|
60
63
|
|
|
61
64
|
if (!imageFiles.length) {
|
|
62
65
|
Logger.persist.success('No images found (skipped)');
|
|
@@ -80,11 +83,19 @@ class OptimizeImagesGenerate {
|
|
|
80
83
|
let generated = 0;
|
|
81
84
|
let cached = 0;
|
|
82
85
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
86
|
+
// Process images with bounded concurrency — sharp encodes on its
|
|
87
|
+
// own thread pool, so overlapping a few images cuts wall time
|
|
88
|
+
const CONCURRENCY = 4;
|
|
89
|
+
const queue = [...imageFiles];
|
|
90
|
+
const workers = Array.from({ length: Math.min(CONCURRENCY, queue.length) }, async () => {
|
|
91
|
+
while (queue.length) {
|
|
92
|
+
const imgPath = queue.shift();
|
|
93
|
+
const result = await this.processImage(imgPath, diskCache);
|
|
94
|
+
if (result.fromCache) cached++;
|
|
95
|
+
else generated++;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
await Promise.all(workers);
|
|
88
99
|
|
|
89
100
|
// Write updated disk cache
|
|
90
101
|
await fs.writeFile(cacheManifestPath, JSON.stringify(diskCache, null, 2), 'utf-8');
|
|
@@ -103,7 +114,11 @@ class OptimizeImagesGenerate {
|
|
|
103
114
|
if (!allPaths) return [];
|
|
104
115
|
return allPaths.filter(p => {
|
|
105
116
|
const ext = path.extname(p).toLowerCase();
|
|
106
|
-
|
|
117
|
+
if (!rasterExts.includes(ext)) return false;
|
|
118
|
+
// Skip our own generated derivatives (foo-320w.webp) — rescanning
|
|
119
|
+
// them produces derivative-of-derivative cascades (foo-320w-320w.avif)
|
|
120
|
+
if (/-\d+w\.(avif|webp|jpe?g|png)$/i.test(p)) return false;
|
|
121
|
+
return true;
|
|
107
122
|
});
|
|
108
123
|
}
|
|
109
124
|
|
|
@@ -119,30 +134,35 @@ class OptimizeImagesGenerate {
|
|
|
119
134
|
const hash = createHash('md5').update(buffer).digest('hex');
|
|
120
135
|
|
|
121
136
|
// Get relative path from dist for manifest key
|
|
122
|
-
const relPath = imgPath.replace(`${distPath}/`, '/');
|
|
137
|
+
const relPath = imgPath.replace(`${this.distPath}/`, '/');
|
|
123
138
|
|
|
124
|
-
// Check disk cache
|
|
139
|
+
// Check disk cache: derivatives are copied back into dist from the
|
|
140
|
+
// cache directory (dist is wiped every production build, so checking
|
|
141
|
+
// dist alone made the cache permanently cold).
|
|
125
142
|
if (diskCache[relPath] && diskCache[relPath].hash === hash) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
existsSync(path.join(distPath, d.path))
|
|
129
|
-
);
|
|
130
|
-
if (allExist) {
|
|
143
|
+
const restored = await this.restoreDerivatives(diskCache[relPath], hash);
|
|
144
|
+
if (restored) {
|
|
131
145
|
this.store.imageManifest[relPath] = diskCache[relPath];
|
|
132
146
|
return { fromCache: true };
|
|
133
147
|
}
|
|
134
148
|
}
|
|
135
149
|
|
|
136
|
-
// Get image metadata
|
|
150
|
+
// Get image metadata.
|
|
151
|
+
// Auto-rotation (EXIF orientation) swaps the visible dimensions for
|
|
152
|
+
// orientations 5-8 (90°/270°) — use the VISUAL size everywhere.
|
|
137
153
|
const metadata = await sharp(buffer).metadata();
|
|
138
|
-
const
|
|
139
|
-
const
|
|
154
|
+
const orientationSwaps = (metadata.orientation || 1) >= 5;
|
|
155
|
+
const srcWidth = orientationSwaps ? metadata.height : metadata.width;
|
|
156
|
+
const srcHeight = orientationSwaps ? metadata.width : metadata.height;
|
|
140
157
|
|
|
141
158
|
// Determine which widths to generate (no upscaling)
|
|
142
159
|
const fittingWidths = this.config.widths.filter(w => w <= srcWidth);
|
|
143
160
|
// If no configured width fits, use the source width for format conversion
|
|
144
161
|
const widths = fittingWidths.length > 0 ? fittingWidths : [srcWidth];
|
|
145
162
|
|
|
163
|
+
// Ensure the cache files directory exists before generating
|
|
164
|
+
await fs.mkdir(path.join(this.config.cache, 'files'), { recursive: true });
|
|
165
|
+
|
|
146
166
|
const parsed = path.parse(imgPath);
|
|
147
167
|
const baseName = parsed.name;
|
|
148
168
|
const baseDir = parsed.dir;
|
|
@@ -168,7 +188,9 @@ class OptimizeImagesGenerate {
|
|
|
168
188
|
// Calculate proportional height
|
|
169
189
|
const height = Math.round((width / srcWidth) * srcHeight);
|
|
170
190
|
|
|
171
|
-
|
|
191
|
+
// .rotate() with no args applies EXIF orientation and strips the
|
|
192
|
+
// tag, so portrait phone photos aren't resized sideways
|
|
193
|
+
let pipeline = sharp(buffer).rotate().resize(width, height, { fit: 'inside', withoutEnlargement: true });
|
|
172
194
|
|
|
173
195
|
if (format === 'avif') pipeline = pipeline.avif({ quality });
|
|
174
196
|
else if (format === 'webp') pipeline = pipeline.webp({ quality });
|
|
@@ -177,8 +199,12 @@ class OptimizeImagesGenerate {
|
|
|
177
199
|
|
|
178
200
|
await pipeline.toFile(outPath);
|
|
179
201
|
|
|
202
|
+
// Keep a copy in the cache directory so the next build can
|
|
203
|
+
// restore it without re-encoding (dist gets wiped every build)
|
|
204
|
+
await fs.copyFile(outPath, this.cacheFilePath(hash, width, outExt));
|
|
205
|
+
|
|
180
206
|
derivatives.push({
|
|
181
|
-
path: outPath.replace(`${distPath}`, '').replace(/\\/g, '/'),
|
|
207
|
+
path: outPath.replace(`${this.distPath}`, '').replace(/\\/g, '/'),
|
|
182
208
|
format,
|
|
183
209
|
width,
|
|
184
210
|
height,
|
|
@@ -200,6 +226,36 @@ class OptimizeImagesGenerate {
|
|
|
200
226
|
return { fromCache: false };
|
|
201
227
|
}
|
|
202
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Path of a cached derivative file for a given source hash + width
|
|
231
|
+
* @param {string} hash - Source image content hash
|
|
232
|
+
* @param {number} width - Derivative width
|
|
233
|
+
* @param {string} ext - Output extension (no dot)
|
|
234
|
+
* @returns {string}
|
|
235
|
+
*/
|
|
236
|
+
cacheFilePath(hash, width, ext) {
|
|
237
|
+
return path.join(this.config.cache, 'files', `${hash}-${width}w.${ext}`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Copy cached derivative files into dist for a cache-hit source image.
|
|
242
|
+
* @param {Object} entry - Disk cache manifest entry
|
|
243
|
+
* @param {string} hash - Source image content hash
|
|
244
|
+
* @returns {boolean} false if any derivative is missing from the cache
|
|
245
|
+
*/
|
|
246
|
+
async restoreDerivatives(entry, hash) {
|
|
247
|
+
for (const d of entry.derivatives) {
|
|
248
|
+
const distFile = path.join(this.distPath, d.path);
|
|
249
|
+
if (existsSync(distFile)) continue;
|
|
250
|
+
const ext = path.extname(d.path).slice(1);
|
|
251
|
+
const cacheFile = this.cacheFilePath(hash, d.width, ext);
|
|
252
|
+
if (!existsSync(cacheFile)) return false;
|
|
253
|
+
await fs.mkdir(path.dirname(distFile), { recursive: true });
|
|
254
|
+
await fs.copyFile(cacheFile, distFile);
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
|
|
203
259
|
|
|
204
260
|
// EXPORT WRAPPER
|
|
205
261
|
// -----------------------------
|
|
@@ -237,8 +293,8 @@ class OptimizeImagesRewrite {
|
|
|
237
293
|
const allowed = Util.isAllowedType(this.opts);
|
|
238
294
|
if (!allowed) return;
|
|
239
295
|
|
|
240
|
-
// Parse HTML
|
|
241
|
-
const dom = Util.
|
|
296
|
+
// Parse HTML (shared cached document — see Util.fileDom)
|
|
297
|
+
const dom = Util.fileDom(this.file);
|
|
242
298
|
const document = dom.window.document;
|
|
243
299
|
|
|
244
300
|
// Find all <img> tags
|
|
@@ -252,10 +308,8 @@ class OptimizeImagesRewrite {
|
|
|
252
308
|
if (changed) rewrote++;
|
|
253
309
|
}
|
|
254
310
|
|
|
255
|
-
// Only
|
|
256
|
-
|
|
257
|
-
this.file.src = Util.setSrc({ dom });
|
|
258
|
-
}
|
|
311
|
+
// Only mark the document mutated if we made changes
|
|
312
|
+
Util.commitDom(this.file, dom, rewrote > 0);
|
|
259
313
|
}
|
|
260
314
|
|
|
261
315
|
/**
|
|
@@ -13,6 +13,7 @@ import { marked } from 'marked';
|
|
|
13
13
|
import matter from 'gray-matter';
|
|
14
14
|
import Logger from '../utils/logger/logger.js';
|
|
15
15
|
import Util from '../utils/util/util.js';
|
|
16
|
+
import { renderTemplate } from '../utils/interpolate/interpolate.js';
|
|
16
17
|
|
|
17
18
|
// Config
|
|
18
19
|
import { distPath, srcPath, markdown as markdownConfig } from '../utils/config/config.js';
|
|
@@ -163,7 +164,7 @@ class ProcessMarkdown {
|
|
|
163
164
|
});
|
|
164
165
|
|
|
165
166
|
} catch (err) {
|
|
166
|
-
Util.
|
|
167
|
+
Util.customWarn(err, `ProcessMarkdown: ${filePath} (page skipped)`);
|
|
167
168
|
}
|
|
168
169
|
}
|
|
169
170
|
|
|
@@ -227,31 +228,15 @@ class ProcessMarkdown {
|
|
|
227
228
|
return this.buildBasicHtml(pageData);
|
|
228
229
|
}
|
|
229
230
|
|
|
230
|
-
//
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
//
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const trimmedKey = key.trim();
|
|
239
|
-
|
|
240
|
-
// Check page data first
|
|
241
|
-
if (pageData[trimmedKey] !== undefined) {
|
|
242
|
-
return pageData[trimmedKey];
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Then check global data
|
|
246
|
-
if (this.data[trimmedKey] !== undefined) {
|
|
247
|
-
return this.data[trimmedKey];
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// Leave unmatched placeholders as-is (for later processing)
|
|
251
|
-
return match;
|
|
231
|
+
// Shared interpolator: single pass over the LAYOUT only — markdown
|
|
232
|
+
// content (which legitimately contains ${...} and $-patterns in code
|
|
233
|
+
// samples) is never scanned for substitution. Page data wins over
|
|
234
|
+
// global data; ${content} always resolves.
|
|
235
|
+
return renderTemplate(layoutContent, {
|
|
236
|
+
...this.data,
|
|
237
|
+
...pageData,
|
|
238
|
+
content: pageData.content ?? '',
|
|
252
239
|
});
|
|
253
|
-
|
|
254
|
-
return html;
|
|
255
240
|
}
|
|
256
241
|
|
|
257
242
|
/**
|
|
@@ -382,3 +367,5 @@ ${pageData.content}
|
|
|
382
367
|
// EXPORT
|
|
383
368
|
// -----------------------------
|
|
384
369
|
export default ProcessMarkdown.export;
|
|
370
|
+
// Named export for testing
|
|
371
|
+
export { ProcessMarkdown };
|