cook-ssg 2.1.2 → 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 +4 -7
- package/scripts/build.js +98 -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 +388 -0
- 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 +44 -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
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
// -----------------------------
|
|
22
22
|
import chalk from 'chalk';
|
|
23
23
|
import Util from '../utils/util/util.js';
|
|
24
|
+
import { renderTemplate as interpolate, getNested } from '../utils/interpolate/interpolate.js';
|
|
24
25
|
|
|
25
26
|
// Config
|
|
26
27
|
import { repeat as repeatConfig } from '../utils/config/config.js';
|
|
@@ -67,11 +68,14 @@ class RepeatCollection {
|
|
|
67
68
|
if (!allowed) return;
|
|
68
69
|
|
|
69
70
|
// Store string source as traversable DOM
|
|
70
|
-
const dom = Util.
|
|
71
|
+
const dom = Util.fileDom(this.file);
|
|
71
72
|
const document = dom.window.document;
|
|
72
73
|
|
|
73
|
-
// Find
|
|
74
|
-
|
|
74
|
+
// Find TOP-LEVEL repeat elements only — nested data-repeat elements
|
|
75
|
+
// are processed recursively with their enclosing iteration context
|
|
76
|
+
// (so "post.tags" can resolve the outer loop variable `post`).
|
|
77
|
+
const allRepeats = Array.from(document.querySelectorAll(`[${this.config.attribute}]`));
|
|
78
|
+
const repeatElements = allRepeats.filter(el => !this.hasRepeatAncestor(el, document));
|
|
75
79
|
this.total = repeatElements.length;
|
|
76
80
|
|
|
77
81
|
// Early Exit: No repeat elements
|
|
@@ -79,19 +83,36 @@ class RepeatCollection {
|
|
|
79
83
|
|
|
80
84
|
// Process each repeat element
|
|
81
85
|
for (const element of repeatElements) {
|
|
82
|
-
this.processRepeatElement(element, document);
|
|
86
|
+
this.processRepeatElement(element, document, {});
|
|
83
87
|
}
|
|
84
88
|
|
|
85
89
|
// Store updated file source
|
|
86
|
-
this.file
|
|
90
|
+
Util.commitDom(this.file, dom);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Return whether the element sits inside another data-repeat element.
|
|
95
|
+
* Such elements are expanded recursively by their parent iteration,
|
|
96
|
+
* never at the top level (their collection may be a loop variable).
|
|
97
|
+
* @param {Element} element
|
|
98
|
+
* @returns {boolean}
|
|
99
|
+
*/
|
|
100
|
+
hasRepeatAncestor(element) {
|
|
101
|
+
let parent = element.parentElement;
|
|
102
|
+
while (parent) {
|
|
103
|
+
if (parent.hasAttribute && parent.hasAttribute(this.config.attribute)) return true;
|
|
104
|
+
parent = parent.parentElement;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
87
107
|
}
|
|
88
108
|
|
|
89
109
|
/**
|
|
90
110
|
* Process a single repeat element
|
|
91
111
|
* @param {Element} element - The element with data-repeat
|
|
92
112
|
* @param {Document} document - The document object
|
|
113
|
+
* @param {Object} outerContext - Enclosing iteration context (loop vars)
|
|
93
114
|
*/
|
|
94
|
-
processRepeatElement(element, document) {
|
|
115
|
+
processRepeatElement(element, document, outerContext = {}) {
|
|
95
116
|
const expression = element.getAttribute(this.config.attribute);
|
|
96
117
|
if (!expression) return;
|
|
97
118
|
|
|
@@ -102,8 +123,12 @@ class RepeatCollection {
|
|
|
102
123
|
return;
|
|
103
124
|
}
|
|
104
125
|
|
|
105
|
-
// Get the collection data
|
|
106
|
-
|
|
126
|
+
// Get the collection data: enclosing loop context first (nested
|
|
127
|
+
// repeats reference loop variables), then global data, then store
|
|
128
|
+
let collection = this.getNestedValue(outerContext, parsed.collection);
|
|
129
|
+
if (!collection || !Array.isArray(collection)) {
|
|
130
|
+
collection = this.getNestedValue(this.data, parsed.collection);
|
|
131
|
+
}
|
|
107
132
|
if (!collection || !Array.isArray(collection)) {
|
|
108
133
|
// Try getting from store (for collections built during build)
|
|
109
134
|
collection = this.getNestedValue(this.store, parsed.collection);
|
|
@@ -141,10 +166,12 @@ class RepeatCollection {
|
|
|
141
166
|
clone.removeAttribute(this.config.attribute);
|
|
142
167
|
}
|
|
143
168
|
|
|
144
|
-
// Build the data context for this iteration
|
|
169
|
+
// Build the data context for this iteration.
|
|
170
|
+
// Enclosing loop variables stay visible (nested repeats).
|
|
145
171
|
const context = {
|
|
172
|
+
...outerContext,
|
|
146
173
|
[parsed.itemName]: item,
|
|
147
|
-
...item, // Also spread item properties for direct access
|
|
174
|
+
...(item && typeof item === 'object' && !Array.isArray(item) ? item : {}), // Also spread item properties for direct access
|
|
148
175
|
};
|
|
149
176
|
|
|
150
177
|
// Add index if requested
|
|
@@ -159,6 +186,10 @@ class RepeatCollection {
|
|
|
159
186
|
// Also process attributes on the clone itself
|
|
160
187
|
this.processElementAttributes(clone, context);
|
|
161
188
|
|
|
189
|
+
// Recursively expand nested data-repeat elements with this
|
|
190
|
+
// iteration's context, so they can loop over loop variables
|
|
191
|
+
this.processNestedRepeats(clone, document, context);
|
|
192
|
+
|
|
162
193
|
fragment.appendChild(clone);
|
|
163
194
|
});
|
|
164
195
|
|
|
@@ -166,6 +197,29 @@ class RepeatCollection {
|
|
|
166
197
|
element.parentNode.replaceChild(fragment, element);
|
|
167
198
|
}
|
|
168
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Expand data-repeat elements directly nested inside a freshly-rendered
|
|
202
|
+
* clone (deeper ones are handled by their own recursion level).
|
|
203
|
+
* @param {Element} container - The clone to search within
|
|
204
|
+
* @param {Document} document - The document object
|
|
205
|
+
* @param {Object} context - The iteration context of the enclosing loop
|
|
206
|
+
*/
|
|
207
|
+
processNestedRepeats(container, document, context) {
|
|
208
|
+
const nested = Array.from(container.querySelectorAll(`[${this.config.attribute}]`))
|
|
209
|
+
.filter(el => {
|
|
210
|
+
// Keep only repeats whose nearest repeat ancestor is the container
|
|
211
|
+
let parent = el.parentElement;
|
|
212
|
+
while (parent && parent !== container) {
|
|
213
|
+
if (parent.hasAttribute(this.config.attribute)) return false;
|
|
214
|
+
parent = parent.parentElement;
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
});
|
|
218
|
+
for (const el of nested) {
|
|
219
|
+
this.processRepeatElement(el, document, context);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
169
223
|
/**
|
|
170
224
|
* Parse the repeat expression
|
|
171
225
|
* Supports: "collection as item" or "collection as item, index"
|
|
@@ -218,54 +272,25 @@ class RepeatCollection {
|
|
|
218
272
|
}
|
|
219
273
|
|
|
220
274
|
/**
|
|
221
|
-
* Get a nested property value from an object
|
|
275
|
+
* Get a nested property value from an object (shared implementation)
|
|
222
276
|
* @param {Object} obj - The object to search
|
|
223
277
|
* @param {string} path - Dot-notation path (e.g., "collections.blog")
|
|
224
278
|
* @returns {*} The value or undefined
|
|
225
279
|
*/
|
|
226
280
|
getNestedValue(obj, path) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const parts = path.split('.');
|
|
230
|
-
let current = obj;
|
|
231
|
-
|
|
232
|
-
for (const part of parts) {
|
|
233
|
-
if (current === null || current === undefined) {
|
|
234
|
-
return undefined;
|
|
235
|
-
}
|
|
236
|
-
current = current[part];
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return current;
|
|
281
|
+
return getNested(obj, path);
|
|
240
282
|
}
|
|
241
283
|
|
|
242
284
|
/**
|
|
243
|
-
* Render template string with data context
|
|
244
|
-
*
|
|
285
|
+
* Render template string with data context.
|
|
286
|
+
* Delegates to the shared interpolator (single ${var} semantics
|
|
287
|
+
* across all plugins; Dates and Arrays formatted for display).
|
|
245
288
|
* @param {string} template - Template string
|
|
246
289
|
* @param {Object} context - Data context
|
|
247
290
|
* @returns {string} Rendered string
|
|
248
291
|
*/
|
|
249
292
|
renderTemplate(template, context) {
|
|
250
|
-
return template
|
|
251
|
-
const trimmedKey = key.trim();
|
|
252
|
-
const value = this.getNestedValue(context, trimmedKey);
|
|
253
|
-
|
|
254
|
-
if (value !== undefined && value !== null) {
|
|
255
|
-
// Handle Date objects
|
|
256
|
-
if (value instanceof Date) {
|
|
257
|
-
return value.toLocaleDateString();
|
|
258
|
-
}
|
|
259
|
-
// Handle arrays
|
|
260
|
-
if (Array.isArray(value)) {
|
|
261
|
-
return value.join(', ');
|
|
262
|
-
}
|
|
263
|
-
return String(value);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// Leave unmatched variables for later processing
|
|
267
|
-
return match;
|
|
268
|
-
});
|
|
293
|
+
return interpolate(template, context);
|
|
269
294
|
}
|
|
270
295
|
|
|
271
296
|
/**
|
|
@@ -33,6 +33,7 @@ import { promises as fs } from 'node:fs';
|
|
|
33
33
|
import { existsSync } from 'node:fs';
|
|
34
34
|
import path from 'node:path';
|
|
35
35
|
import Util from '../utils/util/util.js';
|
|
36
|
+
import { renderTemplate as interpolate } from '../utils/interpolate/interpolate.js';
|
|
36
37
|
|
|
37
38
|
// Config
|
|
38
39
|
import { distPath, srcPath, components as componentsConfig } from '../utils/config/config.js';
|
|
@@ -78,29 +79,46 @@ class ReplaceComponents {
|
|
|
78
79
|
if (!allowed) return;
|
|
79
80
|
|
|
80
81
|
// Store string source as traversable DOM
|
|
81
|
-
const dom = Util.
|
|
82
|
+
const dom = Util.fileDom(this.file);
|
|
82
83
|
const document = dom.window.document;
|
|
83
84
|
|
|
84
85
|
// Find all components to replace
|
|
85
|
-
|
|
86
|
-
this.total = components.length;
|
|
86
|
+
let components = this.findComponents(document);
|
|
87
87
|
|
|
88
88
|
// Early Exit: No components found
|
|
89
|
-
if (
|
|
89
|
+
if (components.length === 0) return;
|
|
90
90
|
|
|
91
91
|
// START LOGGING
|
|
92
92
|
this.startLog();
|
|
93
93
|
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
// Expand until stable: rendered templates and slot content may
|
|
95
|
+
// themselves contain components (nesting), which only appear in the
|
|
96
|
+
// DOM after a pass completes. `handled` stops us retrying elements
|
|
97
|
+
// that stay in the DOM by design (DSD hosts, unknown components),
|
|
98
|
+
// and the pass cap bounds pathological self-referencing templates.
|
|
99
|
+
const MAX_PASSES = 10;
|
|
100
|
+
const handled = new Set();
|
|
101
|
+
let pass = 0;
|
|
102
|
+
while (components.length && pass < MAX_PASSES) {
|
|
103
|
+
let progressed = false;
|
|
104
|
+
for (const component of components) {
|
|
105
|
+
if (handled.has(component.element)) continue;
|
|
106
|
+
const before = this.replaced.length;
|
|
107
|
+
await this.replaceComponent(component);
|
|
108
|
+
handled.add(component.element);
|
|
109
|
+
if (this.replaced.length > before) progressed = true;
|
|
110
|
+
}
|
|
111
|
+
if (!progressed) break;
|
|
112
|
+
pass += 1;
|
|
113
|
+
components = this.findComponents(document).filter(c => !handled.has(c.element));
|
|
97
114
|
}
|
|
115
|
+
this.total = this.replaced.length;
|
|
98
116
|
|
|
99
117
|
// Inject collected component styles into <head>
|
|
100
118
|
this.injectCollectedStyles(document);
|
|
101
119
|
|
|
102
120
|
// Store updated file source
|
|
103
|
-
this.file
|
|
121
|
+
Util.commitDom(this.file, dom);
|
|
104
122
|
|
|
105
123
|
// END LOGGING
|
|
106
124
|
this.endLog();
|
|
@@ -113,6 +131,9 @@ class ReplaceComponents {
|
|
|
113
131
|
*/
|
|
114
132
|
findComponents(document) {
|
|
115
133
|
const components = [];
|
|
134
|
+
// Each element is processed at most once — a hyphenated tag that ALSO
|
|
135
|
+
// carries data-component must not be expanded twice.
|
|
136
|
+
const seen = new Set();
|
|
116
137
|
|
|
117
138
|
// 1. Find custom elements (hyphenated tag names)
|
|
118
139
|
// Custom elements must contain a hyphen per spec
|
|
@@ -122,6 +143,7 @@ class ReplaceComponents {
|
|
|
122
143
|
|
|
123
144
|
// Custom element (contains hyphen)
|
|
124
145
|
if (tagName.includes('-')) {
|
|
146
|
+
seen.add(el);
|
|
125
147
|
components.push({
|
|
126
148
|
element: el,
|
|
127
149
|
type: 'custom-element',
|
|
@@ -134,6 +156,7 @@ class ReplaceComponents {
|
|
|
134
156
|
// 2. Find elements with data-component attribute
|
|
135
157
|
const dataComponents = document.querySelectorAll('[data-component]');
|
|
136
158
|
for (const el of dataComponents) {
|
|
159
|
+
if (seen.has(el)) continue;
|
|
137
160
|
const componentAttr = el.getAttribute('data-component');
|
|
138
161
|
if (componentAttr) {
|
|
139
162
|
components.push({
|
|
@@ -179,7 +202,7 @@ class ReplaceComponents {
|
|
|
179
202
|
* @returns {boolean}
|
|
180
203
|
*/
|
|
181
204
|
isDsdComponent(template) {
|
|
182
|
-
return /<template
|
|
205
|
+
return /<template[^>]*\sshadowrootmode/i.test(template);
|
|
183
206
|
}
|
|
184
207
|
|
|
185
208
|
/**
|
|
@@ -208,7 +231,7 @@ class ReplaceComponents {
|
|
|
208
231
|
this.renderFlatComponent(element, template, type, name, componentName);
|
|
209
232
|
}
|
|
210
233
|
} catch (err) {
|
|
211
|
-
Util.
|
|
234
|
+
Util.customWarn(err, `replace-components.js: Error replacing ${name} in ${this.file.path}`);
|
|
212
235
|
}
|
|
213
236
|
}
|
|
214
237
|
|
|
@@ -223,15 +246,23 @@ class ReplaceComponents {
|
|
|
223
246
|
* @param {string} componentName - Component template filename
|
|
224
247
|
*/
|
|
225
248
|
renderDsdComponent(element, template, name, componentName) {
|
|
226
|
-
//
|
|
227
|
-
// (
|
|
228
|
-
const
|
|
229
|
-
|
|
249
|
+
// Idempotency: if the element already carries a declarative shadow
|
|
250
|
+
// template (already-processed output, e.g. a dev rebuild), skip it.
|
|
251
|
+
const hasShadowTemplate = Array.from(element.children).some(child =>
|
|
252
|
+
child.tagName && child.tagName.toLowerCase() === 'template'
|
|
253
|
+
&& child.hasAttribute('shadowrootmode')
|
|
230
254
|
);
|
|
231
|
-
if (
|
|
255
|
+
if (hasShadowTemplate) return;
|
|
256
|
+
|
|
257
|
+
// Extract the <template shadowrootmode="..."> content via a DOM parse.
|
|
258
|
+
// (The old regex required shadowrootmode to be the first attribute and
|
|
259
|
+
// truncated shadow content at the first nested </template>.)
|
|
260
|
+
const templateDom = Util.jsdom.dom({ src: template });
|
|
261
|
+
const shadowTemplate = templateDom.window.document.querySelector('template[shadowrootmode]');
|
|
262
|
+
if (!shadowTemplate) return;
|
|
232
263
|
|
|
233
|
-
const mode =
|
|
234
|
-
let shadowContent =
|
|
264
|
+
const mode = shadowTemplate.getAttribute('shadowrootmode') || 'open';
|
|
265
|
+
let shadowContent = shadowTemplate.innerHTML;
|
|
235
266
|
|
|
236
267
|
// Extract data from element attributes
|
|
237
268
|
const componentData = this.extractComponentData(element, 'custom-element');
|
|
@@ -286,11 +317,17 @@ class ReplaceComponents {
|
|
|
286
317
|
// Replace template variables with data (style-free HTML only)
|
|
287
318
|
const renderedContent = this.renderTemplate(htmlTemplate, mergedData);
|
|
288
319
|
|
|
320
|
+
// Remember the element's current next sibling so we can tell whether
|
|
321
|
+
// the rendered content actually starts with an element — if it starts
|
|
322
|
+
// with text, nextElementSibling would point at a pre-existing sibling
|
|
323
|
+
// that must NOT receive the component's attributes.
|
|
324
|
+
const priorNextEl = element.nextElementSibling;
|
|
325
|
+
|
|
289
326
|
// Insert rendered content and remove original element
|
|
290
327
|
element.insertAdjacentHTML('afterend', renderedContent);
|
|
291
328
|
|
|
292
329
|
// Transfer non-data attributes to the first valid replaced element
|
|
293
|
-
this.transferAttributes(element, type);
|
|
330
|
+
this.transferAttributes(element, type, priorNextEl);
|
|
294
331
|
|
|
295
332
|
// Remove the original component element
|
|
296
333
|
element.remove();
|
|
@@ -349,7 +386,12 @@ class ReplaceComponents {
|
|
|
349
386
|
|
|
350
387
|
for (const attr of element.attributes) {
|
|
351
388
|
const name = attr.name;
|
|
352
|
-
|
|
389
|
+
// Attribute values arrive DOM-decoded (" → ", < → <) but are
|
|
390
|
+
// re-injected into template HTML via insertAdjacentHTML. Re-escape
|
|
391
|
+
// them so quotes can't truncate attributes and encoded markup can't
|
|
392
|
+
// become live elements. (Slot content is real HTML and is NOT
|
|
393
|
+
// escaped — see extractSlots.)
|
|
394
|
+
const value = this.escapeHtml(attr.value);
|
|
353
395
|
|
|
354
396
|
// Skip data-component attribute itself
|
|
355
397
|
if (name === 'data-component') continue;
|
|
@@ -372,6 +414,19 @@ class ReplaceComponents {
|
|
|
372
414
|
return data;
|
|
373
415
|
}
|
|
374
416
|
|
|
417
|
+
/**
|
|
418
|
+
* Escape HTML-special characters for safe re-injection into template HTML.
|
|
419
|
+
* @param {string} str - Raw (DOM-decoded) attribute value
|
|
420
|
+
* @returns {string}
|
|
421
|
+
*/
|
|
422
|
+
escapeHtml(str) {
|
|
423
|
+
return str
|
|
424
|
+
.replace(/&/g, '&')
|
|
425
|
+
.replace(/</g, '<')
|
|
426
|
+
.replace(/>/g, '>')
|
|
427
|
+
.replace(/"/g, '"');
|
|
428
|
+
}
|
|
429
|
+
|
|
375
430
|
/**
|
|
376
431
|
* Extract named and default slot content from element children.
|
|
377
432
|
* Children with a slot="name" attribute provide named slot content.
|
|
@@ -457,51 +512,15 @@ class ReplaceComponents {
|
|
|
457
512
|
}
|
|
458
513
|
|
|
459
514
|
/**
|
|
460
|
-
* Render template by replacing variables with data
|
|
515
|
+
* Render template by replacing variables with data.
|
|
516
|
+
* Delegates to the shared interpolator (single ${var} semantics
|
|
517
|
+
* across all plugins); unfilled ${slot:x} placeholders are removed.
|
|
461
518
|
* @param {string} template - Template string
|
|
462
519
|
* @param {Object} data - Data object
|
|
463
520
|
* @returns {string} Rendered template
|
|
464
521
|
*/
|
|
465
522
|
renderTemplate(template, data) {
|
|
466
|
-
|
|
467
|
-
// Also replace ${varName} with data values
|
|
468
|
-
// Supports nested: ${user.name}
|
|
469
|
-
return template.replace(/\$\{([^}]+)\}/g, (match, key) => {
|
|
470
|
-
const trimmedKey = key.trim();
|
|
471
|
-
|
|
472
|
-
// Check data directly (handles "slot", "slot:name", and regular vars)
|
|
473
|
-
if (data[trimmedKey] !== undefined && data[trimmedKey] !== null) {
|
|
474
|
-
return data[trimmedKey];
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
// Handle nested properties (e.g., user.name) — but not slot:name (already checked)
|
|
478
|
-
if (trimmedKey.includes('.')) {
|
|
479
|
-
const value = this.getNestedValue(data, trimmedKey);
|
|
480
|
-
if (value !== undefined && value !== null) {
|
|
481
|
-
return value;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
// Named slot not provided — remove the placeholder (empty string)
|
|
486
|
-
if (trimmedKey.startsWith('slot:')) {
|
|
487
|
-
return '';
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
// Keep the original ${var} if not found (allows for later processing)
|
|
491
|
-
return match;
|
|
492
|
-
});
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
/**
|
|
496
|
-
* Get nested value from object using dot notation
|
|
497
|
-
* @param {Object} obj - Source object
|
|
498
|
-
* @param {string} path - Dot-notation path
|
|
499
|
-
* @returns {*} Value or undefined
|
|
500
|
-
*/
|
|
501
|
-
getNestedValue(obj, path) {
|
|
502
|
-
return path.split('.').reduce((current, key) => {
|
|
503
|
-
return current && current[key] !== undefined ? current[key] : undefined;
|
|
504
|
-
}, obj);
|
|
523
|
+
return interpolate(template, data, { removeUnmatchedSlots: true });
|
|
505
524
|
}
|
|
506
525
|
|
|
507
526
|
/**
|
|
@@ -509,9 +528,12 @@ class ReplaceComponents {
|
|
|
509
528
|
* @param {Element} originalElement - The original component element
|
|
510
529
|
* @param {string} type - Component type
|
|
511
530
|
*/
|
|
512
|
-
transferAttributes(originalElement, type) {
|
|
531
|
+
transferAttributes(originalElement, type, priorNextEl) {
|
|
513
532
|
const nextEl = originalElement.nextElementSibling;
|
|
514
533
|
if (!nextEl) return;
|
|
534
|
+
// Rendered content did not start with an element (text-only template):
|
|
535
|
+
// nextElementSibling is a pre-existing sibling — don't touch it.
|
|
536
|
+
if (nextEl === priorNextEl) return;
|
|
515
537
|
|
|
516
538
|
// Attributes to skip
|
|
517
539
|
const skipAttrs = ['data-component'];
|
|
@@ -63,7 +63,7 @@ class ReplaceExternalLinkProtocol {
|
|
|
63
63
|
const { file } = this;
|
|
64
64
|
|
|
65
65
|
// Make source traversable with JSDOM
|
|
66
|
-
|
|
66
|
+
const dom = Util.fileDom(file);
|
|
67
67
|
|
|
68
68
|
// Find <a>, <link>, and <script> tags
|
|
69
69
|
const $link = dom.window.document.querySelectorAll('link');
|
|
@@ -77,7 +77,7 @@ class ReplaceExternalLinkProtocol {
|
|
|
77
77
|
if ($script) $script.forEach(el => this.replaceMissingProtocol({file, el}));
|
|
78
78
|
|
|
79
79
|
// Store updated file source
|
|
80
|
-
|
|
80
|
+
Util.commitDom(file, dom);
|
|
81
81
|
|
|
82
82
|
// END LOGGING
|
|
83
83
|
this.endLog();
|
|
@@ -97,40 +97,25 @@ class ReplaceExternalLinkProtocol {
|
|
|
97
97
|
*/
|
|
98
98
|
replaceMissingProtocol({file, scope, el}) {
|
|
99
99
|
// Get source type (`href` or `src`) — use getAttribute for parser compatibility
|
|
100
|
-
|
|
100
|
+
const value = el.getAttribute('href') || el.getAttribute('src');
|
|
101
101
|
// Early Exit: Tag does not have `[href]` or `[src]` or attribute value is empty string
|
|
102
102
|
// Example: Someone adds an old-school anchor jump point: `<a id="jump-point"></a>`
|
|
103
|
-
if (!
|
|
104
|
-
//
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
if (!value) return;
|
|
104
|
+
// Early Exit: Already has a protocol/scheme (http:, https:, mailto:,
|
|
105
|
+
// tel:, data:, …) or is protocol-relative (//cdn.example.com)
|
|
106
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(value) || value.startsWith('//')) return;
|
|
107
|
+
// Only replace values starting with a target domain prefix followed
|
|
108
|
+
// by a dot (`www.`, `cdn.` by default, or user-defined via config).
|
|
109
|
+
// NOTE: linkedom returns raw attribute values (no URL resolution), so
|
|
110
|
+
// the check works directly on what the author wrote.
|
|
107
111
|
const domainTargets = replaceExternalLinkProtocol.match || Util.replaceExternalLinkProtocolDefaults;
|
|
108
|
-
const
|
|
109
|
-
|
|
112
|
+
const prefixPattern = new RegExp(`^(?:${domainTargets.join('|')})\\.`, 'i');
|
|
113
|
+
if (!prefixPattern.test(value)) return;
|
|
110
114
|
// Update path
|
|
111
|
-
|
|
115
|
+
const pathType = el.getAttribute('href') !== null ? 'href' : 'src';
|
|
116
|
+
el.setAttribute(pathType, `http://${value}`);
|
|
112
117
|
// Increment total counter
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* @description Create new, fixed external path
|
|
118
|
-
* @property {Object} file - The current file's props (ext,name,path,name)
|
|
119
|
-
* @property {Object} el - The current <a>, <link>, or <script> tag being evaluated
|
|
120
|
-
* @property {String} type - Either the element's `[href]` or `[src]` prop to write new value to
|
|
121
|
-
* @private
|
|
122
|
-
*/
|
|
123
|
-
replaceExternal(file, el, type) {
|
|
124
|
-
// Split path on /
|
|
125
|
-
let linkPathSplit = el[type].split('/');
|
|
126
|
-
// Filter out ''
|
|
127
|
-
linkPathSplit = linkPathSplit.filter(s => s);
|
|
128
|
-
// Find if 'localhost' is in the path name
|
|
129
|
-
// We'll only replace these, as they represent the links we want to convert
|
|
130
|
-
if (linkPathSplit.indexOf('localhost') === -1) return;
|
|
131
|
-
el[type] = `http://${linkPathSplit[linkPathSplit.length-1]}`;
|
|
132
|
-
// Show terminal message
|
|
133
|
-
// Logger.success(`/${file.path} - Added 'http://' to [href="${linkPathSplit[linkPathSplit.length-1]}"]: ${ chalk.green(el[type]) }`);
|
|
118
|
+
this.total += 1;
|
|
134
119
|
}
|
|
135
120
|
|
|
136
121
|
|
|
@@ -56,8 +56,8 @@ class ReplaceInclude {
|
|
|
56
56
|
if (!allowed) return;
|
|
57
57
|
|
|
58
58
|
// Early Exit: No includes in source
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
// \binclude=" matches both include="..." and data-include="..."
|
|
60
|
+
if (!/\binclude="|<include-file/i.test(this.file.src)) return;
|
|
61
61
|
|
|
62
62
|
// START LOGGING
|
|
63
63
|
this.startLog();
|
|
@@ -70,9 +70,10 @@ class ReplaceInclude {
|
|
|
70
70
|
changed = false;
|
|
71
71
|
|
|
72
72
|
// Match <include-file src="/path"></include-file>
|
|
73
|
+
// (src may appear after other attributes: <include-file lazy src="...">)
|
|
73
74
|
this.file.src = await this.replacePattern(
|
|
74
75
|
this.file.src,
|
|
75
|
-
/<include-file\
|
|
76
|
+
/<include-file\b[^>]*\bsrc="([^"]+)"[^>]*><\/include-file>/gi,
|
|
76
77
|
(match, srcPath) => srcPath,
|
|
77
78
|
() => { changed = true; }
|
|
78
79
|
);
|
|
@@ -127,9 +128,18 @@ class ReplaceInclude {
|
|
|
127
128
|
for (let i = matches.length - 1; i >= 0; i--) {
|
|
128
129
|
const match = matches[i];
|
|
129
130
|
const filePath = pathExtractor(...match);
|
|
130
|
-
|
|
131
|
+
let content;
|
|
132
|
+
try {
|
|
133
|
+
content = await this.getIncludeContent(filePath);
|
|
134
|
+
this.total++;
|
|
135
|
+
} catch (err) {
|
|
136
|
+
// A missing include must not abort the build: warn with the
|
|
137
|
+
// referencing page, and replace the marker with a diagnostic
|
|
138
|
+
// comment (leaving the raw marker would re-match forever).
|
|
139
|
+
console.warn(chalk.yellow(` [include] Not found: "${filePath}" referenced in ${this.file.path}`));
|
|
140
|
+
content = `<!-- cook:include-not-found ${filePath} -->`;
|
|
141
|
+
}
|
|
131
142
|
src = src.substring(0, match.index) + content + src.substring(match.index + match[0].length);
|
|
132
|
-
this.total++;
|
|
133
143
|
onReplace();
|
|
134
144
|
}
|
|
135
145
|
return src;
|
|
@@ -188,7 +198,7 @@ class ReplaceInclude {
|
|
|
188
198
|
el.outerHTML = content;
|
|
189
199
|
}
|
|
190
200
|
catch (err) {
|
|
191
|
-
Util.
|
|
201
|
+
Util.customWarn(err, `replace-includes.js: ${this.file.path}`);
|
|
192
202
|
}
|
|
193
203
|
}
|
|
194
204
|
}
|
|
@@ -206,23 +216,28 @@ class ReplaceInclude {
|
|
|
206
216
|
async fetchIncludeSrc(path) {
|
|
207
217
|
// Store raw path
|
|
208
218
|
let formattedIncludePath = path;
|
|
209
|
-
// Format the path before doing a file lookup, in case the user added
|
|
210
|
-
|
|
219
|
+
// Format the path before doing a file lookup, in case the user added
|
|
220
|
+
// a malformed path. Only a literal trailing `.html` counts — the old
|
|
221
|
+
// unescaped /.html/ matched any "html" substring in the path
|
|
222
|
+
// (e.g. a directory named html-partials).
|
|
223
|
+
const hasExtension = /\.html$/i.test(path);
|
|
211
224
|
// Include files are read from src/ where they are in their original form
|
|
212
225
|
// (not directory-converted), so just add .html if missing
|
|
213
226
|
if (!hasExtension) formattedIncludePath = `${path}.html`;
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
this.store.cachedIncludes[path] = fetchedSrc;
|
|
220
|
-
// Return source
|
|
221
|
-
return fetchedSrc;
|
|
222
|
-
}
|
|
223
|
-
catch (err) {
|
|
224
|
-
Util.customKill(`fetchInlineSrc(): ${err}`);
|
|
227
|
+
// The cache may be keyed by the formatted path (prior fetch or pre-seed)
|
|
228
|
+
const cached = this.store.cachedIncludes[formattedIncludePath];
|
|
229
|
+
if (cached !== undefined && cached !== null) {
|
|
230
|
+
this.store.cachedIncludes[path] = cached;
|
|
231
|
+
return cached;
|
|
225
232
|
}
|
|
233
|
+
// Get and return content of target include file.
|
|
234
|
+
// Read errors propagate to replacePattern, which warns with the
|
|
235
|
+
// referencing page and keeps the build alive.
|
|
236
|
+
const fetchedSrc = await fs.readFile(formattedIncludePath, 'utf-8');
|
|
237
|
+
// Add entry to cached object store
|
|
238
|
+
this.store.cachedIncludes[path] = fetchedSrc;
|
|
239
|
+
// Return source
|
|
240
|
+
return fetchedSrc;
|
|
226
241
|
}
|
|
227
242
|
|
|
228
243
|
|
|
@@ -55,7 +55,7 @@ class ReplaceInline {
|
|
|
55
55
|
const { file } = this;
|
|
56
56
|
|
|
57
57
|
// Make source traversable with JSDOM
|
|
58
|
-
|
|
58
|
+
const dom = Util.fileDom(file);
|
|
59
59
|
|
|
60
60
|
// Store all <link inline> and <scripts inline>
|
|
61
61
|
const inlineLinkSelector = Util.getSelector(Util.attr.inline, 'link');
|
|
@@ -73,7 +73,7 @@ class ReplaceInline {
|
|
|
73
73
|
await this.replaceScripts({scripts, file});
|
|
74
74
|
|
|
75
75
|
// Store updated file source
|
|
76
|
-
|
|
76
|
+
Util.commitDom(file, dom);
|
|
77
77
|
|
|
78
78
|
// END LOGGING
|
|
79
79
|
this.endLog();
|
|
@@ -158,7 +158,10 @@ class ReplaceInline {
|
|
|
158
158
|
return fetchedSrc;
|
|
159
159
|
}
|
|
160
160
|
catch (err) {
|
|
161
|
-
|
|
161
|
+
// Missing inline asset: warn with the referencing page and inline
|
|
162
|
+
// nothing rather than aborting the whole build
|
|
163
|
+
Util.customWarn(`Not found: "${path}" referenced in ${this.file.path}`, 'replace-inline');
|
|
164
|
+
return '';
|
|
162
165
|
}
|
|
163
166
|
}
|
|
164
167
|
|