mikel 0.17.1 → 0.18.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.
Files changed (3) hide show
  1. package/README.md +29 -0
  2. package/index.js +20 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -161,6 +161,35 @@ const result = m("{{>>foo}}Bob{{/foo}}", {}, options);
161
161
  // Output: 'Hello Bob!'
162
162
  ```
163
163
 
164
+ #### Partials data
165
+
166
+ > This feature was added in `v0.18.0`.
167
+
168
+ Partials allows you to define custom data. Instead of providing a string with the partial content, you can provide an object with the following keys:
169
+
170
+ - `body`: a string with the partial content.
171
+ - `data`: an object with your custom data for the partial. You can also use `attributes` as an alias.
172
+
173
+ Custom data will be available in the partial content as a variable `@partial`.
174
+
175
+ Example:
176
+
177
+ ```javascript
178
+ const options = {
179
+ partials: {
180
+ foo: {
181
+ body: "Hello {{@partial.name}}!",
182
+ data: {
183
+ name: "Bob",
184
+ },
185
+ },
186
+ },
187
+ };
188
+
189
+ const result = m("{{> foo}}", {}, options);
190
+ // Output: 'Hello Bob!'
191
+ ```
192
+
164
193
  ### Inline partials
165
194
 
166
195
  > This feature was added in `v0.16.0`.
package/index.js CHANGED
@@ -35,6 +35,18 @@ const parse = (v, context = {}, vars = {}) => {
35
35
  return (v || "").startsWith("@") ? get(vars, v.slice(1)) : get(context, v || ".");
36
36
  };
37
37
 
38
+ // @description tiny front-matter parser
39
+ const frontmatter = (str = "", parser = null) => {
40
+ let body = (str || "").trim(), data = {};
41
+ const matches = Array.from(body.matchAll(/^(--- *)/gm))
42
+ if (matches?.length === 2 && matches[0].index === 0) {
43
+ const front = body.substring(0 + matches[0][1].length, matches[1].index).trim();
44
+ body = body.substring(matches[1].index + matches[1][1].length).trim();
45
+ data = typeof parser === "function" ? parser(front) : front;
46
+ }
47
+ return {body, data};
48
+ };
49
+
38
50
  // @description default helpers
39
51
  const defaultHelpers = {
40
52
  "each": p => {
@@ -112,10 +124,14 @@ const create = (template = "", options = {}) => {
112
124
  if (tokens[i].startsWith(">>")) {
113
125
  i = compile(tokens, blockContent, context, vars, i + 1, t);
114
126
  }
115
- if (typeof partials[t] === "string") {
127
+ if (typeof partials[t] === "string" || typeof partials[t]?.body === "string") {
116
128
  const newCtx = args.length > 0 ? args[0] : (Object.keys(opt).length > 0 ? opt : context);
117
- const newVars = {...vars, content: blockContent.join("")};
118
- compile(tokenize(partials[t]), output, newCtx, newVars, 0, "");
129
+ const newVars = {
130
+ ...vars,
131
+ content: blockContent.join(""),
132
+ partial: partials[t]?.attributes || partials[t]?.data || {},
133
+ };
134
+ compile(tokenize(partials[t]?.body || partials[t]), output, newCtx, newVars, 0, "");
119
135
  }
120
136
  }
121
137
  else if (tokens[i].startsWith("=")) {
@@ -171,5 +187,6 @@ mikel.create = create;
171
187
  mikel.escape = escape;
172
188
  mikel.get = get;
173
189
  mikel.parse = parse;
190
+ mikel.frontmatter = frontmatter;
174
191
 
175
192
  export default mikel;
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.17.1",
4
+ "version": "0.18.0",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Josemi Juanes",