ecto 4.5.0 → 4.6.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 +63 -1
- package/dist/ecto.d.ts +119 -63
- package/dist/ecto.js +256 -63
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -30,6 +30,7 @@ Ecto is a modern template consolidation engine that enables the best template en
|
|
|
30
30
|
* [Default Engine](#default-engine)
|
|
31
31
|
* [Engine Mappings](#engine-mappings)
|
|
32
32
|
* [FrontMatter Helper Functions](#frontmatter-helper-functions)
|
|
33
|
+
* [Detect Template Engine](#detect-template-engine)
|
|
33
34
|
* [The Template Engines We Support](#the-template-engines-we-support)
|
|
34
35
|
* [EJS](#ejs)
|
|
35
36
|
* [Markdown](#markdown)
|
|
@@ -562,9 +563,70 @@ Ecto has added in some helper functions for frontmatter in markdown files. Front
|
|
|
562
563
|
|
|
563
564
|
* `.hasFrontMatter(source: string): boolean` - This function checks if the markdown file has frontmatter. It takes in a string and returns a boolean value.
|
|
564
565
|
* `.getFrontMatter(source: string): object` - This function gets the frontmatter from the markdown file. It takes in a string and returns an object.
|
|
565
|
-
* `setFrontMatter(source:string, data: Record<string, unknown>)` - This function sets the front matter even if it already exists and returns the full source with the new front matter.
|
|
566
|
+
* `setFrontMatter(source:string, data: Record<string, unknown>)` - This function sets the front matter even if it already exists and returns the full source with the new front matter.
|
|
566
567
|
* `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.
|
|
567
568
|
|
|
569
|
+
# Detect Template Engine
|
|
570
|
+
|
|
571
|
+
Ecto provides a `detectEngine` method that can automatically detect the template engine from a template string by analyzing its syntax patterns. This is useful when you receive template content but don't know which engine it uses.
|
|
572
|
+
|
|
573
|
+
## detectEngine(source: string): string
|
|
574
|
+
|
|
575
|
+
The `detectEngine` method analyzes the template syntax and returns the detected engine name. If no specific template syntax is found, it returns the configured default engine.
|
|
576
|
+
|
|
577
|
+
| Name | Type | Description |
|
|
578
|
+
| ------ | ------ | ------------------------------------------------- |
|
|
579
|
+
| source | string | The template source string to analyze |
|
|
580
|
+
|
|
581
|
+
**Returns:** The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine if no specific syntax is detected.
|
|
582
|
+
|
|
583
|
+
### Basic Usage
|
|
584
|
+
|
|
585
|
+
```javascript
|
|
586
|
+
const ecto = new Ecto();
|
|
587
|
+
|
|
588
|
+
// Detect EJS templates
|
|
589
|
+
const ejsEngine = ecto.detectEngine('<%= name %>');
|
|
590
|
+
console.log(ejsEngine); // 'ejs'
|
|
591
|
+
|
|
592
|
+
// Detect Handlebars templates
|
|
593
|
+
const hbsEngine = ecto.detectEngine('{{name}}');
|
|
594
|
+
console.log(hbsEngine); // 'handlebars'
|
|
595
|
+
|
|
596
|
+
// Detect Markdown
|
|
597
|
+
const mdEngine = ecto.detectEngine('# Hello World\n\nThis is markdown');
|
|
598
|
+
console.log(mdEngine); // 'markdown'
|
|
599
|
+
|
|
600
|
+
// Detect Pug templates
|
|
601
|
+
const pugEngine = ecto.detectEngine('div.container\n h1 Hello');
|
|
602
|
+
console.log(pugEngine); // 'pug'
|
|
603
|
+
|
|
604
|
+
// Detect Nunjucks templates
|
|
605
|
+
const njkEngine = ecto.detectEngine('{% block content %}Hello{% endblock %}');
|
|
606
|
+
console.log(njkEngine); // 'nunjucks'
|
|
607
|
+
|
|
608
|
+
// Detect Liquid templates
|
|
609
|
+
const liquidEngine = ecto.detectEngine('{% assign name = "John" %}{{ name | upcase }}');
|
|
610
|
+
console.log(liquidEngine); // 'liquid'
|
|
611
|
+
|
|
612
|
+
// Returns default engine for plain text
|
|
613
|
+
const defaultEngine = ecto.detectEngine('Plain text without template syntax');
|
|
614
|
+
console.log(defaultEngine); // 'ejs' (or whatever is set as defaultEngine)
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
### Detection Patterns
|
|
618
|
+
|
|
619
|
+
The `detectEngine` method recognizes the following syntax patterns:
|
|
620
|
+
|
|
621
|
+
- **EJS**: `<% %>`, `<%= %>`, `<%- %>`
|
|
622
|
+
- **Handlebars/Mustache**: `{{ }}`, `{{# }}`, `{{> }}`
|
|
623
|
+
- **Pug**: Indentation-based syntax without angle brackets
|
|
624
|
+
- **Nunjucks**: `{% block %}`, `{% extends %}`, `{% include %}`
|
|
625
|
+
- **Liquid**: `{% assign %}`, `{% capture %}`, pipe filters `{{ var | filter }}`
|
|
626
|
+
- **Markdown**: Headers `#`, lists, code blocks, links, tables
|
|
627
|
+
|
|
628
|
+
Note: For ambiguous syntax (like simple `{{ }}` which could be Handlebars, Mustache, or Liquid), the method makes intelligent decisions based on additional context clues in the template.
|
|
629
|
+
|
|
568
630
|
# Caching on Rendering
|
|
569
631
|
|
|
570
632
|
Ecto has a built-in caching mechanism that is `disabled by default` that allows you to cache the rendered output of templates. This is useful for improving performance and reducing the number of times a template needs to be rendered. There are currently two caching engines available: `MemoryCache` and `FileCache`.
|
package/dist/ecto.d.ts
CHANGED
|
@@ -195,120 +195,176 @@ declare class Ecto extends Hookified {
|
|
|
195
195
|
*/
|
|
196
196
|
get liquid(): Liquid;
|
|
197
197
|
/**
|
|
198
|
-
*
|
|
199
|
-
* @param {string} source - The source to render
|
|
200
|
-
* @param {Record<string, unknown} data -
|
|
201
|
-
* @param {string} [engineName] -
|
|
202
|
-
* @param {string} [rootTemplatePath] -
|
|
203
|
-
* @param {string} [filePathOutput] -
|
|
204
|
-
* @returns {Promise<string>}
|
|
198
|
+
* Asynchronously render a template source with data using the specified engine
|
|
199
|
+
* @param {string} source - The template source string to render
|
|
200
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
201
|
+
* @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
|
|
202
|
+
* @param {string} [rootTemplatePath] - Root directory path for template includes/partials resolution
|
|
203
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
204
|
+
* @returns {Promise<string>} The rendered template output as a string
|
|
205
|
+
* @example
|
|
206
|
+
* const result = await ecto.render('<%= name %>', { name: 'World' }, 'ejs');
|
|
205
207
|
*/
|
|
206
208
|
render(source: string, data?: Record<string, unknown>, engineName?: string, rootTemplatePath?: string, filePathOutput?: string): Promise<string>;
|
|
207
209
|
/**
|
|
208
|
-
* Synchronously render
|
|
209
|
-
* @param {string} source - The source to render
|
|
210
|
-
* @param {Record<string, unknown} data -
|
|
211
|
-
* @param {string} [engineName] -
|
|
212
|
-
* @param {string} [rootTemplatePath] -
|
|
213
|
-
* @param {string} [filePathOutput] -
|
|
214
|
-
* @returns {string}
|
|
210
|
+
* Synchronously render a template source with data using the specified engine
|
|
211
|
+
* @param {string} source - The template source string to render
|
|
212
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
213
|
+
* @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
|
|
214
|
+
* @param {string} [rootTemplatePath] - Root directory path for template includes/partials resolution
|
|
215
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
216
|
+
* @returns {string} The rendered template output as a string
|
|
217
|
+
* @example
|
|
218
|
+
* const result = ecto.renderSync('<%= name %>', { name: 'World' }, 'ejs');
|
|
215
219
|
*/
|
|
216
220
|
renderSync(source: string, data?: Record<string, unknown>, engineName?: string, rootTemplatePath?: string, filePathOutput?: string): string;
|
|
217
221
|
/**
|
|
218
|
-
*
|
|
219
|
-
* @param {string} filePath -
|
|
220
|
-
* @param {Record<string, unknown>} data -
|
|
221
|
-
* @param {string} [rootTemplatePath] -
|
|
222
|
-
* @param {string} [filePathOutput] -
|
|
223
|
-
* @param {string} [engineName] -
|
|
224
|
-
* @returns
|
|
222
|
+
* Asynchronously render a template from a file path
|
|
223
|
+
* @param {string} filePath - Path to the template file to render
|
|
224
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
225
|
+
* @param {string} [rootTemplatePath] - Root directory for template includes. Defaults to file's directory
|
|
226
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
227
|
+
* @param {string} [engineName] - Engine to use. If not specified, determined from file extension
|
|
228
|
+
* @returns {Promise<string>} The rendered template output as a string
|
|
229
|
+
* @example
|
|
230
|
+
* const result = await ecto.renderFromFile('./templates/index.ejs', { title: 'Home' });
|
|
225
231
|
*/
|
|
226
232
|
renderFromFile(filePath: string, data?: Record<string, unknown>, rootTemplatePath?: string, filePathOutput?: string, engineName?: string): Promise<string>;
|
|
227
233
|
/**
|
|
228
|
-
*
|
|
229
|
-
* @param {string} filePath -
|
|
230
|
-
* @param {Record<string, unknown>} data -
|
|
231
|
-
* @param {string} [rootTemplatePath] -
|
|
232
|
-
* @param {string} [filePathOutput] -
|
|
233
|
-
* @param {string} [engineName] -
|
|
234
|
-
* @returns {string}
|
|
234
|
+
* Synchronously render a template from a file path
|
|
235
|
+
* @param {string} filePath - Path to the template file to render
|
|
236
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
237
|
+
* @param {string} [rootTemplatePath] - Root directory for template includes. Defaults to file's directory
|
|
238
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
239
|
+
* @param {string} [engineName] - Engine to use. If not specified, determined from file extension
|
|
240
|
+
* @returns {string} The rendered template output as a string
|
|
241
|
+
* @example
|
|
242
|
+
* const result = ecto.renderFromFileSync('./templates/index.ejs', { title: 'Home' });
|
|
235
243
|
*/
|
|
236
244
|
renderFromFileSync(filePath: string, data?: Record<string, unknown>, rootTemplatePath?: string, filePathOutput?: string, engineName?: string): string;
|
|
237
245
|
/**
|
|
238
|
-
*
|
|
239
|
-
* @param {string} path
|
|
246
|
+
* Asynchronously ensure that the directory path for a file exists, creating it if necessary
|
|
247
|
+
* @param {string} path - The full file path (directories will be extracted from this)
|
|
240
248
|
* @returns {Promise<void>}
|
|
249
|
+
* @example
|
|
250
|
+
* await ecto.ensureFilePath('/path/to/file.txt');
|
|
241
251
|
*/
|
|
242
252
|
ensureFilePath(path: string): Promise<void>;
|
|
243
253
|
/**
|
|
244
|
-
*
|
|
245
|
-
* @param {string} path
|
|
254
|
+
* Synchronously ensure that the directory path for a file exists, creating it if necessary
|
|
255
|
+
* @param {string} path - The full file path (directories will be extracted from this)
|
|
246
256
|
* @returns {void}
|
|
257
|
+
* @example
|
|
258
|
+
* ecto.ensureFilePathSync('/path/to/file.txt');
|
|
247
259
|
*/
|
|
248
260
|
ensureFilePathSync(path: string): void;
|
|
249
261
|
/**
|
|
250
|
-
*
|
|
251
|
-
* @param {string} filePath
|
|
252
|
-
* @returns {string}
|
|
262
|
+
* Determine the appropriate template engine based on a file's extension
|
|
263
|
+
* @param {string} filePath - The file path to analyze
|
|
264
|
+
* @returns {string} The engine name (e.g., 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid')
|
|
265
|
+
* @example
|
|
266
|
+
* const engine = ecto.getEngineByFilePath('template.ejs'); // Returns 'ejs'
|
|
253
267
|
*/
|
|
254
268
|
getEngineByFilePath(filePath: string): string;
|
|
255
269
|
/**
|
|
256
|
-
*
|
|
257
|
-
* @param {string} path -
|
|
258
|
-
* @param {string} templateName
|
|
259
|
-
* @returns {Promise<string>}
|
|
270
|
+
* Asynchronously find a template file in a directory by name, regardless of extension
|
|
271
|
+
* @param {string} path - Directory path to search in
|
|
272
|
+
* @param {string} templateName - Template name without extension
|
|
273
|
+
* @returns {Promise<string>} Full path to the found template file, or empty string if not found
|
|
274
|
+
* @example
|
|
275
|
+
* const templatePath = await ecto.findTemplateWithoutExtension('./templates', 'index');
|
|
260
276
|
*/
|
|
261
277
|
findTemplateWithoutExtension(path: string, templateName: string): Promise<string>;
|
|
262
278
|
/**
|
|
263
|
-
*
|
|
264
|
-
* @param {string} path -
|
|
265
|
-
* @param {string} templateName
|
|
266
|
-
* @returns {string}
|
|
279
|
+
* Synchronously find a template file in a directory by name, regardless of extension
|
|
280
|
+
* @param {string} path - Directory path to search in
|
|
281
|
+
* @param {string} templateName - Template name without extension
|
|
282
|
+
* @returns {string} Full path to the found template file, or empty string if not found
|
|
283
|
+
* @example
|
|
284
|
+
* const templatePath = ecto.findTemplateWithoutExtensionSync('./templates', 'index');
|
|
267
285
|
*/
|
|
268
286
|
findTemplateWithoutExtensionSync(path: string, templateName: string): string;
|
|
269
287
|
/**
|
|
270
|
-
*
|
|
271
|
-
* @param engineName
|
|
272
|
-
* @returns {boolean}
|
|
288
|
+
* Check if the given engine name is valid and registered in Ecto
|
|
289
|
+
* @param {string} [engineName] - The engine name to validate
|
|
290
|
+
* @returns {boolean} True if the engine is valid and registered, false otherwise
|
|
291
|
+
* @example
|
|
292
|
+
* const isValid = ecto.isValidEngine('ejs'); // Returns true
|
|
273
293
|
*/
|
|
274
294
|
isValidEngine(engineName?: string): boolean;
|
|
275
295
|
/**
|
|
276
|
-
*
|
|
296
|
+
* Detect the template engine from a template string by analyzing its syntax
|
|
297
|
+
* @param {string} source - The template source string to analyze
|
|
298
|
+
* @returns {string} The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine
|
|
299
|
+
* @example
|
|
300
|
+
* const engine = ecto.detectEngine('<%= name %>'); // Returns 'ejs'
|
|
301
|
+
* const engine2 = ecto.detectEngine('{{name}}'); // Returns 'handlebars' or 'liquid'
|
|
302
|
+
* const engine3 = ecto.detectEngine('# Heading'); // Returns 'markdown'
|
|
303
|
+
* const engine4 = ecto.detectEngine('plain text'); // Returns defaultEngine (e.g., 'ejs')
|
|
304
|
+
*/
|
|
305
|
+
detectEngine(source: string): string;
|
|
306
|
+
/**
|
|
307
|
+
* Register all engine mappings between engine names and file extensions
|
|
277
308
|
* @returns {void}
|
|
309
|
+
* @private
|
|
278
310
|
*/
|
|
279
311
|
registerEngineMappings(): void;
|
|
280
312
|
/**
|
|
281
|
-
* Get
|
|
282
|
-
* @param {string} engineName
|
|
283
|
-
* @returns {EngineInterface}
|
|
313
|
+
* Get the render engine instance by name
|
|
314
|
+
* @param {string} engineName - The name of the engine to retrieve
|
|
315
|
+
* @returns {EngineInterface} The engine instance (defaults to EJS if not found)
|
|
316
|
+
* @example
|
|
317
|
+
* const engine = ecto.getRenderEngine('pug');
|
|
284
318
|
*/
|
|
285
319
|
getRenderEngine(engineName: string): EngineInterface;
|
|
286
320
|
/**
|
|
287
|
-
*
|
|
288
|
-
* @param {string} source
|
|
289
|
-
* @returns {boolean}
|
|
321
|
+
* Check if the source content contains front matter (YAML metadata)
|
|
322
|
+
* @param {string} source - The source content to check
|
|
323
|
+
* @returns {boolean} True if front matter is present, false otherwise
|
|
324
|
+
* @example
|
|
325
|
+
* const hasFM = ecto.hasFrontMatter('---\ntitle: Test\n---\nContent');
|
|
290
326
|
*/
|
|
291
327
|
hasFrontMatter(source: string): boolean;
|
|
292
328
|
/**
|
|
293
|
-
*
|
|
294
|
-
* @param {string} source
|
|
295
|
-
* @returns {Record<string, unknown>}
|
|
329
|
+
* Extract front matter data from the source content
|
|
330
|
+
* @param {string} source - The source content containing front matter
|
|
331
|
+
* @returns {Record<string, unknown>} Parsed front matter as an object
|
|
332
|
+
* @example
|
|
333
|
+
* const data = ecto.getFrontMatter('---\ntitle: Test\n---\nContent');
|
|
296
334
|
*/
|
|
297
335
|
getFrontMatter(source: string): Record<string, unknown>;
|
|
298
336
|
/**
|
|
299
|
-
*
|
|
300
|
-
* @param {string} source - The source
|
|
301
|
-
* @param {Record<string, unknown>} data - The front matter data
|
|
302
|
-
* @returns {string}
|
|
337
|
+
* Set or replace front matter in the source content
|
|
338
|
+
* @param {string} source - The source content
|
|
339
|
+
* @param {Record<string, unknown>} data - The front matter data to set
|
|
340
|
+
* @returns {string} The source content with updated front matter
|
|
341
|
+
* @example
|
|
342
|
+
* const updated = ecto.setFrontMatter('Content', { title: 'New Title' });
|
|
303
343
|
*/
|
|
304
344
|
setFrontMatter(source: string, data: Record<string, unknown>): string;
|
|
305
345
|
/**
|
|
306
|
-
* Remove the
|
|
307
|
-
* @param {string} source
|
|
308
|
-
* @returns {string}
|
|
346
|
+
* Remove front matter from the source content, returning only the body
|
|
347
|
+
* @param {string} source - The source content with front matter
|
|
348
|
+
* @returns {string} The source content without front matter
|
|
349
|
+
* @example
|
|
350
|
+
* const body = ecto.removeFrontMatter('---\ntitle: Test\n---\nContent');
|
|
309
351
|
*/
|
|
310
352
|
removeFrontMatter(source: string): string;
|
|
353
|
+
/**
|
|
354
|
+
* Write content to a file asynchronously, creating directories if needed
|
|
355
|
+
* @private
|
|
356
|
+
* @param {string} [filePath] - The path to write the file to
|
|
357
|
+
* @param {string} [source] - The content to write to the file
|
|
358
|
+
* @returns {Promise<void>}
|
|
359
|
+
*/
|
|
311
360
|
private writeFile;
|
|
361
|
+
/**
|
|
362
|
+
* Write content to a file synchronously, creating directories if needed
|
|
363
|
+
* @private
|
|
364
|
+
* @param {string} [filePath] - The path to write the file to
|
|
365
|
+
* @param {string} [source] - The content to write to the file
|
|
366
|
+
* @returns {void}
|
|
367
|
+
*/
|
|
312
368
|
private writeFileSync;
|
|
313
369
|
}
|
|
314
370
|
|
package/dist/ecto.js
CHANGED
|
@@ -459,13 +459,15 @@ var Ecto = class extends Hookified {
|
|
|
459
459
|
return this._liquid;
|
|
460
460
|
}
|
|
461
461
|
/**
|
|
462
|
-
*
|
|
463
|
-
* @param {string} source - The source to render
|
|
464
|
-
* @param {Record<string, unknown} data -
|
|
465
|
-
* @param {string} [engineName] -
|
|
466
|
-
* @param {string} [rootTemplatePath] -
|
|
467
|
-
* @param {string} [filePathOutput] -
|
|
468
|
-
* @returns {Promise<string>}
|
|
462
|
+
* Asynchronously render a template source with data using the specified engine
|
|
463
|
+
* @param {string} source - The template source string to render
|
|
464
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
465
|
+
* @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
|
|
466
|
+
* @param {string} [rootTemplatePath] - Root directory path for template includes/partials resolution
|
|
467
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
468
|
+
* @returns {Promise<string>} The rendered template output as a string
|
|
469
|
+
* @example
|
|
470
|
+
* const result = await ecto.render('<%= name %>', { name: 'World' }, 'ejs');
|
|
469
471
|
*/
|
|
470
472
|
async render(source, data, engineName, rootTemplatePath, filePathOutput) {
|
|
471
473
|
try {
|
|
@@ -498,13 +500,15 @@ var Ecto = class extends Hookified {
|
|
|
498
500
|
}
|
|
499
501
|
}
|
|
500
502
|
/**
|
|
501
|
-
* Synchronously render
|
|
502
|
-
* @param {string} source - The source to render
|
|
503
|
-
* @param {Record<string, unknown} data -
|
|
504
|
-
* @param {string} [engineName] -
|
|
505
|
-
* @param {string} [rootTemplatePath] -
|
|
506
|
-
* @param {string} [filePathOutput] -
|
|
507
|
-
* @returns {string}
|
|
503
|
+
* Synchronously render a template source with data using the specified engine
|
|
504
|
+
* @param {string} source - The template source string to render
|
|
505
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
506
|
+
* @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
|
|
507
|
+
* @param {string} [rootTemplatePath] - Root directory path for template includes/partials resolution
|
|
508
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
509
|
+
* @returns {string} The rendered template output as a string
|
|
510
|
+
* @example
|
|
511
|
+
* const result = ecto.renderSync('<%= name %>', { name: 'World' }, 'ejs');
|
|
508
512
|
*/
|
|
509
513
|
renderSync(source, data, engineName, rootTemplatePath, filePathOutput) {
|
|
510
514
|
try {
|
|
@@ -537,13 +541,15 @@ var Ecto = class extends Hookified {
|
|
|
537
541
|
}
|
|
538
542
|
}
|
|
539
543
|
/**
|
|
540
|
-
*
|
|
541
|
-
* @param {string} filePath -
|
|
542
|
-
* @param {Record<string, unknown>} data -
|
|
543
|
-
* @param {string} [rootTemplatePath] -
|
|
544
|
-
* @param {string} [filePathOutput] -
|
|
545
|
-
* @param {string} [engineName] -
|
|
546
|
-
* @returns
|
|
544
|
+
* Asynchronously render a template from a file path
|
|
545
|
+
* @param {string} filePath - Path to the template file to render
|
|
546
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
547
|
+
* @param {string} [rootTemplatePath] - Root directory for template includes. Defaults to file's directory
|
|
548
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
549
|
+
* @param {string} [engineName] - Engine to use. If not specified, determined from file extension
|
|
550
|
+
* @returns {Promise<string>} The rendered template output as a string
|
|
551
|
+
* @example
|
|
552
|
+
* const result = await ecto.renderFromFile('./templates/index.ejs', { title: 'Home' });
|
|
547
553
|
*/
|
|
548
554
|
async renderFromFile(filePath, data, rootTemplatePath, filePathOutput, engineName) {
|
|
549
555
|
let result = "";
|
|
@@ -560,13 +566,15 @@ var Ecto = class extends Hookified {
|
|
|
560
566
|
return result;
|
|
561
567
|
}
|
|
562
568
|
/**
|
|
563
|
-
*
|
|
564
|
-
* @param {string} filePath -
|
|
565
|
-
* @param {Record<string, unknown>} data -
|
|
566
|
-
* @param {string} [rootTemplatePath] -
|
|
567
|
-
* @param {string} [filePathOutput] -
|
|
568
|
-
* @param {string} [engineName] -
|
|
569
|
-
* @returns {string}
|
|
569
|
+
* Synchronously render a template from a file path
|
|
570
|
+
* @param {string} filePath - Path to the template file to render
|
|
571
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
572
|
+
* @param {string} [rootTemplatePath] - Root directory for template includes. Defaults to file's directory
|
|
573
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
574
|
+
* @param {string} [engineName] - Engine to use. If not specified, determined from file extension
|
|
575
|
+
* @returns {string} The rendered template output as a string
|
|
576
|
+
* @example
|
|
577
|
+
* const result = ecto.renderFromFileSync('./templates/index.ejs', { title: 'Home' });
|
|
570
578
|
*/
|
|
571
579
|
renderFromFileSync(filePath, data, rootTemplatePath, filePathOutput, engineName) {
|
|
572
580
|
let result = "";
|
|
@@ -583,9 +591,11 @@ var Ecto = class extends Hookified {
|
|
|
583
591
|
return result;
|
|
584
592
|
}
|
|
585
593
|
/**
|
|
586
|
-
*
|
|
587
|
-
* @param {string} path
|
|
594
|
+
* Asynchronously ensure that the directory path for a file exists, creating it if necessary
|
|
595
|
+
* @param {string} path - The full file path (directories will be extracted from this)
|
|
588
596
|
* @returns {Promise<void>}
|
|
597
|
+
* @example
|
|
598
|
+
* await ecto.ensureFilePath('/path/to/file.txt');
|
|
589
599
|
*/
|
|
590
600
|
async ensureFilePath(path2) {
|
|
591
601
|
const pathList = path2.split("/");
|
|
@@ -596,9 +606,11 @@ var Ecto = class extends Hookified {
|
|
|
596
606
|
}
|
|
597
607
|
}
|
|
598
608
|
/**
|
|
599
|
-
*
|
|
600
|
-
* @param {string} path
|
|
609
|
+
* Synchronously ensure that the directory path for a file exists, creating it if necessary
|
|
610
|
+
* @param {string} path - The full file path (directories will be extracted from this)
|
|
601
611
|
* @returns {void}
|
|
612
|
+
* @example
|
|
613
|
+
* ecto.ensureFilePathSync('/path/to/file.txt');
|
|
602
614
|
*/
|
|
603
615
|
ensureFilePathSync(path2) {
|
|
604
616
|
const pathList = path2.split("/");
|
|
@@ -609,9 +621,11 @@ var Ecto = class extends Hookified {
|
|
|
609
621
|
}
|
|
610
622
|
}
|
|
611
623
|
/**
|
|
612
|
-
*
|
|
613
|
-
* @param {string} filePath
|
|
614
|
-
* @returns {string}
|
|
624
|
+
* Determine the appropriate template engine based on a file's extension
|
|
625
|
+
* @param {string} filePath - The file path to analyze
|
|
626
|
+
* @returns {string} The engine name (e.g., 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid')
|
|
627
|
+
* @example
|
|
628
|
+
* const engine = ecto.getEngineByFilePath('template.ejs'); // Returns 'ejs'
|
|
615
629
|
*/
|
|
616
630
|
getEngineByFilePath(filePath) {
|
|
617
631
|
let result = this._defaultEngine;
|
|
@@ -625,10 +639,12 @@ var Ecto = class extends Hookified {
|
|
|
625
639
|
return result;
|
|
626
640
|
}
|
|
627
641
|
/**
|
|
628
|
-
*
|
|
629
|
-
* @param {string} path -
|
|
630
|
-
* @param {string} templateName
|
|
631
|
-
* @returns {Promise<string>}
|
|
642
|
+
* Asynchronously find a template file in a directory by name, regardless of extension
|
|
643
|
+
* @param {string} path - Directory path to search in
|
|
644
|
+
* @param {string} templateName - Template name without extension
|
|
645
|
+
* @returns {Promise<string>} Full path to the found template file, or empty string if not found
|
|
646
|
+
* @example
|
|
647
|
+
* const templatePath = await ecto.findTemplateWithoutExtension('./templates', 'index');
|
|
632
648
|
*/
|
|
633
649
|
async findTemplateWithoutExtension(path2, templateName) {
|
|
634
650
|
let result = "";
|
|
@@ -642,10 +658,12 @@ var Ecto = class extends Hookified {
|
|
|
642
658
|
return result;
|
|
643
659
|
}
|
|
644
660
|
/**
|
|
645
|
-
*
|
|
646
|
-
* @param {string} path -
|
|
647
|
-
* @param {string} templateName
|
|
648
|
-
* @returns {string}
|
|
661
|
+
* Synchronously find a template file in a directory by name, regardless of extension
|
|
662
|
+
* @param {string} path - Directory path to search in
|
|
663
|
+
* @param {string} templateName - Template name without extension
|
|
664
|
+
* @returns {string} Full path to the found template file, or empty string if not found
|
|
665
|
+
* @example
|
|
666
|
+
* const templatePath = ecto.findTemplateWithoutExtensionSync('./templates', 'index');
|
|
649
667
|
*/
|
|
650
668
|
findTemplateWithoutExtensionSync(path2, templateName) {
|
|
651
669
|
let result = "";
|
|
@@ -659,9 +677,11 @@ var Ecto = class extends Hookified {
|
|
|
659
677
|
return result;
|
|
660
678
|
}
|
|
661
679
|
/**
|
|
662
|
-
*
|
|
663
|
-
* @param engineName
|
|
664
|
-
* @returns {boolean}
|
|
680
|
+
* Check if the given engine name is valid and registered in Ecto
|
|
681
|
+
* @param {string} [engineName] - The engine name to validate
|
|
682
|
+
* @returns {boolean} True if the engine is valid and registered, false otherwise
|
|
683
|
+
* @example
|
|
684
|
+
* const isValid = ecto.isValidEngine('ejs'); // Returns true
|
|
665
685
|
*/
|
|
666
686
|
isValidEngine(engineName) {
|
|
667
687
|
let result = false;
|
|
@@ -671,8 +691,157 @@ var Ecto = class extends Hookified {
|
|
|
671
691
|
return result;
|
|
672
692
|
}
|
|
673
693
|
/**
|
|
674
|
-
*
|
|
694
|
+
* Detect the template engine from a template string by analyzing its syntax
|
|
695
|
+
* @param {string} source - The template source string to analyze
|
|
696
|
+
* @returns {string} The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine
|
|
697
|
+
* @example
|
|
698
|
+
* const engine = ecto.detectEngine('<%= name %>'); // Returns 'ejs'
|
|
699
|
+
* const engine2 = ecto.detectEngine('{{name}}'); // Returns 'handlebars' or 'liquid'
|
|
700
|
+
* const engine3 = ecto.detectEngine('# Heading'); // Returns 'markdown'
|
|
701
|
+
* const engine4 = ecto.detectEngine('plain text'); // Returns defaultEngine (e.g., 'ejs')
|
|
702
|
+
*/
|
|
703
|
+
detectEngine(source) {
|
|
704
|
+
if (!source || typeof source !== "string") {
|
|
705
|
+
return this._defaultEngine;
|
|
706
|
+
}
|
|
707
|
+
if (source.includes("<%")) {
|
|
708
|
+
if (source.includes("<%=") || source.includes("<%-") || source.includes("%>")) {
|
|
709
|
+
return "ejs";
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
if (source.includes("{%")) {
|
|
713
|
+
const liquidKeywords = [
|
|
714
|
+
"liquid",
|
|
715
|
+
"assign",
|
|
716
|
+
"capture",
|
|
717
|
+
"endcapture",
|
|
718
|
+
"case",
|
|
719
|
+
"when",
|
|
720
|
+
"unless",
|
|
721
|
+
"endunless",
|
|
722
|
+
"tablerow",
|
|
723
|
+
"endtablerow",
|
|
724
|
+
"increment",
|
|
725
|
+
"decrement"
|
|
726
|
+
];
|
|
727
|
+
for (const keyword of liquidKeywords) {
|
|
728
|
+
if (source.includes(`{% ${keyword}`) || source.includes(`{%${keyword}`)) {
|
|
729
|
+
return "liquid";
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (source.includes("{{") && source.includes("|") && source.includes("}}")) {
|
|
733
|
+
const nunjucksSpecific = [
|
|
734
|
+
"block",
|
|
735
|
+
"extends",
|
|
736
|
+
"macro",
|
|
737
|
+
"import",
|
|
738
|
+
"call"
|
|
739
|
+
];
|
|
740
|
+
let hasNunjucksKeyword = false;
|
|
741
|
+
for (const keyword of nunjucksSpecific) {
|
|
742
|
+
if (source.includes(`{% ${keyword}`) || source.includes(`{%${keyword}`)) {
|
|
743
|
+
hasNunjucksKeyword = true;
|
|
744
|
+
break;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
if (!hasNunjucksKeyword) {
|
|
748
|
+
return "liquid";
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
if (source.includes("{%")) {
|
|
753
|
+
const nunjucksKeywords = [
|
|
754
|
+
"block",
|
|
755
|
+
"extends",
|
|
756
|
+
"include",
|
|
757
|
+
"import",
|
|
758
|
+
"for",
|
|
759
|
+
"if",
|
|
760
|
+
"elif",
|
|
761
|
+
"else",
|
|
762
|
+
"endif",
|
|
763
|
+
"endfor",
|
|
764
|
+
"set",
|
|
765
|
+
"macro",
|
|
766
|
+
"endmacro",
|
|
767
|
+
"call"
|
|
768
|
+
];
|
|
769
|
+
for (const keyword of nunjucksKeywords) {
|
|
770
|
+
if (source.includes(`{% ${keyword}`) || source.includes(`{%${keyword}`)) {
|
|
771
|
+
return "nunjucks";
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
if (source.includes("{{")) {
|
|
776
|
+
if (source.includes(" | ") && source.includes("}}")) {
|
|
777
|
+
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:")) {
|
|
778
|
+
return "liquid";
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
if (source.includes("{{#") || source.includes("{{/") || source.includes("{{>") || source.includes("{{!--")) {
|
|
782
|
+
return "handlebars";
|
|
783
|
+
}
|
|
784
|
+
if (source.includes("}}") && !source.includes("{%")) {
|
|
785
|
+
return "handlebars";
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
const lines = source.split("\n");
|
|
789
|
+
let markdownIndicators = 0;
|
|
790
|
+
let pugIndicators = 0;
|
|
791
|
+
for (const line of lines) {
|
|
792
|
+
const trimmed = line.trim();
|
|
793
|
+
if (trimmed.startsWith("# ") || trimmed.startsWith("## ") || trimmed.startsWith("### ") || trimmed.startsWith("#### ") || trimmed.startsWith("##### ") || trimmed.startsWith("###### ")) {
|
|
794
|
+
markdownIndicators++;
|
|
795
|
+
}
|
|
796
|
+
if (trimmed.startsWith("- ") || trimmed.startsWith("* ") || trimmed.startsWith("+ ")) {
|
|
797
|
+
markdownIndicators++;
|
|
798
|
+
}
|
|
799
|
+
const firstChar = trimmed.charAt(0);
|
|
800
|
+
if (firstChar >= "0" && firstChar <= "9") {
|
|
801
|
+
const dotIndex = trimmed.indexOf(".");
|
|
802
|
+
if (dotIndex > 0 && dotIndex < 4 && trimmed.charAt(dotIndex + 1) === " ") {
|
|
803
|
+
markdownIndicators++;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
if (trimmed.startsWith("> ")) {
|
|
807
|
+
markdownIndicators++;
|
|
808
|
+
}
|
|
809
|
+
if (trimmed.startsWith("```")) {
|
|
810
|
+
markdownIndicators++;
|
|
811
|
+
}
|
|
812
|
+
if (!trimmed.startsWith("# ") && !trimmed.startsWith("- ") && !trimmed.startsWith("* ") && !trimmed.startsWith("+ ") && !trimmed.startsWith("> ")) {
|
|
813
|
+
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") {
|
|
814
|
+
pugIndicators++;
|
|
815
|
+
}
|
|
816
|
+
if (trimmed.includes("(") && trimmed.includes(")") || trimmed.indexOf(".") === 0 || // starts with dot for class
|
|
817
|
+
trimmed.indexOf("#") === 0 && !trimmed.includes(" ")) {
|
|
818
|
+
pugIndicators++;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
if (source.includes("](") && /* c8 ignore next */
|
|
823
|
+
(source.includes("[") || source.includes("!["))) {
|
|
824
|
+
markdownIndicators++;
|
|
825
|
+
}
|
|
826
|
+
if (source.includes("|") && source.includes("---")) {
|
|
827
|
+
markdownIndicators++;
|
|
828
|
+
}
|
|
829
|
+
if (markdownIndicators > 0) {
|
|
830
|
+
if (!source.includes("<%") && !source.includes("{{") && !source.includes("{%")) {
|
|
831
|
+
return "markdown";
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
const hasHtmlOpenTag = source.includes("<") && source.includes(">");
|
|
835
|
+
const hasHtmlCloseTag = source.includes("</");
|
|
836
|
+
if (pugIndicators > 0 && !hasHtmlOpenTag && !hasHtmlCloseTag && !source.includes("<%") && !source.includes("{{") && !source.includes("{%")) {
|
|
837
|
+
return "pug";
|
|
838
|
+
}
|
|
839
|
+
return this._defaultEngine;
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Register all engine mappings between engine names and file extensions
|
|
675
843
|
* @returns {void}
|
|
844
|
+
* @private
|
|
676
845
|
*/
|
|
677
846
|
registerEngineMappings() {
|
|
678
847
|
for (const eng of this._engines) {
|
|
@@ -682,9 +851,11 @@ var Ecto = class extends Hookified {
|
|
|
682
851
|
}
|
|
683
852
|
}
|
|
684
853
|
/**
|
|
685
|
-
* Get
|
|
686
|
-
* @param {string} engineName
|
|
687
|
-
* @returns {EngineInterface}
|
|
854
|
+
* Get the render engine instance by name
|
|
855
|
+
* @param {string} engineName - The name of the engine to retrieve
|
|
856
|
+
* @returns {EngineInterface} The engine instance (defaults to EJS if not found)
|
|
857
|
+
* @example
|
|
858
|
+
* const engine = ecto.getRenderEngine('pug');
|
|
688
859
|
*/
|
|
689
860
|
getRenderEngine(engineName) {
|
|
690
861
|
let result = this._ejs;
|
|
@@ -722,9 +893,11 @@ var Ecto = class extends Hookified {
|
|
|
722
893
|
return result;
|
|
723
894
|
}
|
|
724
895
|
/**
|
|
725
|
-
*
|
|
726
|
-
* @param {string} source
|
|
727
|
-
* @returns {boolean}
|
|
896
|
+
* Check if the source content contains front matter (YAML metadata)
|
|
897
|
+
* @param {string} source - The source content to check
|
|
898
|
+
* @returns {boolean} True if front matter is present, false otherwise
|
|
899
|
+
* @example
|
|
900
|
+
* const hasFM = ecto.hasFrontMatter('---\ntitle: Test\n---\nContent');
|
|
728
901
|
*/
|
|
729
902
|
hasFrontMatter(source) {
|
|
730
903
|
const writr = new Writr2(source);
|
|
@@ -734,19 +907,23 @@ var Ecto = class extends Hookified {
|
|
|
734
907
|
return false;
|
|
735
908
|
}
|
|
736
909
|
/**
|
|
737
|
-
*
|
|
738
|
-
* @param {string} source
|
|
739
|
-
* @returns {Record<string, unknown>}
|
|
910
|
+
* Extract front matter data from the source content
|
|
911
|
+
* @param {string} source - The source content containing front matter
|
|
912
|
+
* @returns {Record<string, unknown>} Parsed front matter as an object
|
|
913
|
+
* @example
|
|
914
|
+
* const data = ecto.getFrontMatter('---\ntitle: Test\n---\nContent');
|
|
740
915
|
*/
|
|
741
916
|
getFrontMatter(source) {
|
|
742
917
|
const writr = new Writr2(source);
|
|
743
918
|
return writr.frontMatter;
|
|
744
919
|
}
|
|
745
920
|
/**
|
|
746
|
-
*
|
|
747
|
-
* @param {string} source - The source
|
|
748
|
-
* @param {Record<string, unknown>} data - The front matter data
|
|
749
|
-
* @returns {string}
|
|
921
|
+
* Set or replace front matter in the source content
|
|
922
|
+
* @param {string} source - The source content
|
|
923
|
+
* @param {Record<string, unknown>} data - The front matter data to set
|
|
924
|
+
* @returns {string} The source content with updated front matter
|
|
925
|
+
* @example
|
|
926
|
+
* const updated = ecto.setFrontMatter('Content', { title: 'New Title' });
|
|
750
927
|
*/
|
|
751
928
|
setFrontMatter(source, data) {
|
|
752
929
|
const writr = new Writr2(source);
|
|
@@ -754,20 +931,36 @@ var Ecto = class extends Hookified {
|
|
|
754
931
|
return writr.content;
|
|
755
932
|
}
|
|
756
933
|
/**
|
|
757
|
-
* Remove the
|
|
758
|
-
* @param {string} source
|
|
759
|
-
* @returns {string}
|
|
934
|
+
* Remove front matter from the source content, returning only the body
|
|
935
|
+
* @param {string} source - The source content with front matter
|
|
936
|
+
* @returns {string} The source content without front matter
|
|
937
|
+
* @example
|
|
938
|
+
* const body = ecto.removeFrontMatter('---\ntitle: Test\n---\nContent');
|
|
760
939
|
*/
|
|
761
940
|
removeFrontMatter(source) {
|
|
762
941
|
const writr = new Writr2(source);
|
|
763
942
|
return writr.body;
|
|
764
943
|
}
|
|
944
|
+
/**
|
|
945
|
+
* Write content to a file asynchronously, creating directories if needed
|
|
946
|
+
* @private
|
|
947
|
+
* @param {string} [filePath] - The path to write the file to
|
|
948
|
+
* @param {string} [source] - The content to write to the file
|
|
949
|
+
* @returns {Promise<void>}
|
|
950
|
+
*/
|
|
765
951
|
async writeFile(filePath, source) {
|
|
766
952
|
if (filePath && source) {
|
|
767
953
|
await this.ensureFilePath(filePath);
|
|
768
954
|
await fs2.promises.writeFile(filePath, source);
|
|
769
955
|
}
|
|
770
956
|
}
|
|
957
|
+
/**
|
|
958
|
+
* Write content to a file synchronously, creating directories if needed
|
|
959
|
+
* @private
|
|
960
|
+
* @param {string} [filePath] - The path to write the file to
|
|
961
|
+
* @param {string} [source] - The content to write to the file
|
|
962
|
+
* @returns {void}
|
|
963
|
+
*/
|
|
771
964
|
writeFileSync(filePath, source) {
|
|
772
965
|
if (filePath && source) {
|
|
773
966
|
this.ensureFilePathSync(filePath);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ecto",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"description": "Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Liquid, and Handlebars",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/ecto.js",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"liquidjs"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@jaredwray/fumanchu": "^4.
|
|
52
|
+
"@jaredwray/fumanchu": "^4.3.0",
|
|
53
53
|
"cacheable": "^2.0.1",
|
|
54
54
|
"ejs": "^3.1.10",
|
|
55
55
|
"hookified": "^1.12.1",
|
|
@@ -61,13 +61,14 @@
|
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@biomejs/biome": "^2.2.4",
|
|
64
|
+
"@faker-js/faker": "^10.0.0",
|
|
64
65
|
"@types/ejs": "^3.1.5",
|
|
65
66
|
"@types/node": "^24.5.2",
|
|
66
67
|
"@types/nunjucks": "^3.2.6",
|
|
67
68
|
"@types/pug": "^2.0.10",
|
|
68
69
|
"@types/underscore": "^1.13.0",
|
|
69
70
|
"@vitest/coverage-v8": "^3.2.4",
|
|
70
|
-
"docula": "^0.
|
|
71
|
+
"docula": "^0.30.0",
|
|
71
72
|
"rimraf": "^6.0.1",
|
|
72
73
|
"tsup": "^8.5.0",
|
|
73
74
|
"typescript": "^5.9.2",
|