Compare commits

...

3 Commits

Author SHA1 Message Date
1b05af09fb Fixed templates
All checks were successful
Build / Publish Docs (push) Successful in 58s
Build / Build NPM Project (push) Successful in 1m15s
Build / Tag Version (push) Successful in 11s
2025-12-06 21:51:47 -05:00
32c61fff42 Fixed templates
All checks were successful
Build / Publish Docs (push) Successful in 1m14s
Build / Build NPM Project (push) Successful in 1m38s
Build / Tag Version (push) Successful in 11s
2025-12-06 21:34:21 -05:00
df517b6078 Fixed templates bleeding into each other between runs
All checks were successful
Build / Publish Docs (push) Successful in 43s
Build / Build NPM Project (push) Successful in 1m49s
Build / Tag Version (push) Successful in 8s
2025-12-06 19:45:38 -05:00
2 changed files with 7 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.27.16", "version": "0.27.19",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@@ -24,7 +24,7 @@ export async function renderTemplate(template: string, data: any, fetch?: (file:
const evaluate = (code: string, data: object, fatal = true) => { const evaluate = (code: string, data: object, fatal = true) => {
try { try {
return Function('data', `Object.assign(this, data); return ${code};`)(data); return Function('data', `with(data) { return ${code}; }`)(data);
} catch { } catch {
if(fatal) throw new TemplateError(`Failed to evaluate: ${code}`); if(fatal) throw new TemplateError(`Failed to evaluate: ${code}`);
else return false; else return false;
@@ -36,12 +36,12 @@ export async function renderTemplate(template: string, data: any, fetch?: (file:
const nested = matchAll(found[0], /\{\{\s*?\?.+?}}/g).slice(-1)?.[0]?.index; const nested = matchAll(found[0], /\{\{\s*?\?.+?}}/g).slice(-1)?.[0]?.index;
if(nested != 0) if(nested != 0)
found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)(?:\{\{\s*?!\?\s*?}}([\s\S]*?))?\{\{\s*?\/\?\s*?}}/g.exec(content.slice(found.index + nested)) found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)(?:\{\{\s*?!\?\s*?}}([\s\S]*?))?\{\{\s*?\/\?\s*?}}/g.exec(content.slice(found.index + nested))
content = content.replaceAll(found[0], (evaluate(found[1], d, false) ? found[2] : found[3]) || ''); content = content.replace(found[0], (evaluate(found[1], d, false) ? found[2] : found[3]) || '');
} }
// Imports - We render bottom up: `{{ < file.html }}` // Imports - We render bottom up: `{{ < file.html }}`
while(!!(found = /\{\{\s*?<\s*?(.+?)\s*?}}/g.exec(content))) { while(!!(found = /\{\{\s*?<\s*?(.+?)\s*?}}/g.exec(content))) {
content = content.replaceAll(found[0], await renderTemplate(await fetch(found[1].trim()), data, fetch)); content = content.replace(found[0], await renderTemplate(await fetch(found[1].trim()), data, fetch));
} }
// For Loops: `{{ * (row, index) in invoice }} CONTENT {{ /* }}` // For Loops: `{{ * (row, index) in invoice }} CONTENT {{ /* }}`
@@ -61,17 +61,17 @@ export async function renderTemplate(template: string, data: any, fetch?: (file:
[index]: i [index]: i
}, fetch)) }, fetch))
} }
content = content.replaceAll(found[0], compiled.join('\n')); content = content.replace(found[0], compiled.join('\n'));
} }
// Evaluate whatever is left - Should come last: `{{ javascript }}` // Evaluate whatever is left - Should come last: `{{ javascript }}`
while(!!(found = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g.exec(content))) { while(!!(found = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g.exec(content))) {
content = content.replaceAll(found[0], evaluate(found[1].trim(), d) || ''); content = content.replace(found[0], evaluate(found[1].trim(), d) || '');
} }
// Extends: `{{ > file.html:property }} CONTENT {{ /> }}` // Extends: `{{ > file.html:property }} CONTENT {{ /> }}`
while(!!(found = /\{\{\s*?>\s*?(.+?):(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/>\s*?}}/g.exec(content))) { while(!!(found = /\{\{\s*?>\s*?(.+?):(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/>\s*?}}/g.exec(content))) {
content = content.replace(found[0], await renderTemplate(await fetch(found[1].trim), { content = content.replace(found[0], await renderTemplate(await fetch(found[1].trim()), {
...data, ...data,
[found[2].trim()]: found[3], [found[2].trim()]: found[3],
}, fetch)); }, fetch));