mikel 0.16.0 → 0.17.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 +10 -0
- package/index.d.ts +48 -0
- package/index.js +1 -13
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -311,6 +311,16 @@ const data = {
|
|
|
311
311
|
console.log(m("{{#with autor}}{{name}} <{{email}}>{{/with}}", data)); // --> 'Bob <bob@email.com>'
|
|
312
312
|
```
|
|
313
313
|
|
|
314
|
+
#### escape
|
|
315
|
+
|
|
316
|
+
> Added in `v0.17.0`.
|
|
317
|
+
|
|
318
|
+
The `escape` helper allows to escape the provided block content.
|
|
319
|
+
|
|
320
|
+
```javascript
|
|
321
|
+
console.log(m("{{#escape}}<b>Hello World!</b>{{/escape}}")); // --> '<b>Hello World!</b>
|
|
322
|
+
```
|
|
323
|
+
|
|
314
324
|
### Custom Helpers
|
|
315
325
|
|
|
316
326
|
> Added in `v0.5.0`.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
declare type HelperFunction = (params: {
|
|
2
|
+
args: any[];
|
|
3
|
+
opt: Record<string, any>;
|
|
4
|
+
context: Record<string, any>;
|
|
5
|
+
fn: (context?: Record<string, any>, vars?: Record<string, any>, output?: string[]) => string;
|
|
6
|
+
}) => string;
|
|
7
|
+
|
|
8
|
+
declare interface Helpers {
|
|
9
|
+
[key: string]: HelperFunction;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare interface Partials {
|
|
13
|
+
[key: string]: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare interface Functions {
|
|
17
|
+
[key: string]: (params: {
|
|
18
|
+
args: any[];
|
|
19
|
+
opt: Record<string, any>;
|
|
20
|
+
context: Record<string, any>;
|
|
21
|
+
}) => string | void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare interface MikelOptions {
|
|
25
|
+
helpers?: Helpers;
|
|
26
|
+
partials?: Partials;
|
|
27
|
+
functions?: Functions;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
declare interface MikelTemplate {
|
|
31
|
+
(data?: any): string;
|
|
32
|
+
addHelper(name: string, fn: HelperFunction): void;
|
|
33
|
+
removeHelper(name: string): void;
|
|
34
|
+
addFunction(name: string, fn: (params: any) => string | void): void;
|
|
35
|
+
removeFunction(name: string): void;
|
|
36
|
+
addPartial(name: string, partial: string): void;
|
|
37
|
+
removePartial(name: string): void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declare const mikel: {
|
|
41
|
+
(template: string, data?: any, options?: Partial<MikelOptions>): string;
|
|
42
|
+
create(template: string, options?: Partial<MikelOptions>): MikelTemplate;
|
|
43
|
+
escape(str: string): string;
|
|
44
|
+
get(context: any, path: string): any;
|
|
45
|
+
parse(value: string, context?: any, vars?: any): any;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default mikel;
|
package/index.js
CHANGED
|
@@ -35,18 +35,6 @@ 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
|
-
|
|
50
38
|
// @description default helpers
|
|
51
39
|
const defaultHelpers = {
|
|
52
40
|
"each": p => {
|
|
@@ -61,6 +49,7 @@ const defaultHelpers = {
|
|
|
61
49
|
"eq": p => p.args[0] === p.args[1] ? p.fn(p.context) : "",
|
|
62
50
|
"ne": p => p.args[0] !== p.args[1] ? p.fn(p.context) : "",
|
|
63
51
|
"with": p => p.fn(p.args[0]),
|
|
52
|
+
"escape": p => escape(p.fn(p.context)),
|
|
64
53
|
};
|
|
65
54
|
|
|
66
55
|
// @description create a new instance of mikel
|
|
@@ -182,6 +171,5 @@ mikel.create = create;
|
|
|
182
171
|
mikel.escape = escape;
|
|
183
172
|
mikel.get = get;
|
|
184
173
|
mikel.parse = parse;
|
|
185
|
-
mikel.frontmatter = frontmatter;
|
|
186
174
|
|
|
187
175
|
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.
|
|
4
|
+
"version": "0.17.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Josemi Juanes",
|
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
"bugs": "https://github.com/jmjuanes/mikel/issues",
|
|
13
13
|
"main": "index.js",
|
|
14
14
|
"module": "index.js",
|
|
15
|
+
"types": "index.d.ts",
|
|
15
16
|
"exports": {
|
|
16
17
|
".": "./index.js",
|
|
17
18
|
"./package.json": "./package.json"
|
|
18
19
|
},
|
|
19
20
|
"scripts": {
|
|
20
|
-
"release": "node scripts/release.js",
|
|
21
21
|
"test": "node test.js"
|
|
22
22
|
},
|
|
23
23
|
"keywords": [
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"files": [
|
|
28
28
|
"README.md",
|
|
29
29
|
"LICENSE",
|
|
30
|
-
"index.js"
|
|
30
|
+
"index.js",
|
|
31
|
+
"index.d.ts"
|
|
31
32
|
]
|
|
32
33
|
}
|