@wxn0brp/falcon-frame 0.9.6 → 0.9.7

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/render.d.ts CHANGED
@@ -10,3 +10,5 @@ export interface RenderHTMLOptions {
10
10
  _renderedPaths?: string[];
11
11
  }
12
12
  export declare function renderHTML(options: RenderHTMLOptions): string;
13
+ export declare function interpolate(template: string, data: Record<string, any>): string;
14
+ export declare function expandLoops(template: string, data: Record<string, any>): string;
package/dist/render.js CHANGED
@@ -28,10 +28,21 @@ export function renderHTML(options) {
28
28
  ...templateData,
29
29
  ...data,
30
30
  };
31
+ // Preliminary loop rendering
32
+ template = expandLoops(template, data);
31
33
  // Inserting data, e.g. {{name}}
32
- template = template.replace(/{{(.*?)}}/g, (_, key) => data[key.trim()] || "");
34
+ template = interpolate(template, data);
33
35
  // Loading partials, e.g. <!-- include header -->
34
- template = template.replace(/<!--\s*include\s*(.*?)\s*-->/g, (_, partialName) => {
36
+ template = template.replace(/<!--\s*include\s+([^\s{]+)(?:\s+(\{[\s\S]*?\}))?\s*-->/g, (_, partialName, json) => {
37
+ let includeData = {};
38
+ if (json) {
39
+ try {
40
+ includeData = JSON.parse(json);
41
+ }
42
+ catch (error) {
43
+ return "<!-- Invalid include data -->";
44
+ }
45
+ }
35
46
  const partialPath = path.join(path.dirname(templatePath), partialName + ".html");
36
47
  return renderHTML({
37
48
  templatePath: partialPath,
@@ -40,7 +51,10 @@ export function renderHTML(options) {
40
51
  realPath,
41
52
  ],
42
53
  noLayout: true,
43
- data,
54
+ data: {
55
+ ...data,
56
+ ...includeData,
57
+ },
44
58
  FF,
45
59
  FFVar,
46
60
  });
@@ -87,3 +101,36 @@ export function renderHTML(options) {
87
101
  return `<-- Template ${options.templatePath} has errors -->`;
88
102
  }
89
103
  }
104
+ function resolvePath(data, key) {
105
+ let value = data;
106
+ for (const part of key.split(".")) {
107
+ if (value == null)
108
+ return undefined;
109
+ value = value[part];
110
+ }
111
+ return value;
112
+ }
113
+ export function interpolate(template, data) {
114
+ return template.replace(/{{(.*?)}}/g, (_, key) => resolvePath(data, key.trim()) ?? "");
115
+ }
116
+ export function expandLoops(template, data) {
117
+ return template
118
+ .replace(/<!--\s*each\s+(\w+)\s+as\s+(\w+)\s*-->([\s\S]*?)<!--\s*\/each\s*-->/g, (_, key, itemVar, body) => (resolvePath(data, key) ?? [])
119
+ .map((value, index) => interpolate(body, {
120
+ ...data,
121
+ [itemVar]: value,
122
+ i: index,
123
+ }))
124
+ .join(""))
125
+ .replace(/<!--\s*range\s+(\w+)\s*-->([\s\S]*?)<!--\s*\/range\s*-->/g, (_, key, body) => {
126
+ const range = Array.from({
127
+ length: +key,
128
+ }, (_, i) => i);
129
+ return range
130
+ .map((i) => interpolate(body, {
131
+ ...data,
132
+ i,
133
+ }))
134
+ .join("");
135
+ });
136
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",