@itrocks/template 0.2.4 → 0.3.0
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/README.md +12 -0
- package/cjs/template.d.ts +3 -0
- package/cjs/template.js +41 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -110,6 +110,18 @@ You can write your pages in your primary language and apply translations automat
|
|
|
110
110
|
|
|
111
111
|
## Syntax and Detailed Features
|
|
112
112
|
|
|
113
|
+
### Public URLs
|
|
114
|
+
|
|
115
|
+
`template.publicUrl` is an optional `(path: string) => string` callback, defaulting to the identity function.
|
|
116
|
+
It converts rendered URL attribute values, including evaluated expressions, included templates, generated controls
|
|
117
|
+
and embedded asset dependencies. Script discovery retains application-relative paths.
|
|
118
|
+
`template.applicationPath` provides the inverse conversion for recognising already prefixed asset URLs.
|
|
119
|
+
|
|
120
|
+
`template.urlAttributes` lists the attributes using that callback: `action`, `formaction`, `href`, `location`, `poster`
|
|
121
|
+
and `src`. Integrations can append their own URL-valued data attributes. Both settings are inherited by includes.
|
|
122
|
+
|
|
123
|
+
The it.rocks framework binds these settings from the current request automatically.
|
|
124
|
+
|
|
113
125
|
### Delimiters Outside of HTML element tags
|
|
114
126
|
|
|
115
127
|
**Simple Expressions**: Use braces `{expression}`.
|
package/cjs/template.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export declare class HtmlResponse {
|
|
|
28
28
|
export declare class Template {
|
|
29
29
|
data?: any | undefined;
|
|
30
30
|
containerData?: any | undefined;
|
|
31
|
+
applicationPath: (path: string) => string;
|
|
31
32
|
blockBack: number;
|
|
32
33
|
blockStack: BlockStackEntry[];
|
|
33
34
|
doExpression: boolean;
|
|
@@ -64,6 +65,8 @@ export declare class Template {
|
|
|
64
65
|
onTagClose?: (name: string) => void;
|
|
65
66
|
parsers: VariableParser[];
|
|
66
67
|
prefixes: string;
|
|
68
|
+
publicUrl: (path: string) => string;
|
|
69
|
+
urlAttributes: string[];
|
|
67
70
|
constructor(data?: any | undefined, containerData?: any | undefined);
|
|
68
71
|
applyLiterals(text: string, parts?: string[]): string;
|
|
69
72
|
closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
|
package/cjs/template.js
CHANGED
|
@@ -31,10 +31,12 @@ exports.HtmlResponse = HtmlResponse;
|
|
|
31
31
|
class Template {
|
|
32
32
|
data;
|
|
33
33
|
containerData;
|
|
34
|
-
//
|
|
34
|
+
// Application path
|
|
35
|
+
applicationPath = (path) => path;
|
|
36
|
+
// Block stack
|
|
35
37
|
blockBack = 0;
|
|
36
38
|
blockStack = [];
|
|
37
|
-
//
|
|
39
|
+
// Parser
|
|
38
40
|
doExpression = true;
|
|
39
41
|
index = 0;
|
|
40
42
|
length = 0;
|
|
@@ -45,17 +47,17 @@ class Template {
|
|
|
45
47
|
target = '';
|
|
46
48
|
targetReplace = '';
|
|
47
49
|
targetStack = [];
|
|
48
|
-
//
|
|
50
|
+
// Literal
|
|
49
51
|
doLiteral = false;
|
|
50
52
|
inLiteral = false;
|
|
51
53
|
literalParts = [];
|
|
52
54
|
literalPartStack = [];
|
|
53
55
|
lockLiteral = false;
|
|
54
|
-
//
|
|
56
|
+
// HTML head
|
|
55
57
|
addLinks = new sorted_array_1.SortedArray();
|
|
56
58
|
doneLinks = new sorted_array_1.SortedArray();
|
|
57
59
|
headTitle;
|
|
58
|
-
//
|
|
60
|
+
// File
|
|
59
61
|
fileName;
|
|
60
62
|
filePath;
|
|
61
63
|
included = false;
|
|
@@ -75,6 +77,9 @@ class Template {
|
|
|
75
77
|
// Additional parsers
|
|
76
78
|
parsers = [];
|
|
77
79
|
prefixes = '';
|
|
80
|
+
// URL management
|
|
81
|
+
publicUrl = (path) => path;
|
|
82
|
+
urlAttributes = ['action', 'formaction', 'href', 'location', 'poster', 'src'];
|
|
78
83
|
constructor(data, containerData) {
|
|
79
84
|
this.data = data;
|
|
80
85
|
this.containerData = containerData;
|
|
@@ -132,7 +137,7 @@ class Template {
|
|
|
132
137
|
if (dependency[0] === '<') {
|
|
133
138
|
const script = dependency.match(/<script[^>]*\bsrc=["']([^"']+)["']/i)?.[1];
|
|
134
139
|
if (script) {
|
|
135
|
-
exports.frontScripts.push(script);
|
|
140
|
+
exports.frontScripts.push(this.applicationPath(script));
|
|
136
141
|
}
|
|
137
142
|
if (DEBUG)
|
|
138
143
|
console.log('addLink(', dependency, ')');
|
|
@@ -197,6 +202,7 @@ class Template {
|
|
|
197
202
|
async include(path, data) {
|
|
198
203
|
const template = new (Object.getPrototypeOf(this).constructor)(data, this.blockStack[0]?.data);
|
|
199
204
|
template.addLinks = this.addLinks;
|
|
205
|
+
template.applicationPath = this.applicationPath;
|
|
200
206
|
template.doExpression = this.doExpression;
|
|
201
207
|
template.doLiteral = this.doLiteral;
|
|
202
208
|
template.doneLinks = this.doneLinks;
|
|
@@ -206,6 +212,8 @@ class Template {
|
|
|
206
212
|
template.onTagOpen = this.onTagOpen;
|
|
207
213
|
template.onTagOpened = this.onTagOpened;
|
|
208
214
|
template.parsers = this.parsers;
|
|
215
|
+
template.publicUrl = this.publicUrl;
|
|
216
|
+
template.urlAttributes = this.urlAttributes;
|
|
209
217
|
const parsed = await template.parseFile(((path[0] === node_path_2.sep) || (path[1] === ':'))
|
|
210
218
|
? path
|
|
211
219
|
: (this.filePath + node_path_2.sep + path));
|
|
@@ -262,8 +270,9 @@ class Template {
|
|
|
262
270
|
this.start = index;
|
|
263
271
|
}
|
|
264
272
|
normalizeLink(link) {
|
|
265
|
-
if (link[0] === '/')
|
|
266
|
-
return link;
|
|
273
|
+
if ((link[0] === '/') || /^[a-z][a-z\d+.-]*:/i.test(link)) {
|
|
274
|
+
return this.applicationPath(link);
|
|
275
|
+
}
|
|
267
276
|
const result = (0, node_path_1.normalize)(this.filePath + node_path_2.sep + link).substring(app_dir_1.appDir.length);
|
|
268
277
|
return (((node_path_2.sep === '/') ? result : result.replaceAll(node_path_2.sep, '/')))
|
|
269
278
|
.replace(/^\/node_modules\/@itrocks\//, '/@itrocks/')
|
|
@@ -290,7 +299,8 @@ class Template {
|
|
|
290
299
|
}
|
|
291
300
|
const quote = addLink[start++];
|
|
292
301
|
const stop = addLink.indexOf(quote, start);
|
|
293
|
-
const link = addLink.substring(start, stop);
|
|
302
|
+
const link = this.publicUrl(addLink.substring(start, stop));
|
|
303
|
+
addLink = addLink.slice(0, start) + link + addLink.slice(stop);
|
|
294
304
|
if (DEBUG)
|
|
295
305
|
console.log('check(', link, ')');
|
|
296
306
|
if (!this.doneLinks.includes(link)) {
|
|
@@ -352,7 +362,19 @@ class Template {
|
|
|
352
362
|
}
|
|
353
363
|
const expression = this.target + this.source.substring(this.start, this.index - minus);
|
|
354
364
|
const lastTarget = this.targetStack.pop();
|
|
355
|
-
|
|
365
|
+
let parsed = await this.parsePath(expression, data);
|
|
366
|
+
const html = (parsed instanceof HtmlResponse) ? parsed.html : parsed;
|
|
367
|
+
if ((typeof html === 'string') && /<[a-z]/i.test(html) && (this.publicUrl('/') !== '/')) {
|
|
368
|
+
const template = new Template({});
|
|
369
|
+
template.applicationPath = this.applicationPath;
|
|
370
|
+
template.doExpression = false;
|
|
371
|
+
template.filePath = this.filePath;
|
|
372
|
+
template.publicUrl = this.publicUrl;
|
|
373
|
+
template.urlAttributes = this.urlAttributes;
|
|
374
|
+
const prefix = '<html><head></head><body>';
|
|
375
|
+
const suffix = '</body></html>';
|
|
376
|
+
parsed = (await template.parseBuffer(prefix + html + suffix)).slice(prefix.length, -suffix.length);
|
|
377
|
+
}
|
|
356
378
|
this.index += (char === close) ? 1 : finalClose.length;
|
|
357
379
|
this.start = this.index;
|
|
358
380
|
this.target = '';
|
|
@@ -739,9 +761,11 @@ class Template {
|
|
|
739
761
|
}
|
|
740
762
|
const inLinkHRef = inLink && (attributeChar === 'h') && (attributeName === 'href');
|
|
741
763
|
const inScriptSrc = inScript && (attributeChar === 's') && (attributeName === 'src');
|
|
742
|
-
|
|
764
|
+
const inUrl = this.urlAttributes.includes(attributeName);
|
|
765
|
+
if ((inUrl || this.inLiteral) && (this.index > this.start)) {
|
|
743
766
|
this.sourceToTarget();
|
|
744
767
|
}
|
|
768
|
+
const urlStart = this.target.length;
|
|
745
769
|
const valuePosition = this.index;
|
|
746
770
|
const shortQuote = !(quote.length - 1);
|
|
747
771
|
if (shortQuote && (attributeBlock !== undefined)) {
|
|
@@ -768,7 +792,7 @@ class Template {
|
|
|
768
792
|
if (!(inHead && this.included)) {
|
|
769
793
|
if (DEBUG)
|
|
770
794
|
console.log('doneLink(', frontStyle, ')');
|
|
771
|
-
this.doneLinks.push(frontStyle);
|
|
795
|
+
this.doneLinks.push(this.publicUrl(frontStyle));
|
|
772
796
|
}
|
|
773
797
|
}
|
|
774
798
|
if (inScriptSrc && attributeValue.endsWith('.js')) {
|
|
@@ -779,9 +803,13 @@ class Template {
|
|
|
779
803
|
if (!(inHead && this.included)) {
|
|
780
804
|
if (DEBUG)
|
|
781
805
|
console.log('doneLink(', frontScript, ')');
|
|
782
|
-
this.doneLinks.push(frontScript);
|
|
806
|
+
this.doneLinks.push(this.publicUrl(frontScript));
|
|
783
807
|
}
|
|
784
808
|
}
|
|
809
|
+
if (inUrl) {
|
|
810
|
+
this.sourceToTarget();
|
|
811
|
+
this.target = this.target.slice(0, urlStart) + this.publicUrl(this.target.slice(urlStart));
|
|
812
|
+
}
|
|
785
813
|
if (this.onAttribute)
|
|
786
814
|
this.onAttribute(attributeName, attributeValue);
|
|
787
815
|
if (char !== '>')
|
package/package.json
CHANGED