mikel 0.34.0 → 0.35.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/README.md CHANGED
@@ -837,7 +837,7 @@ console.log(mk("{{>hello}}", {name: "Susan"})); // --> "Hello, Susan!"
837
837
 
838
838
  It also exposes the following additional methods:
839
839
 
840
- #### `mk.use(options)`
840
+ #### `mk.use(options | function)`
841
841
 
842
842
  > Added in `v0.19.0`.
843
843
 
@@ -856,12 +856,13 @@ mk.use({
856
856
  });
857
857
  ```
858
858
 
859
- When called with a **function**, the function receives the internal context of the instance, giving full access to the hooks system and all registered helpers, functions, and partials. This is the recommended approach for writing reusable plugins:
859
+ When called with a **function**, the function receives the internal context of the instance, giving full access to all registered helpers, functions, and partials. This is the recommended approach for writing reusable plugins:
860
860
 
861
861
  ```javascript
862
862
  const myPlugin = (ctx) => {
863
- ctx.hooks.add("preRender", template => template.trim());
864
- ctx.hooks.add("postRender", output => output.trim());
863
+ ctx.helpers.uppercase = ({ fn, data }) => {
864
+ return fn(data).toUpperCase();
865
+ };
865
866
  };
866
867
 
867
868
  mk.use(myPlugin);
@@ -927,66 +928,16 @@ This function returns the value in `object` following the provided `path` string
927
928
 
928
929
  ### Built‑in Plugins
929
930
 
930
- > Added in `v0.34.0`.
931
+ > Added in `v0.35.0`.
931
932
 
932
- Mikel includes a small set of built‑in plugins that provide common functionality without requiring additional packages. These plugins integrate directly into the compilation lifecycle and use the same hook system available to any external plugin.
933
+ Mikel includes a small set of built‑in plugins that provide common functionality without requiring additional packages.
933
934
 
934
- #### `mikel.WrapperPlugin(options)`
935
+ #### `mikel.SetStatePlugin(name, value)`
935
936
 
936
- Wraps the original template by inserting custom text before and/or after it. The wrapper is applied before rendering, which means it can contain mikel expressions (`{{variables}}`, helpers, etc.).
937
+ Registers a static state variable that become available inside templates through the `@variable` syntax.
937
938
 
938
939
  ```javascript
939
- mk.use(mikel.WrapperPlugin({
940
- header: "<!-- START -->\n",
941
- footer: "\n<!-- END -->",
942
- }));
943
- ```
944
-
945
- #### `mikel.StatePlugin(state)`
946
-
947
- Defines static state variables that become available inside templates through the `@variable` syntax. These values are merged into the initial state before rendering.
948
-
949
- ```javascript
950
- mk.use(mikel.StatePlugin({
951
- version: "1.0.0",
952
- environment: "development",
953
- }));
954
- ```
955
-
956
- ### Hooks
957
-
958
- > Added in `v0.34.0`.
959
-
960
- Hooks allows plugins to tap into different stages of the rendering pipeline. Hooks are registered using the `hooks.add` method inside a plugin function passed to `mk.use()`.
961
-
962
- ```javascript
963
- mk.use((context) => {
964
- context.hooks.add("someHook", (params) => {
965
- // ...
966
- });
967
- });
968
- ```
969
-
970
- Available hooks:
971
-
972
- | Hook | Type | Receives | Must return | Description |
973
- |------|-------|----------|-------------|-------------|
974
- | `preRender` | Waterfall | `template` (string) | `template` (string) | Called before rendering. The returned value is used as the template. |
975
- | `postRender` | Waterfall | `output` (string) | `output` (string) | Called after rendering. The returned value is used as the final output. |
976
-
977
- Example:
978
-
979
- ```javascript
980
- const myPlugin = (context) => {
981
- // trim the template before rendering
982
- context.hooks.add("preRender", template => template.trim());
983
- // minify the output after rendering
984
- context.hooks.add("postRender", output => output.replace(/\s+/g, " ").trim());
985
- };
986
-
987
- mk.use(myPlugin);
988
-
989
- console.log(mk(" Hello {{name}}! ", { name: "World" })); // --> "Hello World!"
940
+ mk.use(mikel.SetStatePlugin("version", "1.0.0"));
990
941
  ```
991
942
 
992
943
  ## License
