Ghost/core/frontend/services/helpers/registry.js
Hannah Wolfe 9d7049cd3f
Refactored helper registration code into a service
- The helper registration code is "framework" code and very specific
- At the moment the "theme engine" is full of lots of disparate theme related stuff
- I'm trying to make the frontend framework code clearer and also expand it to  make it more useful
- The helper system now also exposes 3 methods allowing you to register a directory, a helper or an alias
- I've updated the codebase to use these both for our core helpers and for "apps"
2021-10-05 10:04:02 +01:00

46 lines
978 B
JavaScript

const glob = require('glob');
const path = require('path');
const handlebars = require('./handlebars');
// Internal Cache
const registry = {};
const registerHelper = (name, helperFn) => {
if (registry[name]) {
return;
}
registry[name] = helperFn;
if (helperFn.async) {
handlebars.registerAsyncThemeHelper(name, helperFn);
} else {
handlebars.registerThemeHelper(name, helperFn);
}
};
const registerDir = (helperPath) => {
let helperFiles = glob.sync('!(index).js', {cwd: helperPath});
helperFiles.forEach((helper) => {
const name = helper.replace(/.js$/, '');
const fn = require(path.join(helperPath, helper));
registerHelper(name, fn);
if (fn.alias) {
registerHelper(fn.alias, fn);
}
});
};
const registerAlias = (alias, name) => {
registerHelper(alias, registry[name]);
};
module.exports = {
registerAlias,
registerHelper,
registerDir
};