-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
423 lines (370 loc) · 13.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
'use strict';
const HandlebarsV3 = require('handlebars');
const HandlebarsV4 = require('@bigcommerce/handlebars-v4');
const helpers = require('./helpers');
const AppError = require('./lib/appError');
const { CompileError, FormatError, RenderError, DecoratorError, TemplateNotFoundError, ValidationError, PrecompileError } = require('./lib/errors');
const handlebarsOptions = {
preventIndent: true
};
// HandlebarsRenderer implements the interface Paper requires for its
// rendering needs, and does so with Handlebars.
class HandlebarsRenderer {
// Add static accessor to reference custom errors
static get errors() {
return {
AppError,
CompileError,
FormatError,
RenderError,
DecoratorError,
TemplateNotFoundError,
ValidationError,
PrecompileError,
};
}
/**
* Constructor
*
* @param {Object} siteSettings - Global site settings, passed to helpers
* @param {Object} themeSettings - Theme settings (configuration), passed to helpers
* @param {String} hbVersion - Which version of handlebars to use. One of ['v3', 'v4'] - defaults to 'v3'.
* @param {Object} logger - A console-like object to use for logging
* @param {String} logLevel - log level which will be overriden by renderer
*/
constructor(siteSettings, themeSettings, hbVersion, logger = console, logLevel = 'info', params = {}) {
// Figure out which version of Handlebars to use.
switch(hbVersion) {
case 'v4':
this.handlebars = HandlebarsV4.create();
break;
case 'v3':
default:
this.handlebars = HandlebarsV3.create();
break;
}
this.logger = logger;
this._setHandlebarsLogger();
this._overrideConsoleLog();
this.setSiteSettings(siteSettings || {});
this.setThemeSettings(themeSettings || {});
this.setTranslator(null);
this.setContent({});
this.resetDecorators();
this.setLoggerLevel(logLevel);
this.setRequestParams(params);
// Build global context for helpers
this.helperContext = {
handlebars: this.handlebars,
getSiteSettings: this.getSiteSettings.bind(this),
getThemeSettings: this.getThemeSettings.bind(this),
getRequestParams: this.getRequestParams.bind(this),
getTranslator: this.getTranslator.bind(this),
getContent: this.getContent.bind(this),
getLogger: this.getLogger.bind(this),
storage: {}, // global storage used by helpers to keep state
resourceHints: []
};
// Register helpers with Handlebars
for (let i = 0; i < helpers.length; i++) {
const spec = helpers[i];
this.handlebars.registerHelper(spec.name, spec.factory(this.helperContext));
}
}
getResourceHints() {
return this.helperContext.resourceHints;
}
/**
* Set the paper.Translator instance used to translate strings in helpers.
*
* @param {Translator} translator A paper.Translator instance used to translate strings in helpers
*/
setTranslator(translator) {
this._translator = translator;
};
/**
* Get the paper.Translator instance used to translate strings in helpers.
*
* @return {Translator} A paper.Translator instance used to translate strings in helpers
*/
getTranslator() {
return this._translator;
};
/**
* Set the siteSettings object containing global site settings.
*
* @param {object} settings An object containing global site settings.
*/
setSiteSettings(settings) {
this._siteSettings = settings;
};
/**
* Get the siteSettings object containing global site settings.
*
* @return {object} settings An object containing global site settings.
*/
getSiteSettings() {
return this._siteSettings;
};
/**
* Set the themeSettings object containing the theme configuration.
*
* @param {object} settings An object containing the theme configuration.
*/
setThemeSettings(settings) {
this._themeSettings = settings;
};
/**
* Get the themeSettings object containing the theme configuration.
*
* @return {object} settings An object containing the theme configuration.
*/
getThemeSettings() {
return this._themeSettings;
};
/**
* Set the request params object containing the request parameters.
*
* @param {object} params
*/
setRequestParams(params) {
this._params = params;
}
/**
* Get the request params object containing the request parameters.
*
* @returns {object} params
*/
getRequestParams() {
return this._params;
}
/**
* Reset decorator list.
*/
resetDecorators() {
this._decorators = [];
};
/**
* Add a decorator to be applied at render time.
*
* @param {Function} decorator
*/
addDecorator(decorator) {
this._decorators.push(decorator);
};
/**
* Setup content regions to be used by the `region` helper.
*
* @param {Object} Regions with widgets
*/
setContent(regions) {
this._contentRegions = regions;
};
/**
* Get content regions.
*
* @param {Object} Regions with widgets
*/
getContent() {
return this._contentRegions;
};
/**
* Get logger provided to the library
*
* @param {Object} logger
*/
getLogger() {
return this.logger;
}
/**
* Add templates to the active set of partials. The templates can either be raw
* template strings, or the result coming from the preProcessor function.
*
* @param {Object} A set of templates to register with handlebars
*/
addTemplates(templates) {
const paths = Object.keys(templates);
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (typeof this.handlebars.partials[path] !== 'undefined') {
continue;
}
try {
// Check if it is a precompiled template
const template = this._tryRestoringPrecompiled(templates[path]);
// Register it with handlebars
this.handlebars.registerPartial(path, template);
} catch(e) {
throw new FormatError(e.message);
}
}
};
_tryRestoringPrecompiled(precompiled) {
// Let's analyze the string to make sure it at least looks
// something like a handlebars precompiled template. It should
// be a string representation of an object containing a `main`
// function and a `compiler` array. We do this because the next
// step is a potentially dangerous eval.
const re = /"compiler":\[.*\],"main":function/;
if (!re.test(precompiled)) {
// This is not a valid precompiled template, so this is
// a raw template that can be registered directly.
return precompiled;
}
// We need to take the string representation and turn it into a
// valid JavaScript object. eval is evil, but necessary in this case.
let template;
eval(`template = ${precompiled}`);
// Take the precompiled object and get the actual function out of it,
// after first testing for runtime version compatibility.
return this.handlebars.template(template);
}
/**
* Detect whether a given template has been loaded.
*/
isTemplateLoaded(path) {
return typeof this.handlebars.partials[path] !== 'undefined';
}
/**
* Return a function that performs any preprocessing we want to do on the templates.
* In our case, run them through the Handlebars precompiler. This returns a string
* representation of an object understood by Handlebars to be a precompiled template.
*/
getPreProcessor() {
return templates => {
const paths = Object.keys(templates);
const processed = {};
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
try {
processed[path] = this.handlebars.precompile(templates[path], handlebarsOptions);
} catch(e) {
throw new CompileError(e.message, { path });
}
}
return processed;
};
}
/**
* Render a template with the given context
*
* @param {String} path The path to the template
* @param {Object} context The context to provide to the template
* @return {Promise} A promise to return the rendered template
* @throws [TemplateNotFoundError|RenderError|DecoratorError]
*/
render(path, context) {
return new Promise((resolve, reject) => {
context = context || {};
// Add some data to the context
context.template = path;
if (this._translator) {
context.locale_name = this._translator.getLocale();
}
delete this.handlebars.compile;
// Look up the template
const template = this.handlebars.partials[path];
if (typeof template === 'undefined') {
return reject(new TemplateNotFoundError(`template not found: ${path}`));
}
// Render the template
let result;
try {
result = template(context);
} catch(e) {
return reject(new RenderError(`${e.message} : ${e.stack}`));
}
// Apply decorators
try {
for (let i = 0; i < this._decorators.length; i++) {
result = this._decorators[i](result);
}
} catch(e) {
return reject(new DecoratorError(e.message));
}
resolve(result);
});
};
/**
* Renders a string with the given context
*
* @param {String} template
* @param {Object} context
* @return {Promise}
* @throws [CompileError|RenderError]
*/
renderString(template, context) {
return new Promise((resolve, reject) => {
let precompiled, precompiledTemplate;
context = context || {};
if (typeof template !== 'string') {
return reject(new CompileError('Template must be a string'));
}
try {
delete this.handlebars.compile;
precompiled = this.handlebars.precompile(template, handlebarsOptions);
} catch (e) {
return reject(new PrecompileError(e.message));
}
try {
eval(`precompiledTemplate = ${precompiled}`);
template = this.handlebars.template(precompiledTemplate);
} catch(e) {
return reject(new CompileError(e.message));
}
// Render the result
let result;
try {
result = template(context);
} catch(e) {
return reject(new RenderError(e.message));
}
resolve(result);
});
}
/**
* Internal method. Set the Handlebars logger to use the given console alternative. This is an override
* of https://github.com/wycats/handlebars.js/blob/148b19182d70278237a62d8293db540483a0c46c/lib/handlebars/logger.js#L22
*/
_setHandlebarsLogger() {
// Normalize on the v4 implementation
this.handlebars.logger = HandlebarsV4.logger;
// Override logger.log to use the given console alternative
this.handlebars.log = this.handlebars.logger.log = (level, ...message) => {
level = this.handlebars.logger.lookupLevel(level);
if (this.handlebars.logger.lookupLevel(this.handlebars.logger.level) <= level) {
let method = this.handlebars.logger.methodMap[level];
if (typeof this.logger[method] !== 'function') {
method = 'log';
} else if (this._overrideHandlebarsAccessDeniedToPropertyMessageLevel(...message)) {
method = 'info';
}
this.logger[method](...message);
}
};
}
/**
* As some handlebars helpers do not use the logger, we need to override the console.log method
*/
_overrideConsoleLog() {
this.isLoggerOverriden = false;
if (this.logger !== console) {
console.log = this.logger.info.bind(this.logger);
console.info = this.logger.info.bind(this.logger);
console.error = this.logger.error.bind(this.logger);
console.warn = this.logger.warn.bind(this.logger);
this.isLoggerOverriden = true;
}
}
_overrideHandlebarsAccessDeniedToPropertyMessageLevel(message) {
return this.isLoggerOverriden && message.includes('Handlebars: Access has been denied to resolve the property');
}
/**
*
* @param {String} level
*/
setLoggerLevel(level) {
this.handlebars.logger.level = level;
}
}
module.exports = HandlebarsRenderer;