mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
17499dbc7b
refs #5091 - makes post context explicit - data.post must be set, rather than post being the default - uses channelConfig to determine the context for a channel (the channel name) rather than basing it off of the URL - updates tests to setup the contexts more clearly, the outcome has not changed Since #6469 req has channelConfig attached to it. We can use req.channelConfig to determine what the context should be for a channel (the channel name) This allows us to remove the hardcoded URLs, and means that custom channels will automatically get their own context. Coupled with removing 'post' from being a default/fallthrough, to being explicitly set, this will reduce potential context errors, as we start to extend the frontend capabilities
59 lines
2.1 KiB
JavaScript
59 lines
2.1 KiB
JavaScript
/**
|
|
* # Response context
|
|
*
|
|
* Figures out which context we are currently serving. The biggest challenge with determining this
|
|
* is that the only way to determine whether or not we are a post, or a page, is with data after all the
|
|
* data for the template has been retrieved.
|
|
*
|
|
* Contexts are determined based on 3 pieces of information
|
|
* 1. res.locals.relativeUrl - which never includes the subdirectory
|
|
* 2. req.params.page - always has the page parameter, regardless of if the URL contains a keyword (RSS pages don't)
|
|
* 3. data - used for telling the difference between posts and pages
|
|
*/
|
|
|
|
var config = require('../../config'),
|
|
|
|
// Context patterns, should eventually come from Channel configuration
|
|
privatePattern = new RegExp('^\\/' + config.routeKeywords.private + '\\/'),
|
|
rssPattern = new RegExp('^\\/rss\\/'),
|
|
homePattern = new RegExp('^\\/$');
|
|
|
|
function setResponseContext(req, res, data) {
|
|
var pageParam = req.params && req.params.page !== undefined ? parseInt(req.params.page, 10) : 1;
|
|
|
|
res.locals = res.locals || {};
|
|
res.locals.context = [];
|
|
|
|
// If we don't have a relativeUrl, we can't detect the context, so return
|
|
if (!res.locals.relativeUrl) {
|
|
return;
|
|
}
|
|
|
|
// Paged context - special rule
|
|
if (!isNaN(pageParam) && pageParam > 1) {
|
|
res.locals.context.push('paged');
|
|
}
|
|
|
|
// Home context - special rule
|
|
if (homePattern.test(res.locals.relativeUrl)) {
|
|
res.locals.context.push('home');
|
|
}
|
|
|
|
// This is not currently used, as setRequestContext is not called for RSS feeds
|
|
if (rssPattern.test(res.locals.relativeUrl)) {
|
|
res.locals.context.push('rss');
|
|
}
|
|
|
|
// Each page can only have at most one of these
|
|
if (req.channelConfig) {
|
|
res.locals.context.push(req.channelConfig.name);
|
|
} else if (privatePattern.test(res.locals.relativeUrl)) {
|
|
res.locals.context.push('private');
|
|
} else if (data && data.post && data.post.page) {
|
|
res.locals.context.push('page');
|
|
} else if (data && data.post) {
|
|
res.locals.context.push('post');
|
|
}
|
|
}
|
|
|
|
module.exports = setResponseContext;
|