0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Refactored theme middleware module

no-issue

- Replaces var -> const
- Removed use of object to hold functions
This commit is contained in:
Fabien O'Carroll 2019-04-03 12:40:35 +02:00
parent d0c1853797
commit b38fb32c3f

View file

@ -1,17 +1,16 @@
var _ = require('lodash'), const _ = require('lodash');
hbs = require('./engine'), const hbs = require('./engine');
urlService = require('../url'), const urlService = require('../url');
config = require('../../config'), const config = require('../../config');
common = require('../../lib/common'), const common = require('../../lib/common');
settingsCache = require('../settings/cache'), const settingsCache = require('../settings/cache');
activeTheme = require('./active'), const activeTheme = require('./active');
themeMiddleware = {};
// ### Ensure Active Theme // ### Ensure Active Theme
// Ensure there's a properly set & mounted active theme before attempting to serve a blog request // Ensure there's a properly set & mounted active theme before attempting to serve a blog request
// If there is no active theme, throw an error // If there is no active theme, throw an error
// Else, ensure the active theme is mounted // Else, ensure the active theme is mounted
themeMiddleware.ensureActiveTheme = function ensureActiveTheme(req, res, next) { function ensureActiveTheme(req, res, next) {
// CASE: this means that the theme hasn't been loaded yet i.e. there is no active theme // CASE: this means that the theme hasn't been loaded yet i.e. there is no active theme
if (!activeTheme.get()) { if (!activeTheme.get()) {
// This is the one place we ACTUALLY throw an error for a missing theme as it's a request we cannot serve // This is the one place we ACTUALLY throw an error for a missing theme as it's a request we cannot serve
@ -38,17 +37,17 @@ themeMiddleware.ensureActiveTheme = function ensureActiveTheme(req, res, next) {
} }
next(); next();
}; }
// ### Update Template Data // ### Update Template Data
// Updates handlebars with the contextual data for the current request // Updates handlebars with the contextual data for the current request
themeMiddleware.updateTemplateData = function updateTemplateData(req, res, next) { function updateTemplateData(req, res, next) {
// Static information, same for every request unless the settings change // Static information, same for every request unless the settings change
// @TODO: bind this once and then update based on events? // @TODO: bind this once and then update based on events?
// @TODO: decouple theme layer from settings cache using the Content API // @TODO: decouple theme layer from settings cache using the Content API
var siteData = settingsCache.getPublic(), const siteData = settingsCache.getPublic();
labsData = _.cloneDeep(settingsCache.get('labs')), const labsData = _.cloneDeep(settingsCache.get('labs'));
themeData = {}; const themeData = {};
if (activeTheme.get()) { if (activeTheme.get()) {
themeData.posts_per_page = activeTheme.get().config('posts_per_page'); themeData.posts_per_page = activeTheme.get().config('posts_per_page');
@ -71,15 +70,14 @@ themeMiddleware.updateTemplateData = function updateTemplateData(req, res, next)
blog: siteData, blog: siteData,
site: siteData, site: siteData,
labs: labsData, labs: labsData,
config: themeData, config: themeData
member: req.member
} }
}); });
next(); next();
}; }
module.exports = [ module.exports = [
themeMiddleware.ensureActiveTheme, ensureActiveTheme,
themeMiddleware.updateTemplateData updateTemplateData
]; ];