package/index.d.ts CHANGED
@@ -40,11 +40,6 @@ export type MikelContext = {
40
40
  helpers: Record<string, MikelFunction>;
41
41
  functions: Record<string, MikelFunction>;
42
42
  partials: Record<string, MikelPartial>;
43
- hooks: {
44
- add: (hookName: string, listener: Function) => void;
45
- call: (hookName: string, ...args: any[]) => void;
46
- callWaterfall: (hookName: string, value: any) => any;
47
- };
48
43
  initialState: MikelState;
49
44
  };
50
45
 
@@ -61,8 +56,7 @@ export type Mikel = {
61
56
  removePartial(name: string): void;
62
57
  };
63
58
 
64
- export type MikelWrapperPlugin = (options: { header?: string, footer?: string }) => MikelPlugin;
65
- export type MikelStatePlugin = (state: MikelState) => MikelPlugin;
59
+ export type MikelSetStatePlugin = (name: string, value: any) => MikelPlugin;
66
60
 
67
61
  declare const mikel: {
68
62
  (template: string, data?: any, options?: Partial<MikelOptions>): string;
@@ -72,8 +66,7 @@ declare const mikel: {
72
66
  parse(value: string, context?: any, vars?: any): any;
73
67
  tokenize(str: string): string[];
74
68
  untokenize(tokens: string[], start?: string, end?: string): string;
75
- WrapperPlugin: MikelWrapperPlugin;
76
- StatePlugin: MikelStatePlugin;
69
+ SetStatePlugin: MikelSetStatePlugin;
77
70
  };
78
71
 
79
72
  export default mikel;
package/index.js CHANGED
@@ -122,24 +122,6 @@ const findClosingToken = (tokens, i, token) => {
122
122
  throw new Error(`Unmatched section end: {{${token}}}`);
123
123
  };
124
124
 
125
- // @description create a hook manager for the provided hooks map
126
- const createHookManager = (hooks = new Map()) => {
127
- return {
128
- add: (hookName, listener) => {
129
- if (!hooks.has(hookName.toLowerCase())) {
130
- hooks.set(hookName.toLowerCase(), []);
131
- }
132
- hooks.get(hookName.toLowerCase()).push(listener);
133
- },
134
- call: (hookName, ...args) => {
135
- hooks.get(hookName.toLowerCase())?.forEach((listener) => listener(...args));
136
- },
137
- callWaterfall: (hookName, value) => {
138
- return (hooks.get(hookName.toLowerCase()) || []).reduce((v, listener) => listener(v), value);
139
- },
140
- };
141
- };
142
-
143
125
  // @description internal method to compile the template
144
126
  const compile = (ctx, tokens, output, data, state, index = 0, section = "") => {
145
127
  let i = index;
@@ -296,14 +278,12 @@ const create = (options = {}) => {
296
278
  partials: Object.assign({}, options?.partials || {}),
297
279
  functions: Object.assign({}, options?.functions || {}),
298
280
  initialState: {}, // Object.assign({}, options?.initialState || {}),
299
- hooks: createHookManager(new Map()),
300
281
  });
301
282
  // entry method to compile the template with the provided data object
302
- const compileTemplate = (originalTemplate, data = {}) => {
283
+ const compileTemplate = (template, data = {}) => {
303
284
  const output = [];
304
- const template = ctx.hooks.callWaterfall("prerender", originalTemplate || "");
305
285
  compile(ctx, tokenize(template), output, data, { ...ctx.initialState, root: data }, 0, "");
306
- return ctx.hooks.callWaterfall("postrender", output.join(""));
286
+ return output.join("");
307
287
  };
308
288
  // assign api methods and return method to compile the template
309
289
  return Object.assign(compileTemplate, {
@@ -331,19 +311,10 @@ const mikel = (template = "", data = {}, options = {}) => {
331
311
  return create(options)(template, data);
332
312
  };
333
313
 
334
- // @description plugin to wrap template with custom text
335
- mikel.WrapperPlugin = (options = {}) => {
336
- return (context) => {
337
- context.hooks.add("prerender", template => {
338
- return [options.header || "", template, options.footer || ""].join("");
339
- });
340
- };
341
- };
342
-
343
- // @description plugin to define state variables
344
- mikel.StatePlugin = (state = {}) => {
314
+ // @description plugin to define a new state variable
315
+ mikel.SetStatePlugin = (name, value) => {
345
316
  return (context) => {
346
- Object.assign(context.initialState, state || {});
317
+ context.initialState[name] = value;
347
318
  };
348
319
  };
349
320
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikel",
3
3
  "description": "Micro templating library with zero dependencies",
4
- "version": "0.34.0",
4
+ "version": "0.35.1",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Josemi Juanes",