forked from bigcommerce/paper-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
358 lines (314 loc) · 11.3 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
'use strict';
const HandlebarsV3 = require('handlebars');
const HandlebarsV4 = require('@bigcommerce/handlebars-v4');
const helpers = require('./helpers');
const AppError = require('./lib/appError');
class CompileError extends AppError {}; // Error compiling template
class FormatError extends AppError {}; // Error restoring precompiled template
class RenderError extends AppError {}; // Error rendering template
class DecoratorError extends AppError {}; // Error applying decorator
class TemplateNotFoundError extends AppError {}; // Template not registered
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 {
CompileError,
FormatError,
RenderError,
DecoratorError,
TemplateNotFoundError,
};
}
/**
* 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') {
// 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.setSiteSettings(siteSettings || {});
this.setThemeSettings(themeSettings || {});
this.setTranslator(null);
this.setContent({});
this.resetDecorators();
this.setLoggerLevel(logLevel);
// Build global context for helpers
this.helperContext = {
handlebars: this.handlebars,
getSiteSettings: this.getSiteSettings.bind(this),
getThemeSettings: this.getThemeSettings.bind(this),
getTranslator: this.getTranslator.bind(this),
getContent: this.getContent.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;
};
/**
* 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;
};
/**
* 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();
}
// 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) => {
context = context || {};
// Compile the template
try {
template = this.handlebars.compile(template);
} 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';
}
this.logger[method](...message);
}
};
}
/**
*
* @param {String} level
*/
setLoggerLevel(level) {
this.handlebars.logger.level = level;
}
}
module.exports = HandlebarsRenderer;