2020-05-26 19:10:29 +01:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-06-15 15:36:27 +01:00
|
|
|
const logging = require('@tryghost/logging');
|
2017-04-04 17:07:35 +01:00
|
|
|
|
2022-04-05 17:38:46 +01:00
|
|
|
const {hbs} = require('../handlebars');
|
2021-10-04 16:50:07 +01:00
|
|
|
|
2017-04-04 17:07:35 +01:00
|
|
|
// Register an async handlebars helper for a given handlebars instance
|
2020-10-20 12:02:56 +13:00
|
|
|
function asyncHelperWrapper(hbsInstance, name, fn) {
|
2022-05-11 09:51:38 +01:00
|
|
|
hbsInstance.registerAsyncHelper(name, async function returnAsync(context, options, cb) {
|
2017-04-04 17:07:35 +01:00
|
|
|
// Handle the case where we only get context and cb
|
|
|
|
if (!cb) {
|
|
|
|
cb = options;
|
|
|
|
options = undefined;
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:51:38 +01:00
|
|
|
try {
|
|
|
|
const response = await fn.call(this, context, options);
|
|
|
|
cb(response);
|
|
|
|
} catch (error) {
|
|
|
|
const wrappedErr = errors.utils.isGhostError(error) ? error : new errors.IncorrectUsageError({
|
|
|
|
err: error,
|
2020-04-29 16:44:27 +01:00
|
|
|
context: 'registerAsyncThemeHelper: ' + name,
|
|
|
|
errorDetails: {
|
2022-05-11 09:51:38 +01:00
|
|
|
originalError: error
|
2020-04-29 16:44:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-11 09:51:38 +01:00
|
|
|
const response = process.env.NODE_ENV === 'development' ? wrappedErr : '';
|
2017-08-27 17:06:44 +02:00
|
|
|
|
2020-03-30 21:23:02 +01:00
|
|
|
logging.error(wrappedErr);
|
2017-08-27 17:06:44 +02:00
|
|
|
|
2022-05-11 09:51:38 +01:00
|
|
|
cb(new hbsInstance.SafeString(response));
|
|
|
|
}
|
2017-04-04 17:07:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a handlebars helper for themes
|
|
|
|
module.exports.registerThemeHelper = function registerThemeHelper(name, fn) {
|
|
|
|
hbs.registerHelper(name, fn);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register an async handlebars helper for themes
|
|
|
|
module.exports.registerAsyncThemeHelper = function registerAsyncThemeHelper(name, fn) {
|
|
|
|
asyncHelperWrapper(hbs, name, fn);
|
|
|
|
};
|