cook-ssg 2.0.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 +445 -0
- package/bin/cook.js +66 -0
- package/package.json +75 -0
- package/patches/jsdom-fetch-fix.js +52 -0
- package/scripts/build.js +212 -0
- package/scripts/dev.js +119 -0
- package/scripts/plugins/apply-template.js +226 -0
- package/scripts/plugins/auto-components.js +215 -0
- package/scripts/plugins/bundle.js +338 -0
- package/scripts/plugins/copy-src.js +89 -0
- package/scripts/plugins/create-dir-from-file.js +144 -0
- package/scripts/plugins/create-dist.js +55 -0
- package/scripts/plugins/custom-plugins.js +118 -0
- package/scripts/plugins/declarative-shadow-dom.js +164 -0
- package/scripts/plugins/escape-code-blocks.js +54 -0
- package/scripts/plugins/generate-formats.js +361 -0
- package/scripts/plugins/generate-fragments.js +126 -0
- package/scripts/plugins/generate-sitemap-xml.js +216 -0
- package/scripts/plugins/minify-src.js +253 -0
- package/scripts/plugins/optimize-images.js +389 -0
- package/scripts/plugins/process-html-frontmatter.js +75 -0
- package/scripts/plugins/process-markdown.js +384 -0
- package/scripts/plugins/repeat-collection.js +296 -0
- package/scripts/plugins/replace-components.js +496 -0
- package/scripts/plugins/replace-external-link-protocol.js +171 -0
- package/scripts/plugins/replace-include.js +273 -0
- package/scripts/plugins/replace-inline.js +217 -0
- package/scripts/plugins/replace-template-strings.js +121 -0
- package/scripts/plugins/set-active-links.js +175 -0
- package/scripts/utils/config/config.js +277 -0
- package/scripts/utils/get-src/get-src.js +241 -0
- package/scripts/utils/logger/logger.js +47 -0
- package/scripts/utils/performance/performance.js +35 -0
- package/scripts/utils/spinner/spinner.js +112 -0
- package/scripts/utils/timer/timer.js +40 -0
- package/scripts/utils/util/util.js +719 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file repeat-collection.js
|
|
3
|
+
* @description Repeat elements for each item in a collection/array
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* <article data-repeat="collections.blog as post">
|
|
7
|
+
* <h2>${post.title}</h2>
|
|
8
|
+
* <a href="${post.url}">Read more</a>
|
|
9
|
+
* </article>
|
|
10
|
+
*
|
|
11
|
+
* Supports:
|
|
12
|
+
* - Nested property access: "collections.blog as post"
|
|
13
|
+
* - Array data: "items as item"
|
|
14
|
+
* - Index variable: "items as item, index"
|
|
15
|
+
* - Limit: "collections.blog | limit:5 as post"
|
|
16
|
+
* - Offset: "collections.blog | offset:2 as post"
|
|
17
|
+
* - Combined: "collections.blog | offset:2 | limit:5 as post"
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// IMPORTS
|
|
21
|
+
// -----------------------------
|
|
22
|
+
import chalk from 'chalk';
|
|
23
|
+
import Util from '../utils/util/util.js';
|
|
24
|
+
|
|
25
|
+
// Config
|
|
26
|
+
import { repeat as repeatConfig } from '../utils/config/config.js';
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
// DEFINE
|
|
30
|
+
// -----------------------------
|
|
31
|
+
/**
|
|
32
|
+
* @description Repeat elements for each item in a collection
|
|
33
|
+
* @param {Object} opts - Options object
|
|
34
|
+
* @property {Object} opts.file - Current file info
|
|
35
|
+
* @property {Object} opts.data - User data from config/data.js
|
|
36
|
+
* @property {Object} opts.store - Build process data store
|
|
37
|
+
* @property {Array} [opts.allowType] - Allowed file types
|
|
38
|
+
* @property {Array} [opts.disallowType] - Disallowed file types
|
|
39
|
+
*/
|
|
40
|
+
class RepeatCollection {
|
|
41
|
+
constructor({ file, data, store, allowType, disallowType }) {
|
|
42
|
+
this.opts = { file, allowType, disallowType };
|
|
43
|
+
this.file = file;
|
|
44
|
+
this.data = data;
|
|
45
|
+
this.store = store;
|
|
46
|
+
this.config = {
|
|
47
|
+
// Attribute name for repeat directive
|
|
48
|
+
attribute: 'data-repeat',
|
|
49
|
+
// Remove the data-repeat attribute from output
|
|
50
|
+
removeAttribute: true,
|
|
51
|
+
// User overrides
|
|
52
|
+
...repeatConfig,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Track total replacements for logging
|
|
56
|
+
this.total = 0;
|
|
57
|
+
|
|
58
|
+
// Init terminal logging
|
|
59
|
+
if (process.env.LOGGER) Util.initLogging.call(this);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// INIT
|
|
63
|
+
// -----------------------------
|
|
64
|
+
async init() {
|
|
65
|
+
// Early Exit: File type not allowed
|
|
66
|
+
const allowed = Util.isAllowedType(this.opts);
|
|
67
|
+
if (!allowed) return;
|
|
68
|
+
|
|
69
|
+
// Store string source as traversable DOM
|
|
70
|
+
const dom = Util.jsdom.dom({ src: this.file.src });
|
|
71
|
+
const document = dom.window.document;
|
|
72
|
+
|
|
73
|
+
// Find all elements with data-repeat attribute
|
|
74
|
+
const repeatElements = document.querySelectorAll(`[${this.config.attribute}]`);
|
|
75
|
+
this.total = repeatElements.length;
|
|
76
|
+
|
|
77
|
+
// Early Exit: No repeat elements
|
|
78
|
+
if (!repeatElements.length) return;
|
|
79
|
+
|
|
80
|
+
// Process each repeat element
|
|
81
|
+
for (const element of repeatElements) {
|
|
82
|
+
this.processRepeatElement(element, document);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Store updated file source
|
|
86
|
+
this.file.src = Util.setSrc({ dom });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Process a single repeat element
|
|
91
|
+
* @param {Element} element - The element with data-repeat
|
|
92
|
+
* @param {Document} document - The document object
|
|
93
|
+
*/
|
|
94
|
+
processRepeatElement(element, document) {
|
|
95
|
+
const expression = element.getAttribute(this.config.attribute);
|
|
96
|
+
if (!expression) return;
|
|
97
|
+
|
|
98
|
+
// Parse the repeat expression
|
|
99
|
+
const parsed = this.parseExpression(expression);
|
|
100
|
+
if (!parsed) {
|
|
101
|
+
console.log(chalk.yellow(` [repeat] Invalid expression: "${expression}"`));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Get the collection data
|
|
106
|
+
let collection = this.getNestedValue(this.data, parsed.collection);
|
|
107
|
+
if (!collection || !Array.isArray(collection)) {
|
|
108
|
+
// Try getting from store (for collections built during build)
|
|
109
|
+
collection = this.getNestedValue(this.store, parsed.collection);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!collection || !Array.isArray(collection)) {
|
|
113
|
+
console.log(chalk.yellow(` [repeat] Collection not found or not an array: "${parsed.collection}"`));
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Apply offset if specified
|
|
118
|
+
if (parsed.offset > 0) {
|
|
119
|
+
collection = collection.slice(parsed.offset);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Apply limit if specified
|
|
123
|
+
if (parsed.limit > 0) {
|
|
124
|
+
collection = collection.slice(0, parsed.limit);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Get the template HTML (inner content of the repeat element)
|
|
128
|
+
const template = element.innerHTML;
|
|
129
|
+
const tagName = element.tagName.toLowerCase();
|
|
130
|
+
|
|
131
|
+
// Create a document fragment to hold all repeated elements
|
|
132
|
+
const fragment = document.createDocumentFragment();
|
|
133
|
+
|
|
134
|
+
// Generate repeated elements
|
|
135
|
+
collection.forEach((item, index) => {
|
|
136
|
+
// Clone the original element
|
|
137
|
+
const clone = element.cloneNode(false);
|
|
138
|
+
|
|
139
|
+
// Remove the data-repeat attribute from clone
|
|
140
|
+
if (this.config.removeAttribute) {
|
|
141
|
+
clone.removeAttribute(this.config.attribute);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Build the data context for this iteration
|
|
145
|
+
const context = {
|
|
146
|
+
[parsed.itemName]: item,
|
|
147
|
+
...item, // Also spread item properties for direct access
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Add index if requested
|
|
151
|
+
if (parsed.indexName) {
|
|
152
|
+
context[parsed.indexName] = index;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Replace template variables in the content
|
|
156
|
+
const renderedContent = this.renderTemplate(template, context);
|
|
157
|
+
clone.innerHTML = renderedContent;
|
|
158
|
+
|
|
159
|
+
// Also process attributes on the clone itself
|
|
160
|
+
this.processElementAttributes(clone, context);
|
|
161
|
+
|
|
162
|
+
fragment.appendChild(clone);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Replace original element with the fragment
|
|
166
|
+
element.parentNode.replaceChild(fragment, element);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Parse the repeat expression
|
|
171
|
+
* Supports: "collection as item" or "collection as item, index"
|
|
172
|
+
* With modifiers: "collection | limit:5 | offset:2 as item"
|
|
173
|
+
* @param {string} expression - The data-repeat expression
|
|
174
|
+
* @returns {Object|null} Parsed expression or null if invalid
|
|
175
|
+
*/
|
|
176
|
+
parseExpression(expression) {
|
|
177
|
+
// Match: "collectionPath | modifiers as itemName, indexName"
|
|
178
|
+
const match = expression.match(/^(.+?)\s+as\s+(\w+)(?:\s*,\s*(\w+))?$/);
|
|
179
|
+
if (!match) return null;
|
|
180
|
+
|
|
181
|
+
let collectionPart = match[1].trim();
|
|
182
|
+
const itemName = match[2];
|
|
183
|
+
const indexName = match[3] || null;
|
|
184
|
+
|
|
185
|
+
// Parse modifiers (limit, offset)
|
|
186
|
+
let limit = 0;
|
|
187
|
+
let offset = 0;
|
|
188
|
+
|
|
189
|
+
// Check for pipe modifiers
|
|
190
|
+
const parts = collectionPart.split('|').map(p => p.trim());
|
|
191
|
+
const collection = parts[0];
|
|
192
|
+
|
|
193
|
+
for (let i = 1; i < parts.length; i++) {
|
|
194
|
+
const modifier = parts[i];
|
|
195
|
+
|
|
196
|
+
// Parse limit:N
|
|
197
|
+
const limitMatch = modifier.match(/^limit\s*:\s*(\d+)$/);
|
|
198
|
+
if (limitMatch) {
|
|
199
|
+
limit = parseInt(limitMatch[1], 10);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Parse offset:N
|
|
204
|
+
const offsetMatch = modifier.match(/^offset\s*:\s*(\d+)$/);
|
|
205
|
+
if (offsetMatch) {
|
|
206
|
+
offset = parseInt(offsetMatch[1], 10);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
collection,
|
|
213
|
+
itemName,
|
|
214
|
+
indexName,
|
|
215
|
+
limit,
|
|
216
|
+
offset,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Get a nested property value from an object
|
|
222
|
+
* @param {Object} obj - The object to search
|
|
223
|
+
* @param {string} path - Dot-notation path (e.g., "collections.blog")
|
|
224
|
+
* @returns {*} The value or undefined
|
|
225
|
+
*/
|
|
226
|
+
getNestedValue(obj, path) {
|
|
227
|
+
if (!obj || !path) return undefined;
|
|
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;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Render template string with data context
|
|
244
|
+
* Replaces ${varName} and ${item.property} with values
|
|
245
|
+
* @param {string} template - Template string
|
|
246
|
+
* @param {Object} context - Data context
|
|
247
|
+
* @returns {string} Rendered string
|
|
248
|
+
*/
|
|
249
|
+
renderTemplate(template, context) {
|
|
250
|
+
return template.replace(/\$\{([^}]+)\}/g, (match, key) => {
|
|
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
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Process and replace template variables in element attributes
|
|
273
|
+
* @param {Element} element - The element to process
|
|
274
|
+
* @param {Object} context - Data context
|
|
275
|
+
*/
|
|
276
|
+
processElementAttributes(element, context) {
|
|
277
|
+
const attributes = element.attributes;
|
|
278
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
279
|
+
const attr = attributes[i];
|
|
280
|
+
if (attr.value.includes('${')) {
|
|
281
|
+
attr.value = this.renderTemplate(attr.value, context);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// EXPORT WRAPPER
|
|
287
|
+
// -----------------------------
|
|
288
|
+
static async export(opts) {
|
|
289
|
+
return new RepeatCollection(opts).init();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
// EXPORT
|
|
295
|
+
// -----------------------------
|
|
296
|
+
export default RepeatCollection.export;
|