htmv 0.0.29 → 0.0.31
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/dist/views.js +9 -1
- package/package.json +1 -1
- package/src/views.ts +20 -1
package/dist/views.js
CHANGED
|
@@ -10,10 +10,18 @@ export async function view(view, props) {
|
|
|
10
10
|
const filePath = path.join(viewsPath, `${view}.html`);
|
|
11
11
|
const code = await fs.readFile(filePath, "utf-8");
|
|
12
12
|
const replacedCode = code
|
|
13
|
+
.replace(/<For\s+(\w+)\s+in\s+(\w+)>([\s\S]*?)<\/For>/g, (_, itemName, listName, innerContent) => {
|
|
14
|
+
const list = props[listName];
|
|
15
|
+
if (!Array.isArray(list))
|
|
16
|
+
throw new Error(`${listName} on view ${view} is not an array. If you wish for the For to not do anything pass an empty array instead.`);
|
|
17
|
+
return list
|
|
18
|
+
.map((item) => innerContent.replace(new RegExp(`{${itemName}}`, "g"), String(item)))
|
|
19
|
+
.join("");
|
|
20
|
+
})
|
|
13
21
|
.replace(/{(\w+)}/g, (_, propName) => {
|
|
14
22
|
return props[propName];
|
|
15
23
|
})
|
|
16
|
-
.replace(/<Isset\s+(
|
|
24
|
+
.replace(/<Isset\s+(!?\w+)>([\s\S]*?)<\/Isset>/g, (_, propNameWithPrefix, innerContent) => {
|
|
17
25
|
const isNegated = propNameWithPrefix.startsWith("!");
|
|
18
26
|
const propName = isNegated
|
|
19
27
|
? propNameWithPrefix.slice(1)
|
package/package.json
CHANGED
package/src/views.ts
CHANGED
|
@@ -15,11 +15,30 @@ export async function view(view: string, props: Record<string, unknown>) {
|
|
|
15
15
|
const filePath = path.join(viewsPath, `${view}.html`);
|
|
16
16
|
const code = await fs.readFile(filePath, "utf-8");
|
|
17
17
|
const replacedCode = code
|
|
18
|
+
.replace(
|
|
19
|
+
/<For\s+(\w+)\s+in\s+(\w+)>([\s\S]*?)<\/For>/g,
|
|
20
|
+
(_, itemName: string, listName: string, innerContent: string) => {
|
|
21
|
+
const list = props[listName];
|
|
22
|
+
if (!Array.isArray(list))
|
|
23
|
+
throw new Error(
|
|
24
|
+
`${listName} on view ${view} is not an array. If you wish for the For to not do anything pass an empty array instead.`,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
return list
|
|
28
|
+
.map((item) =>
|
|
29
|
+
innerContent.replace(
|
|
30
|
+
new RegExp(`{${itemName}}`, "g"),
|
|
31
|
+
String(item),
|
|
32
|
+
),
|
|
33
|
+
)
|
|
34
|
+
.join("");
|
|
35
|
+
},
|
|
36
|
+
)
|
|
18
37
|
.replace(/{(\w+)}/g, (_, propName) => {
|
|
19
38
|
return props[propName] as string;
|
|
20
39
|
})
|
|
21
40
|
.replace(
|
|
22
|
-
/<Isset\s+(
|
|
41
|
+
/<Isset\s+(!?\w+)>([\s\S]*?)<\/Isset>/g,
|
|
23
42
|
(_, propNameWithPrefix: string, innerContent: string) => {
|
|
24
43
|
const isNegated = propNameWithPrefix.startsWith("!");
|
|
25
44
|
const propName = isNegated
|