Ghost/core/frontend/services/routing/helpers/context.js
Hannah Wolfe 22e13acd65 Updated var declarations to const/let and no lists
- All var declarations are now const or let as per ES6
- All comma-separated lists / chained declarations are now one declaration per line
- This is for clarity/readability but also made running the var-to-const/let switch smoother
- ESLint rules updated to match

How this was done:

- npm install -g jscodeshift
- git clone https://github.com/cpojer/js-codemod.git
- git clone git@github.com:TryGhost/Ghost.git shallow-ghost
- cd shallow-ghost
- jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2
- jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2
- yarn
- yarn test
- yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode
- grunt test-regression
- sorted!
2020-04-29 16:51:13 +01:00

78 lines
2.7 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
*/
// @TODO: fix this!! These regexes are app specific and should be dynamic. They should not belong here....
// routeKeywords.private: 'private'
const privatePattern = new RegExp('^\\/private\\/');
// routeKeywords.amp: 'amp'
const ampPattern = new RegExp('\\/amp\\/$');
const homePattern = new RegExp('^\\/$');
function setResponseContext(req, res, data) {
const 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 web/parent/middleware/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');
}
}
// @TODO: remove first if condition when only page key is returned
// ref.: https://github.com/TryGhost/Ghost/issues/10042
if (data && data.post && data.post.page) {
if (!res.locals.context.includes('page')) {
res.locals.context.push('page');
}
} else 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;