ecto 4.8.3 → 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 +26 -24
- package/dist/ecto.d.ts +0 -400
- package/dist/ecto.js +0 -1042
package/README.md
CHANGED
|
@@ -715,108 +715,75 @@ await ecto.render(source, data); // <h1>Hello John Doe!</h1>
|
|
|
715
715
|
|
|
716
716
|
# Hooks
|
|
717
717
|
|
|
718
|
-
Ecto supports hooks that
|
|
718
|
+
Ecto supports hooks that intercept and modify data during the render pipeline. Unlike events (notifications only), hooks can transform the source, data, or result.
|
|
719
719
|
|
|
720
720
|
## Available Hooks
|
|
721
721
|
|
|
722
|
-
| Hook
|
|
723
|
-
|
|
|
724
|
-
| `beforeRender`
|
|
725
|
-
| `afterRender`
|
|
726
|
-
| `beforeRenderSync` | Called before sync rendering. Allows modifying source and data. |
|
|
727
|
-
| `afterRenderSync` | Called after sync rendering. Allows modifying the result. |
|
|
728
|
-
|
|
729
|
-
## Hook Context
|
|
730
|
-
|
|
731
|
-
The `beforeRender` and `beforeRenderSync` hooks receive a `RenderContext` object:
|
|
722
|
+
| Hook | Receives | Purpose |
|
|
723
|
+
| ---- | -------- | ------- |
|
|
724
|
+
| `beforeRender` / `beforeRenderSync` | `RenderContext` | Modify source and data before rendering. |
|
|
725
|
+
| `afterRender` / `afterRenderSync` | `RenderResult` | Modify the result after rendering. |
|
|
732
726
|
|
|
733
727
|
```typescript
|
|
734
728
|
type RenderContext = {
|
|
735
|
-
source: string;
|
|
729
|
+
source: string; // Template source (modifiable)
|
|
736
730
|
data?: Record<string, unknown>; // Template data (modifiable)
|
|
737
|
-
engineName: string;
|
|
738
|
-
rootTemplatePath?: string;
|
|
739
|
-
filePathOutput?: string;
|
|
740
|
-
cached: boolean;
|
|
731
|
+
engineName: string; // Engine being used
|
|
732
|
+
rootTemplatePath?: string; // Root path for partials
|
|
733
|
+
filePathOutput?: string; // Output file path
|
|
734
|
+
cached: boolean; // True if result came from cache
|
|
741
735
|
};
|
|
742
|
-
```
|
|
743
|
-
|
|
744
|
-
The `afterRender` and `afterRenderSync` hooks receive a `RenderResult` object:
|
|
745
736
|
|
|
746
|
-
```typescript
|
|
747
737
|
type RenderResult = {
|
|
748
|
-
result: string;
|
|
749
|
-
context: RenderContext;
|
|
738
|
+
result: string; // Rendered output (modifiable)
|
|
739
|
+
context: RenderContext; // Original render context
|
|
750
740
|
};
|
|
751
741
|
```
|
|
752
742
|
|
|
753
743
|
## Using Hooks
|
|
754
744
|
|
|
755
|
-
Register hooks
|
|
745
|
+
Register hooks with `onHook`. Multiple hooks for the same event run in registration order, and the sync variants (`beforeRenderSync`, `afterRenderSync`) work identically.
|
|
756
746
|
|
|
757
747
|
```javascript
|
|
758
748
|
import { Ecto, EctoEvents } from 'ecto';
|
|
759
749
|
|
|
760
750
|
const ecto = new Ecto();
|
|
761
751
|
|
|
762
|
-
// Modify source before rendering
|
|
752
|
+
// Modify source / inject data before rendering
|
|
763
753
|
ecto.onHook(EctoEvents.beforeRender, (context) => {
|
|
764
754
|
context.source = context.source.replace('{{placeholder}}', '{{replaced}}');
|
|
755
|
+
context.data = { ...(context.data ?? {}), injectedValue: 'hello' };
|
|
765
756
|
});
|
|
766
757
|
|
|
767
|
-
//
|
|
768
|
-
ecto.onHook(EctoEvents.beforeRender, (context) => {
|
|
769
|
-
context.data = { ...context.data, injectedValue: 'hello' };
|
|
770
|
-
});
|
|
771
|
-
|
|
772
|
-
// Transform result after rendering
|
|
758
|
+
// Transform the result after rendering
|
|
773
759
|
ecto.onHook(EctoEvents.afterRender, (renderResult) => {
|
|
760
|
+
if (renderResult.context.cached) console.log('from cache');
|
|
774
761
|
renderResult.result = renderResult.result.toUpperCase();
|
|
775
762
|
});
|
|
776
763
|
|
|
777
|
-
// Check if result was cached
|
|
778
|
-
ecto.onHook(EctoEvents.afterRender, (renderResult) => {
|
|
779
|
-
if (renderResult.context.cached) {
|
|
780
|
-
console.log('Result came from cache');
|
|
781
|
-
}
|
|
782
|
-
});
|
|
783
|
-
|
|
784
764
|
const output = await ecto.render('<%= name %>', { name: 'World' });
|
|
785
765
|
```
|
|
786
766
|
|
|
787
|
-
##
|
|
767
|
+
## Hook Objects, IDs, and Errors
|
|
768
|
+
|
|
769
|
+
Ecto extends [Hookified](https://hookified.org), so you can also register hooks via the `Hook` class. Every hook has a unique `id` (auto-generated unless supplied) for later lookup or removal across events. Pass `throwOnHookError: true` to the `Ecto` constructor to make hook errors fail the render instead of just emitting on `error`.
|
|
788
770
|
|
|
789
771
|
```javascript
|
|
790
772
|
import { Ecto, EctoEvents } from 'ecto';
|
|
773
|
+
import { Hook } from 'hookified';
|
|
791
774
|
|
|
792
|
-
const ecto = new Ecto();
|
|
793
|
-
|
|
794
|
-
// Works the same for sync rendering
|
|
795
|
-
ecto.onHook(EctoEvents.beforeRenderSync, (context) => {
|
|
796
|
-
context.data = { ...context.data, timestamp: Date.now() };
|
|
797
|
-
});
|
|
798
|
-
|
|
799
|
-
ecto.onHook(EctoEvents.afterRenderSync, (renderResult) => {
|
|
800
|
-
renderResult.result = `<!-- Rendered at ${Date.now()} -->\n${renderResult.result}`;
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
const output = ecto.renderSync('<%= name %>', { name: 'World' });
|
|
804
|
-
```
|
|
805
|
-
|
|
806
|
-
## Multiple Hooks
|
|
775
|
+
const ecto = new Ecto({ throwOnHookError: true });
|
|
807
776
|
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
});
|
|
777
|
+
const injectUser = new Hook(
|
|
778
|
+
EctoEvents.beforeRender,
|
|
779
|
+
(context) => { context.data = { ...(context.data ?? {}), user: 'jared' }; },
|
|
780
|
+
'inject-user',
|
|
781
|
+
);
|
|
782
|
+
ecto.onHook(injectUser); // append to chain
|
|
783
|
+
ecto.onHook(injectUser, { position: 'Top' }); // or insert at top
|
|
815
784
|
|
|
816
|
-
ecto.
|
|
817
|
-
|
|
818
|
-
context.source += ' - modified by second';
|
|
819
|
-
});
|
|
785
|
+
ecto.removeHookById('inject-user'); // remove one hook by id
|
|
786
|
+
ecto.removeEventHooks(EctoEvents.afterRender); // or all hooks for an event
|
|
820
787
|
```
|
|
821
788
|
|
|
822
789
|
# Creating Custom Engines
|
package/dist/ecto.d.mts
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { Cacheable, CacheableMemory } from "cacheable";
|
|
2
|
+
import { Hookified, HookifiedOptions } from "hookified";
|
|
3
|
+
|
|
4
|
+
//#region src/engine-interface.d.ts
|
|
5
|
+
type EngineInterface = {
|
|
6
|
+
names: string[];
|
|
7
|
+
engine: any;
|
|
8
|
+
opts?: Record<string, unknown>;
|
|
9
|
+
rootTemplatePath?: string;
|
|
10
|
+
render(source: string, data?: Record<string, unknown>): Promise<string>;
|
|
11
|
+
renderSync(source: string, data?: Record<string, unknown>): string;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/engine-map.d.ts
|
|
15
|
+
declare class EngineMap {
|
|
16
|
+
private readonly _mappings;
|
|
17
|
+
set(name: string, extensions: string[]): void;
|
|
18
|
+
delete(name: string): void;
|
|
19
|
+
deleteExtension(name: string, extension: string): void;
|
|
20
|
+
get(name: string): string[] | undefined;
|
|
21
|
+
getName(extension: string): string | undefined;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/base-engine.d.ts
|
|
25
|
+
declare class BaseEngine {
|
|
26
|
+
names: string[];
|
|
27
|
+
opts?: Record<string, unknown>;
|
|
28
|
+
engine: any;
|
|
29
|
+
rootTemplatePath?: string;
|
|
30
|
+
private _extensions;
|
|
31
|
+
getExtensions(): string[];
|
|
32
|
+
setExtensions(extensions: string[]): void;
|
|
33
|
+
deleteExtension(name: string): void;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/engines/ejs.d.ts
|
|
37
|
+
declare class EJS extends BaseEngine implements EngineInterface {
|
|
38
|
+
constructor(options?: Record<string, unknown>);
|
|
39
|
+
render(source: string, data?: Record<string, unknown>): Promise<string>;
|
|
40
|
+
renderSync(source: string, data?: Record<string, unknown>): string;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/engines/handlebars.d.ts
|
|
44
|
+
declare class Handlebars extends BaseEngine implements EngineInterface {
|
|
45
|
+
partialsPath: string[];
|
|
46
|
+
constructor(options?: Record<string, unknown>);
|
|
47
|
+
render(source: string, data?: Record<string, unknown>): Promise<string>;
|
|
48
|
+
renderSync(source: string, data?: Record<string, unknown>): string;
|
|
49
|
+
initPartials(): void;
|
|
50
|
+
registerPartials(partialsPath: string): boolean;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/engines/liquid.d.ts
|
|
54
|
+
declare class Liquid extends BaseEngine implements EngineInterface {
|
|
55
|
+
constructor(options?: Record<string, unknown>);
|
|
56
|
+
render(source: string, data?: Record<string, unknown>): Promise<string>;
|
|
57
|
+
renderSync(source: string, data?: Record<string, unknown>): string;
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/engines/markdown.d.ts
|
|
61
|
+
declare class Markdown extends BaseEngine implements EngineInterface {
|
|
62
|
+
constructor(options?: Record<string, unknown>);
|
|
63
|
+
render(source: string, data?: Record<string, unknown>): Promise<string>;
|
|
64
|
+
renderSync(source: string, data?: Record<string, unknown>): string;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/engines/nunjucks.d.ts
|
|
68
|
+
declare class Nunjucks extends BaseEngine implements EngineInterface {
|
|
69
|
+
constructor(options?: Record<string, unknown>);
|
|
70
|
+
render(source: string, data?: Record<string, unknown>): Promise<string>;
|
|
71
|
+
renderSync(source: string, data?: Record<string, unknown>): string;
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/engines/pug.d.ts
|
|
75
|
+
declare class Pug extends BaseEngine implements EngineInterface {
|
|
76
|
+
constructor(options?: Record<string, unknown>);
|
|
77
|
+
render(source: string, data?: Record<string, unknown>): Promise<string>;
|
|
78
|
+
renderSync(source: string, data?: Record<string, unknown>): string;
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/ecto.d.ts
|
|
82
|
+
type EctoOptions = {
|
|
83
|
+
/**
|
|
84
|
+
* The default engine to use. This can be 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
|
|
85
|
+
* @default 'ejs'
|
|
86
|
+
* @type {string}
|
|
87
|
+
*/
|
|
88
|
+
defaultEngine?: string;
|
|
89
|
+
/**
|
|
90
|
+
* The engine options to pass to each engine
|
|
91
|
+
* @type {Record<string, Record<string, unknown>>}
|
|
92
|
+
* @default {}
|
|
93
|
+
* @example
|
|
94
|
+
* {
|
|
95
|
+
* nunjucks: {
|
|
96
|
+
* autoescape: true
|
|
97
|
+
* },
|
|
98
|
+
* markdown: {
|
|
99
|
+
* html: true
|
|
100
|
+
* }
|
|
101
|
+
* }
|
|
102
|
+
*/
|
|
103
|
+
engineOptions?: Record<string, Record<string, unknown>>;
|
|
104
|
+
/**
|
|
105
|
+
* Caching for async rendered templates. If set to true, it will use the default cacheable options.
|
|
106
|
+
* If set to Cacheable instantce, it will use the provided cacheable instance.
|
|
107
|
+
* @type {boolean | Cacheable}
|
|
108
|
+
* @default false
|
|
109
|
+
*/
|
|
110
|
+
cache?: boolean | Cacheable;
|
|
111
|
+
/**
|
|
112
|
+
* If set to true, it will cache the rendered templates synchronously when running renderSync.
|
|
113
|
+
* If set to CacheableMemory instance, it will use the provided cacheable memory instance.
|
|
114
|
+
* @type {boolean | CacheableMemory}
|
|
115
|
+
* @default false
|
|
116
|
+
*/
|
|
117
|
+
cacheSync?: boolean | CacheableMemory;
|
|
118
|
+
} & HookifiedOptions;
|
|
119
|
+
/**
|
|
120
|
+
* Context passed to beforeRender and beforeRenderSync hooks
|
|
121
|
+
*/
|
|
122
|
+
type RenderContext = {
|
|
123
|
+
source: string;
|
|
124
|
+
data?: Record<string, unknown>;
|
|
125
|
+
engineName: string;
|
|
126
|
+
rootTemplatePath?: string;
|
|
127
|
+
filePathOutput?: string;
|
|
128
|
+
cached: boolean;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Result passed to afterRender and afterRenderSync hooks
|
|
132
|
+
*/
|
|
133
|
+
type RenderResult = {
|
|
134
|
+
result: string;
|
|
135
|
+
context: RenderContext;
|
|
136
|
+
};
|
|
137
|
+
declare enum EctoEvents {
|
|
138
|
+
cacheHit = "cacheHit",
|
|
139
|
+
cacheMiss = "cacheMiss",
|
|
140
|
+
warn = "warn",
|
|
141
|
+
error = "error",
|
|
142
|
+
beforeRender = "beforeRender",
|
|
143
|
+
afterRender = "afterRender",
|
|
144
|
+
beforeRenderSync = "beforeRenderSync",
|
|
145
|
+
afterRenderSync = "afterRenderSync"
|
|
146
|
+
}
|
|
147
|
+
declare class Ecto extends Hookified {
|
|
148
|
+
private readonly _mapping;
|
|
149
|
+
private readonly _engines;
|
|
150
|
+
private _cache;
|
|
151
|
+
private _cacheSync;
|
|
152
|
+
private _defaultEngine;
|
|
153
|
+
private readonly _ejs;
|
|
154
|
+
private readonly _markdown;
|
|
155
|
+
private readonly _pug;
|
|
156
|
+
private readonly _nunjucks;
|
|
157
|
+
private readonly _handlebars;
|
|
158
|
+
private readonly _liquid;
|
|
159
|
+
/**
|
|
160
|
+
* Ecto constructor
|
|
161
|
+
* @param {EctoOptions} [options] - The options for the ecto engine
|
|
162
|
+
*/
|
|
163
|
+
constructor(options?: EctoOptions);
|
|
164
|
+
/**
|
|
165
|
+
* Get the default engine
|
|
166
|
+
* @returns {string} - the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
|
|
167
|
+
*/
|
|
168
|
+
get defaultEngine(): string;
|
|
169
|
+
/**
|
|
170
|
+
* Set the default engine
|
|
171
|
+
* @param {string} value the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
|
|
172
|
+
*/
|
|
173
|
+
set defaultEngine(value: string);
|
|
174
|
+
/**
|
|
175
|
+
* Get the cacheable instance
|
|
176
|
+
* @returns {Cacheable | undefined} - The cacheable instance or undefined if caching is disabled
|
|
177
|
+
*/
|
|
178
|
+
get cache(): Cacheable | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* Set the cacheable instance
|
|
181
|
+
* @param {Cacheable | undefined} value - The cacheable instance to set. If set to undefined, caching will be disabled.
|
|
182
|
+
*/
|
|
183
|
+
set cache(value: Cacheable | undefined);
|
|
184
|
+
/**
|
|
185
|
+
* Get the cacheable memory instance
|
|
186
|
+
* @returns {CacheableMemory | undefined} - The cacheable memory instance or undefined if caching is disabled
|
|
187
|
+
*/
|
|
188
|
+
get cacheSync(): CacheableMemory | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* Set the cacheable memory instance
|
|
191
|
+
* @param {CacheableMemory | undefined} value - The cacheable memory instance to set. If set to undefined, caching will be disabled.
|
|
192
|
+
*/
|
|
193
|
+
set cacheSync(value: CacheableMemory | undefined);
|
|
194
|
+
/**
|
|
195
|
+
* Get the Engine Mappings. This is used to map file extensions to engines
|
|
196
|
+
* @returns {EngineMap}
|
|
197
|
+
*/
|
|
198
|
+
get mappings(): EngineMap;
|
|
199
|
+
/**
|
|
200
|
+
* Get the EJS Engine
|
|
201
|
+
* @returns {EJS}
|
|
202
|
+
*/
|
|
203
|
+
get ejs(): EJS;
|
|
204
|
+
/**
|
|
205
|
+
* Get the Markdown Engine
|
|
206
|
+
* @returns {Markdown}
|
|
207
|
+
*/
|
|
208
|
+
get markdown(): Markdown;
|
|
209
|
+
/**
|
|
210
|
+
* Get the Pug Engine
|
|
211
|
+
* @returns {Pug}
|
|
212
|
+
*/
|
|
213
|
+
get pug(): Pug;
|
|
214
|
+
/**
|
|
215
|
+
* Get the Nunjucks Engine
|
|
216
|
+
* @returns {Nunjucks}
|
|
217
|
+
*/
|
|
218
|
+
get nunjucks(): Nunjucks;
|
|
219
|
+
/**
|
|
220
|
+
* Get the Handlebars Engine
|
|
221
|
+
* @returns {Handlebars}
|
|
222
|
+
*/
|
|
223
|
+
get handlebars(): Handlebars;
|
|
224
|
+
/**
|
|
225
|
+
* Get the Liquid Engine
|
|
226
|
+
* @returns {Liquid}
|
|
227
|
+
*/
|
|
228
|
+
get liquid(): Liquid;
|
|
229
|
+
/**
|
|
230
|
+
* Asynchronously render a template source with data using the specified engine
|
|
231
|
+
* @param {string} source - The template source string to render
|
|
232
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
233
|
+
* @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
|
|
234
|
+
* @param {string} [rootTemplatePath] - Root directory path for template includes/partials resolution
|
|
235
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
236
|
+
* @returns {Promise<string>} The rendered template output as a string
|
|
237
|
+
* @example
|
|
238
|
+
* const result = await ecto.render('<%= name %>', { name: 'World' }, 'ejs');
|
|
239
|
+
*/
|
|
240
|
+
render(source: string, data?: Record<string, unknown>, engineName?: string, rootTemplatePath?: string, filePathOutput?: string): Promise<string>;
|
|
241
|
+
/**
|
|
242
|
+
* Synchronously render a template source with data using the specified engine
|
|
243
|
+
* @param {string} source - The template source string to render
|
|
244
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
245
|
+
* @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
|
|
246
|
+
* @param {string} [rootTemplatePath] - Root directory path for template includes/partials resolution
|
|
247
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
248
|
+
* @returns {string} The rendered template output as a string
|
|
249
|
+
* @example
|
|
250
|
+
* const result = ecto.renderSync('<%= name %>', { name: 'World' }, 'ejs');
|
|
251
|
+
*/
|
|
252
|
+
renderSync(source: string, data?: Record<string, unknown>, engineName?: string, rootTemplatePath?: string, filePathOutput?: string): string;
|
|
253
|
+
/**
|
|
254
|
+
* Asynchronously render a template from a file path
|
|
255
|
+
* @param {string} filePath - Path to the template file to render
|
|
256
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
257
|
+
* @param {string} [rootTemplatePath] - Root directory for template includes. Defaults to file's directory
|
|
258
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
259
|
+
* @param {string} [engineName] - Engine to use. If not specified, determined from file extension
|
|
260
|
+
* @returns {Promise<string>} The rendered template output as a string
|
|
261
|
+
* @example
|
|
262
|
+
* const result = await ecto.renderFromFile('./templates/index.ejs', { title: 'Home' });
|
|
263
|
+
*/
|
|
264
|
+
renderFromFile(filePath: string, data?: Record<string, unknown>, rootTemplatePath?: string, filePathOutput?: string, engineName?: string): Promise<string>;
|
|
265
|
+
/**
|
|
266
|
+
* Synchronously render a template from a file path
|
|
267
|
+
* @param {string} filePath - Path to the template file to render
|
|
268
|
+
* @param {Record<string, unknown>} [data] - Data object to pass to the template engine
|
|
269
|
+
* @param {string} [rootTemplatePath] - Root directory for template includes. Defaults to file's directory
|
|
270
|
+
* @param {string} [filePathOutput] - Optional file path to write the rendered output to
|
|
271
|
+
* @param {string} [engineName] - Engine to use. If not specified, determined from file extension
|
|
272
|
+
* @returns {string} The rendered template output as a string
|
|
273
|
+
* @example
|
|
274
|
+
* const result = ecto.renderFromFileSync('./templates/index.ejs', { title: 'Home' });
|
|
275
|
+
*/
|
|
276
|
+
renderFromFileSync(filePath: string, data?: Record<string, unknown>, rootTemplatePath?: string, filePathOutput?: string, engineName?: string): string;
|
|
277
|
+
/**
|
|
278
|
+
* Asynchronously ensure that the directory path for a file exists, creating it if necessary
|
|
279
|
+
* @param {string} path - The full file path (directories will be extracted from this)
|
|
280
|
+
* @returns {Promise<void>}
|
|
281
|
+
* @example
|
|
282
|
+
* await ecto.ensureFilePath('/path/to/file.txt');
|
|
283
|
+
*/
|
|
284
|
+
ensureFilePath(path: string): Promise<void>;
|
|
285
|
+
/**
|
|
286
|
+
* Synchronously ensure that the directory path for a file exists, creating it if necessary
|
|
287
|
+
* @param {string} path - The full file path (directories will be extracted from this)
|
|
288
|
+
* @returns {void}
|
|
289
|
+
* @example
|
|
290
|
+
* ecto.ensureFilePathSync('/path/to/file.txt');
|
|
291
|
+
*/
|
|
292
|
+
ensureFilePathSync(path: string): void;
|
|
293
|
+
/**
|
|
294
|
+
* Determine the appropriate template engine based on a file's extension
|
|
295
|
+
* @param {string} filePath - The file path to analyze
|
|
296
|
+
* @returns {string} The engine name (e.g., 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid')
|
|
297
|
+
* @example
|
|
298
|
+
* const engine = ecto.getEngineByFilePath('template.ejs'); // Returns 'ejs'
|
|
299
|
+
*/
|
|
300
|
+
getEngineByFilePath(filePath: string): string;
|
|
301
|
+
/**
|
|
302
|
+
* Asynchronously find a template file in a directory by name, regardless of extension
|
|
303
|
+
* @param {string} path - Directory path to search in
|
|
304
|
+
* @param {string} templateName - Template name without extension
|
|
305
|
+
* @returns {Promise<string>} Full path to the found template file, or empty string if not found
|
|
306
|
+
* @example
|
|
307
|
+
* const templatePath = await ecto.findTemplateWithoutExtension('./templates', 'index');
|
|
308
|
+
*/
|
|
309
|
+
findTemplateWithoutExtension(path: string, templateName: string): Promise<string>;
|
|
310
|
+
/**
|
|
311
|
+
* Synchronously find a template file in a directory by name, regardless of extension
|
|
312
|
+
* @param {string} path - Directory path to search in
|
|
313
|
+
* @param {string} templateName - Template name without extension
|
|
314
|
+
* @returns {string} Full path to the found template file, or empty string if not found
|
|
315
|
+
* @example
|
|
316
|
+
* const templatePath = ecto.findTemplateWithoutExtensionSync('./templates', 'index');
|
|
317
|
+
*/
|
|
318
|
+
findTemplateWithoutExtensionSync(path: string, templateName: string): string;
|
|
319
|
+
/**
|
|
320
|
+
* Check if the given engine name is valid and registered in Ecto
|
|
321
|
+
* @param {string} [engineName] - The engine name to validate
|
|
322
|
+
* @returns {boolean} True if the engine is valid and registered, false otherwise
|
|
323
|
+
* @example
|
|
324
|
+
* const isValid = ecto.isValidEngine('ejs'); // Returns true
|
|
325
|
+
*/
|
|
326
|
+
isValidEngine(engineName?: string): boolean;
|
|
327
|
+
/**
|
|
328
|
+
* Detect the template engine from a template string by analyzing its syntax
|
|
329
|
+
* @param {string} source - The template source string to analyze
|
|
330
|
+
* @returns {string} The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine
|
|
331
|
+
* @example
|
|
332
|
+
* const engine = ecto.detectEngine('<%= name %>'); // Returns 'ejs'
|
|
333
|
+
* const engine2 = ecto.detectEngine('{{name}}'); // Returns 'handlebars' or 'liquid'
|
|
334
|
+
* const engine3 = ecto.detectEngine('# Heading'); // Returns 'markdown'
|
|
335
|
+
* const engine4 = ecto.detectEngine('plain text'); // Returns defaultEngine (e.g., 'ejs')
|
|
336
|
+
*/
|
|
337
|
+
detectEngine(source: string): string;
|
|
338
|
+
/**
|
|
339
|
+
* Register all engine mappings between engine names and file extensions
|
|
340
|
+
* @returns {void}
|
|
341
|
+
* @private
|
|
342
|
+
*/
|
|
343
|
+
registerEngineMappings(): void;
|
|
344
|
+
/**
|
|
345
|
+
* Get the render engine instance by name
|
|
346
|
+
* @param {string} engineName - The name of the engine to retrieve
|
|
347
|
+
* @returns {EngineInterface} The engine instance (defaults to EJS if not found)
|
|
348
|
+
* @example
|
|
349
|
+
* const engine = ecto.getRenderEngine('pug');
|
|
350
|
+
*/
|
|
351
|
+
getRenderEngine(engineName: string): EngineInterface;
|
|
352
|
+
/**
|
|
353
|
+
* Check if the source content contains front matter (YAML metadata)
|
|
354
|
+
* @param {string} source - The source content to check
|
|
355
|
+
* @returns {boolean} True if front matter is present, false otherwise
|
|
356
|
+
* @example
|
|
357
|
+
* const hasFM = ecto.hasFrontMatter('---\ntitle: Test\n---\nContent');
|
|
358
|
+
*/
|
|
359
|
+
hasFrontMatter(source: string): boolean;
|
|
360
|
+
/**
|
|
361
|
+
* Extract front matter data from the source content
|
|
362
|
+
* @param {string} source - The source content containing front matter
|
|
363
|
+
* @returns {Record<string, unknown>} Parsed front matter as an object
|
|
364
|
+
* @example
|
|
365
|
+
* const data = ecto.getFrontMatter('---\ntitle: Test\n---\nContent');
|
|
366
|
+
*/
|
|
367
|
+
getFrontMatter(source: string): Record<string, unknown>;
|
|
368
|
+
/**
|
|
369
|
+
* Set or replace front matter in the source content
|
|
370
|
+
* @param {string} source - The source content
|
|
371
|
+
* @param {Record<string, unknown>} data - The front matter data to set
|
|
372
|
+
* @returns {string} The source content with updated front matter
|
|
373
|
+
* @example
|
|
374
|
+
* const updated = ecto.setFrontMatter('Content', { title: 'New Title' });
|
|
375
|
+
*/
|
|
376
|
+
setFrontMatter(source: string, data: Record<string, unknown>): string;
|
|
377
|
+
/**
|
|
378
|
+
* Remove front matter from the source content, returning only the body
|
|
379
|
+
* @param {string} source - The source content with front matter
|
|
380
|
+
* @returns {string} The source content without front matter
|
|
381
|
+
* @example
|
|
382
|
+
* const body = ecto.removeFrontMatter('---\ntitle: Test\n---\nContent');
|
|
383
|
+
*/
|
|
384
|
+
removeFrontMatter(source: string): string;
|
|
385
|
+
/**
|
|
386
|
+
* Write content to a file asynchronously, creating directories if needed
|
|
387
|
+
* @private
|
|
388
|
+
* @param {string} [filePath] - The path to write the file to
|
|
389
|
+
* @param {string} [source] - The content to write to the file
|
|
390
|
+
* @returns {Promise<void>}
|
|
391
|
+
*/
|
|
392
|
+
private writeFile;
|
|
393
|
+
/**
|
|
394
|
+
* Write content to a file synchronously, creating directories if needed
|
|
395
|
+
* @private
|
|
396
|
+
* @param {string} [filePath] - The path to write the file to
|
|
397
|
+
* @param {string} [source] - The content to write to the file
|
|
398
|
+
* @returns {void}
|
|
399
|
+
*/
|
|
400
|
+
private writeFileSync;
|
|
401
|
+
}
|
|
402
|
+
//#endregion
|
|
403
|
+
export { type BaseEngine, EJS, Ecto, EctoEvents, EctoOptions, type EngineInterface, Handlebars, Liquid, Markdown, Nunjucks, Pug, RenderContext, RenderResult };
|