mikel 0.22.2 → 0.23.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.
- package/README.md +10 -0
- package/index.js +20 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -360,6 +360,16 @@ The `escape` helper allows to escape the provided block content.
|
|
|
360
360
|
console.log(m("{{#escape}}<b>Hello World!</b>{{/escape}}")); // --> '<b>Hello World!</b>
|
|
361
361
|
```
|
|
362
362
|
|
|
363
|
+
#### raw
|
|
364
|
+
|
|
365
|
+
> Added in `v0.23.0`.
|
|
366
|
+
|
|
367
|
+
The `raw` helper allows to render the content of the block without evaluating it. All the stuff inside the block will be rendered as is, without processing any variables or helpers.
|
|
368
|
+
|
|
369
|
+
```javascript
|
|
370
|
+
console.log(m("{{#raw}}Hello {{name}}!{{/raw}}", {name: "Bob"})); // --> 'Hello {{name}}!'
|
|
371
|
+
```
|
|
372
|
+
|
|
363
373
|
### Custom Helpers
|
|
364
374
|
|
|
365
375
|
> Added in `v0.5.0`.
|
package/index.js
CHANGED
|
@@ -45,6 +45,22 @@ const parse = (v, data = {}, vars = {}) => {
|
|
|
45
45
|
return (v || "").startsWith("@") ? get(vars, v.slice(1)) : get(data, v || ".");
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
+
// @description find the index of the closing token
|
|
49
|
+
const findClosingToken = (tokens, i, token) => {
|
|
50
|
+
while(i < tokens.length) {
|
|
51
|
+
if (i % 2 > 0) {
|
|
52
|
+
if (tokens[i].startsWith("/") && tokens[i].slice(1).trim() === token) {
|
|
53
|
+
return i;
|
|
54
|
+
}
|
|
55
|
+
else if (tokens[i].startsWith("#") && tokens[i].slice(1).trim().split(" ")[0] === token) {
|
|
56
|
+
i = findClosingToken(tokens, i + 1, token);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
i = i + 1;
|
|
60
|
+
}
|
|
61
|
+
throw new Error(`Unmatched section end: {{${token}}}`);
|
|
62
|
+
};
|
|
63
|
+
|
|
48
64
|
// @description default helpers
|
|
49
65
|
const defaultHelpers = {
|
|
50
66
|
"each": p => {
|
|
@@ -60,6 +76,7 @@ const defaultHelpers = {
|
|
|
60
76
|
"ne": p => p.args[0] !== p.args[1] ? p.fn(p.data) : "",
|
|
61
77
|
"with": p => p.fn(p.args[0]),
|
|
62
78
|
"escape": p => escape(p.fn(p.data)),
|
|
79
|
+
"raw": p => untokenize(p.tokens),
|
|
63
80
|
};
|
|
64
81
|
|
|
65
82
|
// @description create a new instance of mikel
|
|
@@ -81,20 +98,18 @@ const create = (template = "", options = {}) => {
|
|
|
81
98
|
else if (tokens[i].startsWith("#") && typeof ctx.helpers[tokens[i].slice(1).trim().split(" ")[0]] === "function") {
|
|
82
99
|
const [t, args, opt] = parseArgs(tokens[i].slice(1), data, vars);
|
|
83
100
|
const j = i + 1;
|
|
101
|
+
i = findClosingToken(tokens, j, t);
|
|
84
102
|
output.push(ctx.helpers[t]({
|
|
85
103
|
args: args,
|
|
86
104
|
opt: opt,
|
|
105
|
+
tokens: tokens.slice(j, i),
|
|
87
106
|
data: data,
|
|
88
107
|
variables: vars,
|
|
89
108
|
fn: (blockData = {}, blockVars = {}, blockOutput = []) => {
|
|
90
|
-
|
|
109
|
+
compile(tokens, blockOutput, blockData, {...vars, ...blockVars, parent: data, root: vars.root}, j, t);
|
|
91
110
|
return blockOutput.join("");
|
|
92
111
|
},
|
|
93
112
|
}));
|
|
94
|
-
// Make sure that this block is executed at least once
|
|
95
|
-
if (i + 1 === j) {
|
|
96
|
-
i = compile(tokens, [], {}, {}, j, t);
|
|
97
|
-
}
|
|
98
113
|
}
|
|
99
114
|
else if (tokens[i].startsWith("#") || tokens[i].startsWith("^")) {
|
|
100
115
|
const t = tokens[i].slice(1).trim();
|