mikel 0.38.0 → 0.38.1
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/index.d.ts +4 -0
- package/index.js +6 -3
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -26,6 +26,8 @@ export type MikelFunction = (params: {
|
|
|
26
26
|
|
|
27
27
|
export type MikelState = Record<string, string>;
|
|
28
28
|
|
|
29
|
+
export type MikelTransform = (content: string) => string;
|
|
30
|
+
|
|
29
31
|
export type MikelOptions = {
|
|
30
32
|
helpers?: Record<string, MikelHelper>;
|
|
31
33
|
partials?: Record<string, MikelPartial>;
|
|
@@ -41,6 +43,8 @@ export type MikelContext = {
|
|
|
41
43
|
functions: Record<string, MikelFunction>;
|
|
42
44
|
partials: Record<string, MikelPartial>;
|
|
43
45
|
initialState: MikelState;
|
|
46
|
+
preTransforms: MikelTransform[];
|
|
47
|
+
postTransforms: MikelTransform[];
|
|
44
48
|
};
|
|
45
49
|
|
|
46
50
|
export type MikelPlugin = (ctx: MikelContext) => void;
|
package/index.js
CHANGED
|
@@ -135,6 +135,7 @@ const compile = (ctx, tokens, output, data, state, index = 0, section = "") => {
|
|
|
135
135
|
const j = i + 1;
|
|
136
136
|
i = findClosingToken(tokens, j, t);
|
|
137
137
|
output.push(ctx.helpers[t]({
|
|
138
|
+
context: ctx,
|
|
138
139
|
args: args,
|
|
139
140
|
options: opt,
|
|
140
141
|
tokens: tokens.slice(j, i),
|
|
@@ -280,12 +281,14 @@ const create = (options = {}) => {
|
|
|
280
281
|
partials: Object.assign({}, options?.partials || {}),
|
|
281
282
|
functions: Object.assign({}, options?.functions || {}),
|
|
282
283
|
initialState: {}, // Object.assign({}, options?.initialState || {}),
|
|
284
|
+
preTransforms: [], // list of pre-transform to apply to the template
|
|
285
|
+
postTransforms: [], // list of post-transforms to apply to the result
|
|
283
286
|
});
|
|
284
287
|
// entry method to compile the template with the provided data object
|
|
285
288
|
const compileTemplate = (template, data = {}) => {
|
|
286
|
-
const output = [];
|
|
287
|
-
compile(ctx, tokenize(
|
|
288
|
-
return output.join("");
|
|
289
|
+
const output = [], input = ctx.preTransforms.reduce((content, fn) => fn(content), template);
|
|
290
|
+
compile(ctx, tokenize(input), output, data, { ...ctx.initialState, root: data }, 0, "");
|
|
291
|
+
return ctx.postTransforms.reduce((result, fn) => fn(result), output.join(""));
|
|
289
292
|
};
|
|
290
293
|
// assign api methods and return method to compile the template
|
|
291
294
|
return Object.assign(compileTemplate, {
|