@reldens/cms 0.35.0 → 0.36.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.
|
@@ -30,10 +30,23 @@ class AssetTransformer
|
|
|
30
30
|
|
|
31
31
|
buildAssetUrl(assetPath, currentRequest)
|
|
32
32
|
{
|
|
33
|
-
if
|
|
33
|
+
// if the path is a url, we will not transform it:
|
|
34
|
+
if(assetPath && assetPath.startsWith('http')){
|
|
34
35
|
return assetPath;
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
+
let assetUrl = sc.get(currentRequest, 'assetUrl', '');
|
|
38
|
+
let publicUrl = sc.get(currentRequest, 'publicUrl', '');
|
|
39
|
+
if(!assetPath){
|
|
40
|
+
if(assetUrl){
|
|
41
|
+
return assetUrl;
|
|
42
|
+
}
|
|
43
|
+
return '';
|
|
44
|
+
}
|
|
45
|
+
let normalizedPath = assetPath.startsWith('/') ? assetPath : '/'+assetPath;
|
|
46
|
+
if(assetUrl){
|
|
47
|
+
return assetUrl+normalizedPath;
|
|
48
|
+
}
|
|
49
|
+
return publicUrl+'/assets'+normalizedPath;
|
|
37
50
|
}
|
|
38
51
|
|
|
39
52
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Reldens - CMS - CdnTransformer
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { sc } = require('@reldens/utils');
|
|
8
|
+
|
|
9
|
+
class CdnTransformer
|
|
10
|
+
{
|
|
11
|
+
|
|
12
|
+
async transform(template, domain, req, systemVariables)
|
|
13
|
+
{
|
|
14
|
+
if(!template){
|
|
15
|
+
return template;
|
|
16
|
+
}
|
|
17
|
+
let currentRequest = sc.get(systemVariables, 'currentRequest', {});
|
|
18
|
+
let cdnPattern = /\[cdn\(([^)]+)\)\]/g;
|
|
19
|
+
let matches = [...template.matchAll(cdnPattern)];
|
|
20
|
+
for(let i = matches.length - 1; i >= 0; i--){
|
|
21
|
+
let match = matches[i];
|
|
22
|
+
let cdnPath = match[1].replace(/['"]/g, '');
|
|
23
|
+
let absoluteUrl = this.buildCdnUrl(cdnPath, currentRequest);
|
|
24
|
+
template = template.substring(0, match.index)+absoluteUrl+template.substring(match.index+match[0].length);
|
|
25
|
+
}
|
|
26
|
+
return template;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
buildCdnUrl(cdnPath, currentRequest)
|
|
30
|
+
{
|
|
31
|
+
// if the path is a url, we will not transform it:
|
|
32
|
+
if(cdnPath && cdnPath.startsWith('http')){
|
|
33
|
+
return cdnPath;
|
|
34
|
+
}
|
|
35
|
+
let assetUrl = sc.get(currentRequest, 'assetUrl', '');
|
|
36
|
+
let publicUrl = sc.get(currentRequest, 'publicUrl', '');
|
|
37
|
+
if(!cdnPath){
|
|
38
|
+
if(assetUrl){
|
|
39
|
+
return assetUrl;
|
|
40
|
+
}
|
|
41
|
+
return publicUrl;
|
|
42
|
+
}
|
|
43
|
+
let normalizedPath = cdnPath.startsWith('/') ? cdnPath : '/'+cdnPath;
|
|
44
|
+
if(assetUrl){
|
|
45
|
+
return assetUrl+normalizedPath;
|
|
46
|
+
}
|
|
47
|
+
return publicUrl+normalizedPath;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
module.exports.CdnTransformer = CdnTransformer;
|
package/lib/template-engine.js
CHANGED
|
@@ -12,6 +12,7 @@ const { PartialsTransformer } = require('./template-engine/partials-transformer'
|
|
|
12
12
|
const { FormsTransformer } = require('./template-engine/forms-transformer');
|
|
13
13
|
const { UrlTransformer } = require('./template-engine/url-transformer');
|
|
14
14
|
const { AssetTransformer } = require('./template-engine/asset-transformer');
|
|
15
|
+
const { CdnTransformer } = require('./template-engine/cdn-transformer');
|
|
15
16
|
const { DateTransformer } = require('./template-engine/date-transformer');
|
|
16
17
|
const { TranslateTransformer } = require('./template-engine/translate-transformer');
|
|
17
18
|
const { SystemVariablesProvider } = require('./template-engine/system-variables-provider');
|
|
@@ -84,6 +85,7 @@ class TemplateEngine
|
|
|
84
85
|
}
|
|
85
86
|
this.urlTransformer = new UrlTransformer();
|
|
86
87
|
this.assetTransformer = new AssetTransformer();
|
|
88
|
+
this.cdnTransformer = new CdnTransformer();
|
|
87
89
|
this.dateTransformer = new DateTransformer({
|
|
88
90
|
defaultFormat: sc.get(props, 'defaultDateFormat', 'Y-m-d H:i:s')
|
|
89
91
|
});
|
|
@@ -99,6 +101,7 @@ class TemplateEngine
|
|
|
99
101
|
this.partialsTransformer,
|
|
100
102
|
this.urlTransformer,
|
|
101
103
|
this.assetTransformer,
|
|
104
|
+
this.cdnTransformer,
|
|
102
105
|
this.dateTransformer,
|
|
103
106
|
this.translateTransformer
|
|
104
107
|
];
|