mikel 0.9.0 → 0.9.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.js +64 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const tags = /\{\{|\}\}/;
|
|
2
|
+
const endl = "\n";
|
|
2
3
|
const escapedChars = {
|
|
3
4
|
"&": "&",
|
|
4
5
|
"<": "<",
|
|
@@ -18,6 +19,67 @@ const parse = (v, context = {}, vars = {}) => {
|
|
|
18
19
|
return (v || "").startsWith("@") ? get(vars, v.slice(1)) : get(context, v || ".");
|
|
19
20
|
};
|
|
20
21
|
|
|
22
|
+
// @description tiny yaml parser
|
|
23
|
+
const yamlParser = (str = "") => {
|
|
24
|
+
const lines = str.split(endl), result = {}, levels = [{ctx: result, indent: 0}];
|
|
25
|
+
let level = 0, i = 0;
|
|
26
|
+
while (i < lines.length) {
|
|
27
|
+
const line = lines[i] || "";
|
|
28
|
+
if (!!line.trim() && !line.trim().startsWith("#")) {
|
|
29
|
+
const indent = (line.match(/^( *)/m)?.[0] || "").length;
|
|
30
|
+
while(level > 0 && indent < levels[level].indent) {
|
|
31
|
+
levels.pop();
|
|
32
|
+
level = level - 1;
|
|
33
|
+
}
|
|
34
|
+
const isArrayItem = line.trim().startsWith("-");
|
|
35
|
+
let [key, value] = line.trim().split(":").map(v => v.trim());
|
|
36
|
+
// Check if is a new item of the array
|
|
37
|
+
if (isArrayItem) {
|
|
38
|
+
key = key.replace(/^(- *)/m, "");
|
|
39
|
+
if (typeof value === "undefined") {
|
|
40
|
+
value = key;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const newIndent = (line.slice(0, line.indexOf(key))).length;
|
|
44
|
+
levels[level].ctx.push({});
|
|
45
|
+
levels.push({ctx: levels[level].ctx[levels[level].ctx.length - 1], indent: newIndent});
|
|
46
|
+
level = level + 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Check for empty value --> entering into a nested object or array
|
|
50
|
+
if (!value) {
|
|
51
|
+
const nextLine = lines[i + 1] || "";
|
|
52
|
+
const nextIndent = (nextLine.match(/^( *)/m)?.[0] || "").length;
|
|
53
|
+
levels[level].ctx[key] = nextLine.trim().startsWith("-") ? [] : {};
|
|
54
|
+
if (nextIndent > levels[level].indent) {
|
|
55
|
+
levels.push({ctx: levels[level].ctx[key], indent: nextIndent});
|
|
56
|
+
level = level + 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else if(value && Array.isArray(levels[level].ctx)) {
|
|
60
|
+
levels[level].ctx.push(JSON.parse(value));
|
|
61
|
+
}
|
|
62
|
+
else if (value) {
|
|
63
|
+
levels[level].ctx[key] = JSON.parse(value);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
i = i + 1;
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// @description tiny front-matter parser
|
|
72
|
+
const frontmatter = (str = "", parser = null) => {
|
|
73
|
+
let body = (str || "").trim(), data = {};
|
|
74
|
+
const matches = Array.from(body.matchAll(/^(--- *)/gm))
|
|
75
|
+
if (matches?.length === 2 && matches[0].index === 0) {
|
|
76
|
+
const front = body.substring(0 + matches[0][1].length, matches[1].index).trim();
|
|
77
|
+
body = body.substring(matches[1].index + matches[1][1].length).trim();
|
|
78
|
+
data = typeof parser === "function" ? parser(front) : yamlParser(front);
|
|
79
|
+
}
|
|
80
|
+
return {body, data};
|
|
81
|
+
};
|
|
82
|
+
|
|
21
83
|
const defaultHelpers = {
|
|
22
84
|
"each": (value, opt) => {
|
|
23
85
|
return (typeof value === "object" ? Object.entries(value || {}) : [])
|
|
@@ -111,5 +173,7 @@ const mikel = (str, context = {}, opt = {}, output = []) => {
|
|
|
111
173
|
mikel.escape = escape;
|
|
112
174
|
mikel.get = get;
|
|
113
175
|
mikel.parse = parse;
|
|
176
|
+
mikel.yaml = yamlParser;
|
|
177
|
+
mikel.frontmatter = frontmatter;
|
|
114
178
|
|
|
115
179
|
export default mikel;
|