mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
7dc38e2078
no issue - Removed v1 'author' leftover in include statement for preview controller - Removed v1 'author' leftover in include statement for preview controller - Removed v1 'author' leftover in include statement in entry lookup routing helper - Migrated related test to use v2 API controller - Removed v0.1 routing confif - Removed v0.1 url config - Fixed tests that had to do with url's in resources after removing v0.1 resources from URL cache - Removed v1 'author' leftover in include statement in static routing helper - Modified the test to use v2 API - Removed v1 specific condition with 'page' in context helper - Fixed dynamic routing spec after theme switch to v2. All tested users have to have at least one published post to be shown as an author - Fixed URL Service spec to use theme engine v2
78 lines
2.8 KiB
JavaScript
78 lines
2.8 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
|
|
* 3. data - used for telling the difference between posts and pages
|
|
*/
|
|
const labs = require('../../../../server/services/labs'),
|
|
// @TODO: fix this!! These regexes are app specific and should be dynamic. They should not belong here....
|
|
// routeKeywords.private: 'private'
|
|
privatePattern = new RegExp('^\\/private\\/'),
|
|
// routeKeywords.subscribe: 'subscribe'
|
|
subscribePattern = new RegExp('^\\/subscribe\\/'),
|
|
// routeKeywords.amp: 'amp'
|
|
ampPattern = new RegExp('\\/amp\\/$'),
|
|
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
|
|
// See shared/middlewares/ghost-locals
|
|
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');
|
|
}
|
|
|
|
// Add context 'amp' to either post or page, if we have an `*/amp` route
|
|
if (ampPattern.test(res.locals.relativeUrl) && (data.post || data.page)) {
|
|
res.locals.context.push('amp');
|
|
}
|
|
|
|
// Each page can only have at most one of these
|
|
if (res.routerOptions && res.routerOptions.context) {
|
|
res.locals.context = res.locals.context.concat(res.routerOptions.context);
|
|
}
|
|
|
|
if (privatePattern.test(res.locals.relativeUrl)) {
|
|
if (!res.locals.context.includes('private')) {
|
|
res.locals.context.push('private');
|
|
}
|
|
}
|
|
|
|
if (subscribePattern.test(res.locals.relativeUrl) && labs.isSet('subscribers') === true) {
|
|
if (!res.locals.context.includes('subscribe')) {
|
|
res.locals.context.push('subscribe');
|
|
}
|
|
}
|
|
|
|
if (data && data.post) {
|
|
if (!res.locals.context.includes('post')) {
|
|
res.locals.context.push('post');
|
|
}
|
|
} else if (data && data.page) {
|
|
if (!res.locals.context.includes('page')) {
|
|
res.locals.context.push('page');
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = setResponseContext;
|