ecto 4.8.2 → 4.8.4
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 +31 -64
- package/dist/ecto.d.mts +403 -0
- package/dist/ecto.mjs +863 -0
- package/package.json +27 -25
- package/dist/ecto.d.ts +0 -400
- package/dist/ecto.js +0 -1042
package/dist/ecto.js
DELETED
|
@@ -1,1042 +0,0 @@
|
|
|
1
|
-
// src/ecto.ts
|
|
2
|
-
import fs2 from "fs";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { Cacheable, CacheableMemory } from "cacheable";
|
|
5
|
-
import { Hookified } from "hookified";
|
|
6
|
-
import { Writr as Writr2 } from "writr";
|
|
7
|
-
|
|
8
|
-
// src/engine-map.ts
|
|
9
|
-
var EngineMap = class {
|
|
10
|
-
_mappings = /* @__PURE__ */ new Map();
|
|
11
|
-
set(name, extensions) {
|
|
12
|
-
const engineName = name.trim().toLowerCase();
|
|
13
|
-
const engineExtensions = [];
|
|
14
|
-
if (extensions.length > 0 && name !== "") {
|
|
15
|
-
for (let extension of extensions) {
|
|
16
|
-
extension = extension.trim().toLowerCase();
|
|
17
|
-
if (!engineExtensions.includes(extension)) {
|
|
18
|
-
engineExtensions.push(extension);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
this._mappings.set(engineName, engineExtensions);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
delete(name) {
|
|
25
|
-
this._mappings.delete(name.trim().toLowerCase());
|
|
26
|
-
}
|
|
27
|
-
deleteExtension(name, extension) {
|
|
28
|
-
const engineName = name.trim().toLowerCase();
|
|
29
|
-
const extensions = this._mappings.get(engineName);
|
|
30
|
-
if (extensions) {
|
|
31
|
-
const engineExtensions = [];
|
|
32
|
-
for (const extension_ of extensions) {
|
|
33
|
-
if (extension_ !== extension.trim().toLowerCase()) {
|
|
34
|
-
engineExtensions.push(extension_);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
this._mappings.set(engineName, engineExtensions);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
get(name) {
|
|
41
|
-
return this._mappings.get(name.trim().toLowerCase());
|
|
42
|
-
}
|
|
43
|
-
getName(extension) {
|
|
44
|
-
let engineName;
|
|
45
|
-
for (const [name, extensions] of this._mappings) {
|
|
46
|
-
if (extensions.includes(extension.trim().toLowerCase())) {
|
|
47
|
-
engineName = name;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return engineName;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// src/engines/ejs.ts
|
|
55
|
-
import ejs from "ejs";
|
|
56
|
-
|
|
57
|
-
// src/base-engine.ts
|
|
58
|
-
var BaseEngine = class {
|
|
59
|
-
names = [];
|
|
60
|
-
opts = void 0;
|
|
61
|
-
// biome-ignore lint/suspicious/noExplicitAny: Different engines use different types, any is required for flexibility
|
|
62
|
-
engine;
|
|
63
|
-
rootTemplatePath = void 0;
|
|
64
|
-
_extensions = [];
|
|
65
|
-
getExtensions() {
|
|
66
|
-
return this._extensions;
|
|
67
|
-
}
|
|
68
|
-
setExtensions(extensions) {
|
|
69
|
-
this._extensions = [];
|
|
70
|
-
for (const extension of extensions) {
|
|
71
|
-
const newExtension = extension.trim().toLowerCase();
|
|
72
|
-
if (!this._extensions.includes(newExtension)) {
|
|
73
|
-
this._extensions.push(newExtension);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
deleteExtension(name) {
|
|
78
|
-
for (const [index, extension] of this._extensions.entries()) {
|
|
79
|
-
if (name.toLowerCase().trim() === extension) {
|
|
80
|
-
this._extensions.splice(index, 1);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
// src/engines/ejs.ts
|
|
87
|
-
var EJS = class extends BaseEngine {
|
|
88
|
-
constructor(options) {
|
|
89
|
-
super();
|
|
90
|
-
this.names = ["ejs"];
|
|
91
|
-
if (options) {
|
|
92
|
-
this.opts = options;
|
|
93
|
-
}
|
|
94
|
-
this.setExtensions(["ejs"]);
|
|
95
|
-
}
|
|
96
|
-
async render(source, data) {
|
|
97
|
-
this.engine ??= ejs;
|
|
98
|
-
this.opts ??= {};
|
|
99
|
-
if (this.rootTemplatePath) {
|
|
100
|
-
this.opts.root = this.rootTemplatePath;
|
|
101
|
-
}
|
|
102
|
-
return ejs.render(source, data, this.opts);
|
|
103
|
-
}
|
|
104
|
-
renderSync(source, data) {
|
|
105
|
-
this.engine ??= ejs;
|
|
106
|
-
this.opts ??= {};
|
|
107
|
-
if (this.rootTemplatePath) {
|
|
108
|
-
this.opts.root = this.rootTemplatePath;
|
|
109
|
-
}
|
|
110
|
-
return ejs.render(source, data, this.opts);
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
// src/engines/handlebars.ts
|
|
115
|
-
import fs from "fs";
|
|
116
|
-
import { fumanchu } from "@jaredwray/fumanchu";
|
|
117
|
-
import { decode } from "ent";
|
|
118
|
-
var Handlebars = class extends BaseEngine {
|
|
119
|
-
partialsPath = ["partials", "includes", "templates"];
|
|
120
|
-
constructor(options) {
|
|
121
|
-
super();
|
|
122
|
-
this.names = ["handlebars", "mustache"];
|
|
123
|
-
this.opts = options;
|
|
124
|
-
this.engine = fumanchu();
|
|
125
|
-
this.setExtensions(["hbs", "hjs", "handlebars", "mustache"]);
|
|
126
|
-
}
|
|
127
|
-
async render(source, data) {
|
|
128
|
-
if (this.rootTemplatePath) {
|
|
129
|
-
this.initPartials();
|
|
130
|
-
}
|
|
131
|
-
const template = this.engine.compile(source, this.opts);
|
|
132
|
-
let result = template(data, this.opts);
|
|
133
|
-
result = decode(result);
|
|
134
|
-
return result;
|
|
135
|
-
}
|
|
136
|
-
renderSync(source, data) {
|
|
137
|
-
if (this.rootTemplatePath) {
|
|
138
|
-
this.initPartials();
|
|
139
|
-
}
|
|
140
|
-
const template = this.engine.compile(source, this.opts);
|
|
141
|
-
let result = template(data, this.opts);
|
|
142
|
-
result = decode(result);
|
|
143
|
-
return result;
|
|
144
|
-
}
|
|
145
|
-
initPartials() {
|
|
146
|
-
for (const path2 of this.partialsPath) {
|
|
147
|
-
const fullPath = `${this.rootTemplatePath}/${path2}`;
|
|
148
|
-
this.registerPartials(fullPath);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
registerPartials(partialsPath) {
|
|
152
|
-
let result = false;
|
|
153
|
-
if (fs.existsSync(partialsPath)) {
|
|
154
|
-
const partials = fs.readdirSync(partialsPath, {
|
|
155
|
-
recursive: true,
|
|
156
|
-
encoding: "utf8"
|
|
157
|
-
});
|
|
158
|
-
for (const p of partials) {
|
|
159
|
-
if (fs.statSync(`${partialsPath}/${p}`).isDirectory()) {
|
|
160
|
-
const directoryPartials = fs.readdirSync(`${partialsPath}/${p}`, {
|
|
161
|
-
recursive: true,
|
|
162
|
-
encoding: "utf8"
|
|
163
|
-
});
|
|
164
|
-
for (const dp of directoryPartials) {
|
|
165
|
-
const source = fs.readFileSync(`${partialsPath}/${p}/${dp}`).toString();
|
|
166
|
-
const name = `${p}/${dp.split(".")[0]}`;
|
|
167
|
-
this.engine.registerPartial(name, this.engine.compile(source));
|
|
168
|
-
}
|
|
169
|
-
} else {
|
|
170
|
-
const source = fs.readFileSync(`${partialsPath}/${p}`, "utf8");
|
|
171
|
-
const name = p.split(".")[0];
|
|
172
|
-
this.engine.registerPartial(name, this.engine.compile(source));
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
result = true;
|
|
176
|
-
}
|
|
177
|
-
return result;
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
// src/engines/liquid.ts
|
|
182
|
-
import { Liquid as LiquidEngine } from "liquidjs";
|
|
183
|
-
var Liquid = class extends BaseEngine {
|
|
184
|
-
constructor(options) {
|
|
185
|
-
super();
|
|
186
|
-
this.names = ["liquid"];
|
|
187
|
-
if (options) {
|
|
188
|
-
this.opts = options;
|
|
189
|
-
}
|
|
190
|
-
this.setExtensions(["liquid"]);
|
|
191
|
-
}
|
|
192
|
-
async render(source, data) {
|
|
193
|
-
if (this.rootTemplatePath) {
|
|
194
|
-
this.opts ??= {};
|
|
195
|
-
this.opts.root = this.rootTemplatePath;
|
|
196
|
-
}
|
|
197
|
-
if (!this.engine) {
|
|
198
|
-
this.engine = new LiquidEngine(this.opts);
|
|
199
|
-
}
|
|
200
|
-
return this.engine.parseAndRender(source, data);
|
|
201
|
-
}
|
|
202
|
-
renderSync(source, data) {
|
|
203
|
-
if (this.rootTemplatePath) {
|
|
204
|
-
this.opts ??= {};
|
|
205
|
-
this.opts.root = this.rootTemplatePath;
|
|
206
|
-
}
|
|
207
|
-
if (!this.engine) {
|
|
208
|
-
this.engine = new LiquidEngine(this.opts);
|
|
209
|
-
}
|
|
210
|
-
return this.engine.parseAndRenderSync(source, data);
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
// src/engines/markdown.ts
|
|
215
|
-
import { Writr } from "writr";
|
|
216
|
-
var Markdown = class extends BaseEngine {
|
|
217
|
-
constructor(options) {
|
|
218
|
-
super();
|
|
219
|
-
this.names = ["markdown"];
|
|
220
|
-
if (options) {
|
|
221
|
-
this.opts = options;
|
|
222
|
-
}
|
|
223
|
-
this.engine = new Writr(options);
|
|
224
|
-
this.setExtensions(["md", "markdown"]);
|
|
225
|
-
}
|
|
226
|
-
async render(source, data) {
|
|
227
|
-
this.engine.content = source;
|
|
228
|
-
return this.engine.render(data);
|
|
229
|
-
}
|
|
230
|
-
renderSync(source, data) {
|
|
231
|
-
this.engine.content = source;
|
|
232
|
-
return this.engine.renderSync(data);
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
// src/engines/nunjucks.ts
|
|
237
|
-
import * as nunjucks from "nunjucks";
|
|
238
|
-
var Nunjucks = class extends BaseEngine {
|
|
239
|
-
constructor(options) {
|
|
240
|
-
super();
|
|
241
|
-
this.names = ["nunjucks"];
|
|
242
|
-
this.engine = nunjucks;
|
|
243
|
-
this.opts = { autoescape: true };
|
|
244
|
-
if (options) {
|
|
245
|
-
this.opts = options;
|
|
246
|
-
}
|
|
247
|
-
this.setExtensions(["njk"]);
|
|
248
|
-
}
|
|
249
|
-
async render(source, data) {
|
|
250
|
-
if (this.rootTemplatePath) {
|
|
251
|
-
nunjucks.configure(
|
|
252
|
-
this.rootTemplatePath,
|
|
253
|
-
this.opts
|
|
254
|
-
);
|
|
255
|
-
} else {
|
|
256
|
-
nunjucks.configure(this.opts);
|
|
257
|
-
}
|
|
258
|
-
data ??= {};
|
|
259
|
-
return nunjucks.renderString(source, data);
|
|
260
|
-
}
|
|
261
|
-
renderSync(source, data) {
|
|
262
|
-
if (this.rootTemplatePath) {
|
|
263
|
-
nunjucks.configure(
|
|
264
|
-
this.rootTemplatePath,
|
|
265
|
-
this.opts
|
|
266
|
-
);
|
|
267
|
-
} else {
|
|
268
|
-
nunjucks.configure(this.opts);
|
|
269
|
-
}
|
|
270
|
-
data ??= {};
|
|
271
|
-
return nunjucks.renderString(source, data);
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
// src/engines/pug.ts
|
|
276
|
-
import * as pug from "pug";
|
|
277
|
-
var Pug = class extends BaseEngine {
|
|
278
|
-
constructor(options) {
|
|
279
|
-
super();
|
|
280
|
-
this.names = ["pug"];
|
|
281
|
-
this.engine = pug;
|
|
282
|
-
if (options) {
|
|
283
|
-
this.opts = options;
|
|
284
|
-
}
|
|
285
|
-
this.setExtensions(["pug", "jade"]);
|
|
286
|
-
}
|
|
287
|
-
async render(source, data) {
|
|
288
|
-
if (this.rootTemplatePath) {
|
|
289
|
-
this.opts ??= {};
|
|
290
|
-
this.opts.basedir = this.rootTemplatePath;
|
|
291
|
-
}
|
|
292
|
-
const template = pug.compile(source, this.opts);
|
|
293
|
-
return template(data);
|
|
294
|
-
}
|
|
295
|
-
renderSync(source, data) {
|
|
296
|
-
if (this.rootTemplatePath) {
|
|
297
|
-
this.opts ??= {};
|
|
298
|
-
this.opts.basedir = this.rootTemplatePath;
|
|
299
|
-
}
|
|
300
|
-
const template = pug.compile(source, this.opts);
|
|
301
|
-
return template(data);
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
// src/ecto.ts
|
|
306
|
-
var EctoEvents = /* @__PURE__ */ ((EctoEvents2) => {
|
|
307
|
-
EctoEvents2["cacheHit"] = "cacheHit";
|
|
308
|
-
EctoEvents2["cacheMiss"] = "cacheMiss";
|
|
309
|
-
EctoEvents2["warn"] = "warn";
|
|
310
|
-
EctoEvents2["error"] = "error";
|
|
311
|
-
EctoEvents2["beforeRender"] = "beforeRender";
|
|
312
|
-
EctoEvents2["afterRender"] = "afterRender";
|
|
313
|
-
EctoEvents2["beforeRenderSync"] = "beforeRenderSync";
|
|
314
|
-
EctoEvents2["afterRenderSync"] = "afterRenderSync";
|
|
315
|
-
return EctoEvents2;
|
|
316
|
-
})(EctoEvents || {});
|
|
317
|
-
var Ecto = class extends Hookified {
|
|
318
|
-
_mapping = new EngineMap();
|
|
319
|
-
_engines = [];
|
|
320
|
-
// Cacheable instance for caching rendered templates
|
|
321
|
-
_cache;
|
|
322
|
-
_cacheSync;
|
|
323
|
-
_defaultEngine = "ejs";
|
|
324
|
-
// Engines
|
|
325
|
-
_ejs;
|
|
326
|
-
_markdown;
|
|
327
|
-
_pug;
|
|
328
|
-
_nunjucks;
|
|
329
|
-
_handlebars;
|
|
330
|
-
_liquid;
|
|
331
|
-
/**
|
|
332
|
-
* Ecto constructor
|
|
333
|
-
* @param {EctoOptions} [options] - The options for the ecto engine
|
|
334
|
-
*/
|
|
335
|
-
constructor(options) {
|
|
336
|
-
super();
|
|
337
|
-
this._ejs = new EJS(options?.engineOptions?.ejs);
|
|
338
|
-
this._markdown = new Markdown(options?.engineOptions?.markdown);
|
|
339
|
-
this._pug = new Pug(options?.engineOptions?.pug);
|
|
340
|
-
this._nunjucks = new Nunjucks(options?.engineOptions?.nunjucks);
|
|
341
|
-
this._handlebars = new Handlebars(options?.engineOptions?.handlebars);
|
|
342
|
-
this._liquid = new Liquid(options?.engineOptions?.liquid);
|
|
343
|
-
this._engines.push(
|
|
344
|
-
this._ejs,
|
|
345
|
-
this._markdown,
|
|
346
|
-
this._pug,
|
|
347
|
-
this._nunjucks,
|
|
348
|
-
this._handlebars,
|
|
349
|
-
this._liquid
|
|
350
|
-
);
|
|
351
|
-
this.registerEngineMappings();
|
|
352
|
-
if (options?.cache === true) {
|
|
353
|
-
this._cache = new Cacheable();
|
|
354
|
-
} else if (options?.cache instanceof Cacheable) {
|
|
355
|
-
this._cache = options.cache;
|
|
356
|
-
}
|
|
357
|
-
if (options?.cacheSync === true) {
|
|
358
|
-
this._cacheSync = new CacheableMemory();
|
|
359
|
-
} else if (options?.cacheSync instanceof CacheableMemory) {
|
|
360
|
-
this._cacheSync = options.cacheSync;
|
|
361
|
-
}
|
|
362
|
-
if (options?.defaultEngine && this.isValidEngine(options.defaultEngine)) {
|
|
363
|
-
this._defaultEngine = options.defaultEngine;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* Get the default engine
|
|
368
|
-
* @returns {string} - the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
|
|
369
|
-
*/
|
|
370
|
-
get defaultEngine() {
|
|
371
|
-
return this._defaultEngine;
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Set the default engine
|
|
375
|
-
* @param {string} value the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
|
|
376
|
-
*/
|
|
377
|
-
set defaultEngine(value) {
|
|
378
|
-
value = value.toLowerCase().trim();
|
|
379
|
-
if (this.isValidEngine(value)) {
|
|
380
|
-
this._defaultEngine = value;
|
|
381
|
-
} else {
|
|
382
|
-
this.emit(
|
|
383
|
-
"warn" /* warn */,
|
|
384
|
-
`Invalid engine name: ${value}. Defaulting to ${this._defaultEngine}.`
|
|
385
|
-
);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Get the cacheable instance
|
|
390
|
-
* @returns {Cacheable | undefined} - The cacheable instance or undefined if caching is disabled
|
|
391
|
-
*/
|
|
392
|
-
get cache() {
|
|
393
|
-
return this._cache;
|
|
394
|
-
}
|
|
395
|
-
/**
|
|
396
|
-
* Set the cacheable instance
|
|
397
|
-
* @param {Cacheable | undefined} value - The cacheable instance to set. If set to undefined, caching will be disabled.
|
|
398
|
-
*/
|
|
399
|
-
set cache(value) {
|
|
400
|
-
this._cache = value;
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Get the cacheable memory instance
|
|
404
|
-
* @returns {CacheableMemory | undefined} - The cacheable memory instance or undefined if caching is disabled
|
|
405
|
-
*/
|
|
406
|
-
get cacheSync() {
|
|
407
|
-
return this._cacheSync;
|
|
408
|
-
}
|
|
409
|
-
/**
|
|
410
|
-
* Set the cacheable memory instance
|
|
411
|
-
* @param {CacheableMemory | undefined} value - The cacheable memory instance to set. If set to undefined, caching will be disabled.
|
|
412
|
-
*/
|
|
413
|
-
set cacheSync(value) {
|
|
414
|
-
this._cacheSync = value;
|
|
415
|
-
}
|
|
416
|
-
/**
|
|
417
|
-
* Get the Engine Mappings. This is used to map file extensions to engines
|
|
418
|
-
* @returns {EngineMap}
|
|
419
|
-
*/
|
|
420
|
-
get mappings() {
|
|
421
|
-
return this._mapping;
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Get the EJS Engine
|
|
425
|
-
* @returns {EJS}
|
|
426
|
-
*/
|
|
427
|
-
get ejs() {
|
|
428
|
-
return this._ejs;
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Get the Markdown Engine
|
|
432
|
-
* @returns {Markdown}
|
|
433
|
-
*/
|
|
434
|
-
get markdown() {
|
|
435
|
-
return this._markdown;
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Get the Pug Engine
|
|
439
|
-
* @returns {Pug}
|
|
440
|
-
*/
|
|
441
|
-
get pug() {
|
|
442
|
-
return this._pug;
|
|
443
|
-
}
|
|
444
|
-
/**
|
|
445
|
-
* Get the Nunjucks Engine
|
|
446
|
-
* @returns {Nunjucks}
|
|
447
|
-
*/
|
|
448
|
-
get nunjucks() {
|
|
449
|
-
return this._nunjucks;
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Get the Handlebars Engine
|
|
453
|
-
* @returns {Handlebars}
|
|
454
|
-
*/
|
|
455
|
-
get handlebars() {
|
|
456
|
-
return this._handlebars;
|
|
457
|
-
}
|
|
458
|
-
/**
|
|
459
|
-
* Get the Liquid Engine
|
|
460
|
-
* @returns {Liquid}
|
|
461
|
-
*/
|
|
462
|
-
get liquid() {
|
|
463
|
-
return this._liquid;
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Asynchronously render a template source with data using the specified engine
|
|
467
|
-
* @param {string} source - The template source string to render
|
|
468
|
-
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
469
|
-
* @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
|
|
470
|
-
* @param {string} [rootTemplatePath] - Root directory path for template includes/partials resolution
|
|
471
|
-
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
472
|
-
* @returns {Promise<string>} The rendered template output as a string
|
|
473
|
-
* @example
|
|
474
|
-
* const result = await ecto.render('<%= name %>', { name: 'World' }, 'ejs');
|
|
475
|
-
*/
|
|
476
|
-
async render(source, data, engineName, rootTemplatePath, filePathOutput) {
|
|
477
|
-
try {
|
|
478
|
-
let renderEngineName = this._defaultEngine;
|
|
479
|
-
if (this.isValidEngine(engineName) && engineName !== void 0) {
|
|
480
|
-
renderEngineName = engineName;
|
|
481
|
-
}
|
|
482
|
-
const cacheKey = `${renderEngineName}-${source}-${JSON.stringify(data)}`;
|
|
483
|
-
if (this._cache) {
|
|
484
|
-
const cachedResult = await this._cache.get(cacheKey);
|
|
485
|
-
if (cachedResult) {
|
|
486
|
-
this.emit("cacheHit" /* cacheHit */, `Cache hit for key: ${cacheKey}`);
|
|
487
|
-
const context2 = {
|
|
488
|
-
source,
|
|
489
|
-
data,
|
|
490
|
-
engineName: renderEngineName,
|
|
491
|
-
rootTemplatePath,
|
|
492
|
-
filePathOutput,
|
|
493
|
-
cached: true
|
|
494
|
-
};
|
|
495
|
-
await this.hook("beforeRender" /* beforeRender */, context2);
|
|
496
|
-
const renderResult2 = { result: cachedResult, context: context2 };
|
|
497
|
-
await this.hook("afterRender" /* afterRender */, renderResult2);
|
|
498
|
-
await this.writeFile(filePathOutput, renderResult2.result);
|
|
499
|
-
return renderResult2.result;
|
|
500
|
-
}
|
|
501
|
-
this.emit("cacheMiss" /* cacheMiss */, `Cache miss for key: ${cacheKey}`);
|
|
502
|
-
}
|
|
503
|
-
const context = {
|
|
504
|
-
source,
|
|
505
|
-
data,
|
|
506
|
-
engineName: renderEngineName,
|
|
507
|
-
rootTemplatePath,
|
|
508
|
-
filePathOutput,
|
|
509
|
-
cached: false
|
|
510
|
-
};
|
|
511
|
-
await this.hook("beforeRender" /* beforeRender */, context);
|
|
512
|
-
const renderEngine = this.getRenderEngine(renderEngineName);
|
|
513
|
-
renderEngine.rootTemplatePath = context.rootTemplatePath;
|
|
514
|
-
let result = await renderEngine.render(context.source, context.data);
|
|
515
|
-
const renderResult = { result, context };
|
|
516
|
-
await this.hook("afterRender" /* afterRender */, renderResult);
|
|
517
|
-
result = renderResult.result;
|
|
518
|
-
if (this._cache) {
|
|
519
|
-
await this._cache.set(cacheKey, result);
|
|
520
|
-
}
|
|
521
|
-
await this.writeFile(filePathOutput, result);
|
|
522
|
-
return result;
|
|
523
|
-
} catch (error) {
|
|
524
|
-
this.emit("error" /* error */, error);
|
|
525
|
-
return "";
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
/**
|
|
529
|
-
* Synchronously render a template source with data using the specified engine
|
|
530
|
-
* @param {string} source - The template source string to render
|
|
531
|
-
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
532
|
-
* @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
|
|
533
|
-
* @param {string} [rootTemplatePath] - Root directory path for template includes/partials resolution
|
|
534
|
-
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
535
|
-
* @returns {string} The rendered template output as a string
|
|
536
|
-
* @example
|
|
537
|
-
* const result = ecto.renderSync('<%= name %>', { name: 'World' }, 'ejs');
|
|
538
|
-
*/
|
|
539
|
-
renderSync(source, data, engineName, rootTemplatePath, filePathOutput) {
|
|
540
|
-
try {
|
|
541
|
-
let renderEngineName = this._defaultEngine;
|
|
542
|
-
if (this.isValidEngine(engineName) && engineName !== void 0) {
|
|
543
|
-
renderEngineName = engineName;
|
|
544
|
-
}
|
|
545
|
-
const cacheKey = `${renderEngineName}-${source}-${JSON.stringify(data)}`;
|
|
546
|
-
if (this._cacheSync) {
|
|
547
|
-
const cachedResult = this._cacheSync.get(cacheKey);
|
|
548
|
-
if (cachedResult) {
|
|
549
|
-
this.emit("cacheHit" /* cacheHit */, `Cache hit for key: ${cacheKey}`);
|
|
550
|
-
const context2 = {
|
|
551
|
-
source,
|
|
552
|
-
data,
|
|
553
|
-
engineName: renderEngineName,
|
|
554
|
-
rootTemplatePath,
|
|
555
|
-
filePathOutput,
|
|
556
|
-
cached: true
|
|
557
|
-
};
|
|
558
|
-
this.runHooksSync("beforeRenderSync" /* beforeRenderSync */, context2);
|
|
559
|
-
const renderResult2 = { result: cachedResult, context: context2 };
|
|
560
|
-
this.runHooksSync("afterRenderSync" /* afterRenderSync */, renderResult2);
|
|
561
|
-
this.writeFileSync(filePathOutput, renderResult2.result);
|
|
562
|
-
return renderResult2.result;
|
|
563
|
-
}
|
|
564
|
-
this.emit("cacheMiss" /* cacheMiss */, `Cache miss for key: ${cacheKey}`);
|
|
565
|
-
}
|
|
566
|
-
const context = {
|
|
567
|
-
source,
|
|
568
|
-
data,
|
|
569
|
-
engineName: renderEngineName,
|
|
570
|
-
rootTemplatePath,
|
|
571
|
-
filePathOutput,
|
|
572
|
-
cached: false
|
|
573
|
-
};
|
|
574
|
-
this.runHooksSync("beforeRenderSync" /* beforeRenderSync */, context);
|
|
575
|
-
const renderEngine = this.getRenderEngine(renderEngineName);
|
|
576
|
-
renderEngine.rootTemplatePath = context.rootTemplatePath;
|
|
577
|
-
let result = renderEngine.renderSync(context.source, context.data);
|
|
578
|
-
const renderResult = { result, context };
|
|
579
|
-
this.runHooksSync("afterRenderSync" /* afterRenderSync */, renderResult);
|
|
580
|
-
result = renderResult.result;
|
|
581
|
-
if (this._cacheSync) {
|
|
582
|
-
this._cacheSync.set(cacheKey, result);
|
|
583
|
-
}
|
|
584
|
-
this.writeFileSync(filePathOutput, result);
|
|
585
|
-
return result;
|
|
586
|
-
} catch (error) {
|
|
587
|
-
this.emit("error" /* error */, error);
|
|
588
|
-
return "";
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
/**
|
|
592
|
-
* Run hooks synchronously for a specific event
|
|
593
|
-
* @param {string} event - The event name
|
|
594
|
-
* @param {unknown[]} args - Arguments to pass to the hooks
|
|
595
|
-
* @private
|
|
596
|
-
*/
|
|
597
|
-
runHooksSync(event, ...args) {
|
|
598
|
-
const eventHooks = this.hooks.get(event);
|
|
599
|
-
if (eventHooks) {
|
|
600
|
-
for (const hook of eventHooks) {
|
|
601
|
-
hook(...args);
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
/**
|
|
606
|
-
* Asynchronously render a template from a file path
|
|
607
|
-
* @param {string} filePath - Path to the template file to render
|
|
608
|
-
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
609
|
-
* @param {string} [rootTemplatePath] - Root directory for template includes. Defaults to file's directory
|
|
610
|
-
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
611
|
-
* @param {string} [engineName] - Engine to use. If not specified, determined from file extension
|
|
612
|
-
* @returns {Promise<string>} The rendered template output as a string
|
|
613
|
-
* @example
|
|
614
|
-
* const result = await ecto.renderFromFile('./templates/index.ejs', { title: 'Home' });
|
|
615
|
-
*/
|
|
616
|
-
async renderFromFile(filePath, data, rootTemplatePath, filePathOutput, engineName) {
|
|
617
|
-
let result = "";
|
|
618
|
-
engineName ??= this.getEngineByFilePath(filePath);
|
|
619
|
-
const templateRootPath = rootTemplatePath ?? path.dirname(filePath);
|
|
620
|
-
const source = await fs2.promises.readFile(filePath, "utf8");
|
|
621
|
-
result = await this.render(
|
|
622
|
-
source,
|
|
623
|
-
data,
|
|
624
|
-
engineName,
|
|
625
|
-
templateRootPath,
|
|
626
|
-
filePathOutput
|
|
627
|
-
);
|
|
628
|
-
return result;
|
|
629
|
-
}
|
|
630
|
-
/**
|
|
631
|
-
* Synchronously render a template from a file path
|
|
632
|
-
* @param {string} filePath - Path to the template file to render
|
|
633
|
-
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
634
|
-
* @param {string} [rootTemplatePath] - Root directory for template includes. Defaults to file's directory
|
|
635
|
-
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
636
|
-
* @param {string} [engineName] - Engine to use. If not specified, determined from file extension
|
|
637
|
-
* @returns {string} The rendered template output as a string
|
|
638
|
-
* @example
|
|
639
|
-
* const result = ecto.renderFromFileSync('./templates/index.ejs', { title: 'Home' });
|
|
640
|
-
*/
|
|
641
|
-
renderFromFileSync(filePath, data, rootTemplatePath, filePathOutput, engineName) {
|
|
642
|
-
let result = "";
|
|
643
|
-
engineName ??= this.getEngineByFilePath(filePath);
|
|
644
|
-
const templateRootPath = rootTemplatePath ?? path.dirname(filePath);
|
|
645
|
-
const source = fs2.readFileSync(filePath, "utf8");
|
|
646
|
-
result = this.renderSync(
|
|
647
|
-
source,
|
|
648
|
-
data,
|
|
649
|
-
engineName,
|
|
650
|
-
templateRootPath,
|
|
651
|
-
filePathOutput
|
|
652
|
-
);
|
|
653
|
-
return result;
|
|
654
|
-
}
|
|
655
|
-
/**
|
|
656
|
-
* Asynchronously ensure that the directory path for a file exists, creating it if necessary
|
|
657
|
-
* @param {string} path - The full file path (directories will be extracted from this)
|
|
658
|
-
* @returns {Promise<void>}
|
|
659
|
-
* @example
|
|
660
|
-
* await ecto.ensureFilePath('/path/to/file.txt');
|
|
661
|
-
*/
|
|
662
|
-
async ensureFilePath(path2) {
|
|
663
|
-
const pathList = path2.split("/");
|
|
664
|
-
pathList.pop();
|
|
665
|
-
const directory = pathList.join("/");
|
|
666
|
-
if (!fs2.existsSync(directory)) {
|
|
667
|
-
fs2.mkdirSync(directory, { recursive: true });
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
|
-
* Synchronously ensure that the directory path for a file exists, creating it if necessary
|
|
672
|
-
* @param {string} path - The full file path (directories will be extracted from this)
|
|
673
|
-
* @returns {void}
|
|
674
|
-
* @example
|
|
675
|
-
* ecto.ensureFilePathSync('/path/to/file.txt');
|
|
676
|
-
*/
|
|
677
|
-
ensureFilePathSync(path2) {
|
|
678
|
-
const pathList = path2.split("/");
|
|
679
|
-
pathList.pop();
|
|
680
|
-
const directory = pathList.join("/");
|
|
681
|
-
if (!fs2.existsSync(directory)) {
|
|
682
|
-
fs2.mkdirSync(directory, { recursive: true });
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
/**
|
|
686
|
-
* Determine the appropriate template engine based on a file's extension
|
|
687
|
-
* @param {string} filePath - The file path to analyze
|
|
688
|
-
* @returns {string} The engine name (e.g., 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid')
|
|
689
|
-
* @example
|
|
690
|
-
* const engine = ecto.getEngineByFilePath('template.ejs'); // Returns 'ejs'
|
|
691
|
-
*/
|
|
692
|
-
getEngineByFilePath(filePath) {
|
|
693
|
-
let result = this._defaultEngine;
|
|
694
|
-
if (filePath !== void 0) {
|
|
695
|
-
const extension = filePath.includes(".") ? filePath.slice(filePath.lastIndexOf(".") + 1) : "";
|
|
696
|
-
const engExtension = this._mapping.getName(extension);
|
|
697
|
-
if (engExtension !== void 0) {
|
|
698
|
-
result = engExtension;
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
return result;
|
|
702
|
-
}
|
|
703
|
-
/**
|
|
704
|
-
* Asynchronously find a template file in a directory by name, regardless of extension
|
|
705
|
-
* @param {string} path - Directory path to search in
|
|
706
|
-
* @param {string} templateName - Template name without extension
|
|
707
|
-
* @returns {Promise<string>} Full path to the found template file, or empty string if not found
|
|
708
|
-
* @example
|
|
709
|
-
* const templatePath = await ecto.findTemplateWithoutExtension('./templates', 'index');
|
|
710
|
-
*/
|
|
711
|
-
async findTemplateWithoutExtension(path2, templateName) {
|
|
712
|
-
let result = "";
|
|
713
|
-
const files = await fs2.promises.readdir(path2);
|
|
714
|
-
for (const file of files) {
|
|
715
|
-
if (file.startsWith(`${templateName}.`)) {
|
|
716
|
-
result = `${path2}/${file}`;
|
|
717
|
-
break;
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
return result;
|
|
721
|
-
}
|
|
722
|
-
/**
|
|
723
|
-
* Synchronously find a template file in a directory by name, regardless of extension
|
|
724
|
-
* @param {string} path - Directory path to search in
|
|
725
|
-
* @param {string} templateName - Template name without extension
|
|
726
|
-
* @returns {string} Full path to the found template file, or empty string if not found
|
|
727
|
-
* @example
|
|
728
|
-
* const templatePath = ecto.findTemplateWithoutExtensionSync('./templates', 'index');
|
|
729
|
-
*/
|
|
730
|
-
findTemplateWithoutExtensionSync(path2, templateName) {
|
|
731
|
-
let result = "";
|
|
732
|
-
const files = fs2.readdirSync(path2);
|
|
733
|
-
for (const file of files) {
|
|
734
|
-
if (file.startsWith(`${templateName}.`)) {
|
|
735
|
-
result = `${path2}/${file}`;
|
|
736
|
-
break;
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
return result;
|
|
740
|
-
}
|
|
741
|
-
/**
|
|
742
|
-
* Check if the given engine name is valid and registered in Ecto
|
|
743
|
-
* @param {string} [engineName] - The engine name to validate
|
|
744
|
-
* @returns {boolean} True if the engine is valid and registered, false otherwise
|
|
745
|
-
* @example
|
|
746
|
-
* const isValid = ecto.isValidEngine('ejs'); // Returns true
|
|
747
|
-
*/
|
|
748
|
-
isValidEngine(engineName) {
|
|
749
|
-
let result = false;
|
|
750
|
-
if (engineName !== void 0 && this._mapping.get(engineName) !== void 0) {
|
|
751
|
-
result = true;
|
|
752
|
-
}
|
|
753
|
-
return result;
|
|
754
|
-
}
|
|
755
|
-
/**
|
|
756
|
-
* Detect the template engine from a template string by analyzing its syntax
|
|
757
|
-
* @param {string} source - The template source string to analyze
|
|
758
|
-
* @returns {string} The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine
|
|
759
|
-
* @example
|
|
760
|
-
* const engine = ecto.detectEngine('<%= name %>'); // Returns 'ejs'
|
|
761
|
-
* const engine2 = ecto.detectEngine('{{name}}'); // Returns 'handlebars' or 'liquid'
|
|
762
|
-
* const engine3 = ecto.detectEngine('# Heading'); // Returns 'markdown'
|
|
763
|
-
* const engine4 = ecto.detectEngine('plain text'); // Returns defaultEngine (e.g., 'ejs')
|
|
764
|
-
*/
|
|
765
|
-
detectEngine(source) {
|
|
766
|
-
if (!source || typeof source !== "string") {
|
|
767
|
-
return this._defaultEngine;
|
|
768
|
-
}
|
|
769
|
-
if (source.includes("<%")) {
|
|
770
|
-
if (source.includes("<%=") || source.includes("<%-") || source.includes("%>")) {
|
|
771
|
-
return "ejs";
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
if (source.includes("{%")) {
|
|
775
|
-
const liquidKeywords = [
|
|
776
|
-
"liquid",
|
|
777
|
-
"assign",
|
|
778
|
-
"capture",
|
|
779
|
-
"endcapture",
|
|
780
|
-
"case",
|
|
781
|
-
"when",
|
|
782
|
-
"unless",
|
|
783
|
-
"endunless",
|
|
784
|
-
"tablerow",
|
|
785
|
-
"endtablerow",
|
|
786
|
-
"increment",
|
|
787
|
-
"decrement"
|
|
788
|
-
];
|
|
789
|
-
for (const keyword of liquidKeywords) {
|
|
790
|
-
if (source.includes(`{% ${keyword}`) || source.includes(`{%${keyword}`)) {
|
|
791
|
-
return "liquid";
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
if (source.includes("{{") && source.includes("|") && source.includes("}}")) {
|
|
795
|
-
const nunjucksSpecific = [
|
|
796
|
-
"block",
|
|
797
|
-
"extends",
|
|
798
|
-
"macro",
|
|
799
|
-
"import",
|
|
800
|
-
"call"
|
|
801
|
-
];
|
|
802
|
-
let hasNunjucksKeyword = false;
|
|
803
|
-
for (const keyword of nunjucksSpecific) {
|
|
804
|
-
if (source.includes(`{% ${keyword}`) || source.includes(`{%${keyword}`)) {
|
|
805
|
-
hasNunjucksKeyword = true;
|
|
806
|
-
break;
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
if (!hasNunjucksKeyword) {
|
|
810
|
-
return "liquid";
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
if (source.includes("{%")) {
|
|
815
|
-
const nunjucksKeywords = [
|
|
816
|
-
"block",
|
|
817
|
-
"extends",
|
|
818
|
-
"include",
|
|
819
|
-
"import",
|
|
820
|
-
"for",
|
|
821
|
-
"if",
|
|
822
|
-
"elif",
|
|
823
|
-
"else",
|
|
824
|
-
"endif",
|
|
825
|
-
"endfor",
|
|
826
|
-
"set",
|
|
827
|
-
"macro",
|
|
828
|
-
"endmacro",
|
|
829
|
-
"call"
|
|
830
|
-
];
|
|
831
|
-
for (const keyword of nunjucksKeywords) {
|
|
832
|
-
if (source.includes(`{% ${keyword}`) || source.includes(`{%${keyword}`)) {
|
|
833
|
-
return "nunjucks";
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
if (source.includes("{{")) {
|
|
838
|
-
if (source.includes(" | ") && source.includes("}}")) {
|
|
839
|
-
if (source.includes("{% assign ") || source.includes("{% capture ") || source.includes("{% unless ") || source.includes("{% increment ") || source.includes("{% decrement ") || source.includes("{% tablerow ") || source.includes("{% case ") || source.includes("{% when ") || source.includes(" | upcase") || source.includes(" | downcase") || source.includes(" | capitalize") || source.includes(" | minus:") || source.includes(" | plus:") || source.includes(" | money") || source.includes(" | date:")) {
|
|
840
|
-
return "liquid";
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
if (source.includes("{{#") || source.includes("{{/") || source.includes("{{>") || source.includes("{{!--")) {
|
|
844
|
-
return "handlebars";
|
|
845
|
-
}
|
|
846
|
-
if (source.includes("}}") && !source.includes("{%")) {
|
|
847
|
-
return "handlebars";
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
const lines = source.split("\n");
|
|
851
|
-
let markdownIndicators = 0;
|
|
852
|
-
let pugIndicators = 0;
|
|
853
|
-
for (const line of lines) {
|
|
854
|
-
const trimmed = line.trim();
|
|
855
|
-
if (trimmed.startsWith("# ") || trimmed.startsWith("## ") || trimmed.startsWith("### ") || trimmed.startsWith("#### ") || trimmed.startsWith("##### ") || trimmed.startsWith("###### ")) {
|
|
856
|
-
markdownIndicators++;
|
|
857
|
-
}
|
|
858
|
-
if (trimmed.startsWith("- ") || trimmed.startsWith("* ") || trimmed.startsWith("+ ")) {
|
|
859
|
-
markdownIndicators++;
|
|
860
|
-
}
|
|
861
|
-
const firstChar = trimmed.charAt(0);
|
|
862
|
-
if (firstChar >= "0" && firstChar <= "9") {
|
|
863
|
-
const dotIndex = trimmed.indexOf(".");
|
|
864
|
-
if (dotIndex > 0 && dotIndex < 4 && trimmed.charAt(dotIndex + 1) === " ") {
|
|
865
|
-
markdownIndicators++;
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
if (trimmed.startsWith("> ")) {
|
|
869
|
-
markdownIndicators++;
|
|
870
|
-
}
|
|
871
|
-
if (trimmed.startsWith("```")) {
|
|
872
|
-
markdownIndicators++;
|
|
873
|
-
}
|
|
874
|
-
if (!trimmed.startsWith("# ") && !trimmed.startsWith("- ") && !trimmed.startsWith("* ") && !trimmed.startsWith("+ ") && !trimmed.startsWith("> ")) {
|
|
875
|
-
if (trimmed.startsWith("doctype") || trimmed.startsWith("html") || trimmed.startsWith("head") || trimmed.startsWith("body") || trimmed.startsWith("div") || trimmed.startsWith("p") || trimmed.startsWith("h") && trimmed.length > 1 && trimmed.charAt(1) >= "1" && trimmed.charAt(1) <= "6") {
|
|
876
|
-
pugIndicators++;
|
|
877
|
-
}
|
|
878
|
-
if (trimmed.includes("(") && trimmed.includes(")") || trimmed.indexOf(".") === 0 || // starts with dot for class
|
|
879
|
-
trimmed.indexOf("#") === 0 && !trimmed.includes(" ")) {
|
|
880
|
-
pugIndicators++;
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
if (source.includes("](") && (source.includes("[") || source.includes("!["))) {
|
|
885
|
-
markdownIndicators++;
|
|
886
|
-
}
|
|
887
|
-
if (source.includes("|") && source.includes("---")) {
|
|
888
|
-
markdownIndicators++;
|
|
889
|
-
}
|
|
890
|
-
if (markdownIndicators > 0) {
|
|
891
|
-
if (!source.includes("<%") && !source.includes("{{") && !source.includes("{%")) {
|
|
892
|
-
return "markdown";
|
|
893
|
-
}
|
|
894
|
-
}
|
|
895
|
-
const hasHtmlOpenTag = source.includes("<") && source.includes(">");
|
|
896
|
-
const hasHtmlCloseTag = source.includes("</");
|
|
897
|
-
if (pugIndicators > 0 && !hasHtmlOpenTag && !hasHtmlCloseTag && !source.includes("<%") && !source.includes("{{") && !source.includes("{%")) {
|
|
898
|
-
return "pug";
|
|
899
|
-
}
|
|
900
|
-
return this._defaultEngine;
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* Register all engine mappings between engine names and file extensions
|
|
904
|
-
* @returns {void}
|
|
905
|
-
* @private
|
|
906
|
-
*/
|
|
907
|
-
registerEngineMappings() {
|
|
908
|
-
for (const eng of this._engines) {
|
|
909
|
-
for (const name of eng.names) {
|
|
910
|
-
this._mapping.set(name, eng.getExtensions());
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
/**
|
|
915
|
-
* Get the render engine instance by name
|
|
916
|
-
* @param {string} engineName - The name of the engine to retrieve
|
|
917
|
-
* @returns {EngineInterface} The engine instance (defaults to EJS if not found)
|
|
918
|
-
* @example
|
|
919
|
-
* const engine = ecto.getRenderEngine('pug');
|
|
920
|
-
*/
|
|
921
|
-
getRenderEngine(engineName) {
|
|
922
|
-
let result = this._ejs;
|
|
923
|
-
const cleanEngineName = engineName.trim().toLowerCase();
|
|
924
|
-
switch (cleanEngineName) {
|
|
925
|
-
case "markdown": {
|
|
926
|
-
result = this._markdown;
|
|
927
|
-
break;
|
|
928
|
-
}
|
|
929
|
-
case "pug": {
|
|
930
|
-
result = this._pug;
|
|
931
|
-
break;
|
|
932
|
-
}
|
|
933
|
-
case "nunjucks": {
|
|
934
|
-
result = this._nunjucks;
|
|
935
|
-
break;
|
|
936
|
-
}
|
|
937
|
-
case "mustache": {
|
|
938
|
-
result = this._handlebars;
|
|
939
|
-
break;
|
|
940
|
-
}
|
|
941
|
-
case "handlebars": {
|
|
942
|
-
result = this._handlebars;
|
|
943
|
-
break;
|
|
944
|
-
}
|
|
945
|
-
case "liquid": {
|
|
946
|
-
result = this._liquid;
|
|
947
|
-
break;
|
|
948
|
-
}
|
|
949
|
-
default: {
|
|
950
|
-
result = this._ejs;
|
|
951
|
-
break;
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
return result;
|
|
955
|
-
}
|
|
956
|
-
/**
|
|
957
|
-
* Check if the source content contains front matter (YAML metadata)
|
|
958
|
-
* @param {string} source - The source content to check
|
|
959
|
-
* @returns {boolean} True if front matter is present, false otherwise
|
|
960
|
-
* @example
|
|
961
|
-
* const hasFM = ecto.hasFrontMatter('---\ntitle: Test\n---\nContent');
|
|
962
|
-
*/
|
|
963
|
-
hasFrontMatter(source) {
|
|
964
|
-
const writr = new Writr2(source);
|
|
965
|
-
if (writr.frontMatterRaw !== "") {
|
|
966
|
-
return true;
|
|
967
|
-
}
|
|
968
|
-
return false;
|
|
969
|
-
}
|
|
970
|
-
/**
|
|
971
|
-
* Extract front matter data from the source content
|
|
972
|
-
* @param {string} source - The source content containing front matter
|
|
973
|
-
* @returns {Record<string, unknown>} Parsed front matter as an object
|
|
974
|
-
* @example
|
|
975
|
-
* const data = ecto.getFrontMatter('---\ntitle: Test\n---\nContent');
|
|
976
|
-
*/
|
|
977
|
-
getFrontMatter(source) {
|
|
978
|
-
const writr = new Writr2(source);
|
|
979
|
-
return writr.frontMatter;
|
|
980
|
-
}
|
|
981
|
-
/**
|
|
982
|
-
* Set or replace front matter in the source content
|
|
983
|
-
* @param {string} source - The source content
|
|
984
|
-
* @param {Record<string, unknown>} data - The front matter data to set
|
|
985
|
-
* @returns {string} The source content with updated front matter
|
|
986
|
-
* @example
|
|
987
|
-
* const updated = ecto.setFrontMatter('Content', { title: 'New Title' });
|
|
988
|
-
*/
|
|
989
|
-
setFrontMatter(source, data) {
|
|
990
|
-
const writr = new Writr2(source);
|
|
991
|
-
writr.frontMatter = data;
|
|
992
|
-
return writr.content;
|
|
993
|
-
}
|
|
994
|
-
/**
|
|
995
|
-
* Remove front matter from the source content, returning only the body
|
|
996
|
-
* @param {string} source - The source content with front matter
|
|
997
|
-
* @returns {string} The source content without front matter
|
|
998
|
-
* @example
|
|
999
|
-
* const body = ecto.removeFrontMatter('---\ntitle: Test\n---\nContent');
|
|
1000
|
-
*/
|
|
1001
|
-
removeFrontMatter(source) {
|
|
1002
|
-
const writr = new Writr2(source);
|
|
1003
|
-
return writr.body;
|
|
1004
|
-
}
|
|
1005
|
-
/**
|
|
1006
|
-
* Write content to a file asynchronously, creating directories if needed
|
|
1007
|
-
* @private
|
|
1008
|
-
* @param {string} [filePath] - The path to write the file to
|
|
1009
|
-
* @param {string} [source] - The content to write to the file
|
|
1010
|
-
* @returns {Promise<void>}
|
|
1011
|
-
*/
|
|
1012
|
-
async writeFile(filePath, source) {
|
|
1013
|
-
if (filePath && source) {
|
|
1014
|
-
await this.ensureFilePath(filePath);
|
|
1015
|
-
await fs2.promises.writeFile(filePath, source);
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
/**
|
|
1019
|
-
* Write content to a file synchronously, creating directories if needed
|
|
1020
|
-
* @private
|
|
1021
|
-
* @param {string} [filePath] - The path to write the file to
|
|
1022
|
-
* @param {string} [source] - The content to write to the file
|
|
1023
|
-
* @returns {void}
|
|
1024
|
-
*/
|
|
1025
|
-
writeFileSync(filePath, source) {
|
|
1026
|
-
if (filePath && source) {
|
|
1027
|
-
this.ensureFilePathSync(filePath);
|
|
1028
|
-
fs2.writeFileSync(filePath, source);
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
};
|
|
1032
|
-
export {
|
|
1033
|
-
EJS,
|
|
1034
|
-
Ecto,
|
|
1035
|
-
EctoEvents,
|
|
1036
|
-
Handlebars,
|
|
1037
|
-
Liquid,
|
|
1038
|
-
Markdown,
|
|
1039
|
-
Nunjucks,
|
|
1040
|
-
Pug
|
|
1041
|
-
};
|
|
1042
|
-
/* v8 ignore next -- @preserve */
|