htmv 0.0.30 → 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 CHANGED
@@ -10,6 +10,14 @@ 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
  })
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "htmv",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.30",
5
+ "version": "0.0.31",
6
6
  "devDependencies": {
7
7
  "@biomejs/biome": "2.3.3",
8
8
  "@types/bun": "latest"
package/src/views.ts CHANGED
@@ -15,6 +15,25 @@ 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
  })