mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
2f3081fa9f
closes #7769 Because Google AMP is bitching around and shows errors in Googles' webmaster tools for missing post images and blog icons, we decided to make AMP optional. It will be enabled by default, but can be disabled in general settings. Once disabled, the `amp` route doesn't work anymore. This PR contains the back end changes for Ghost-alpha: - Adds `amp` to settings table incl default setting `true` - Adds `amp` value to our settings cache - Changes the route handling of AMP app to check for the `amp` setting first. - Adds tests to check the route handling and ghost_head output - Includes changes to `post-lookup.js` as done by @kirrg001 in #7842
95 lines
2.9 KiB
JavaScript
95 lines
2.9 KiB
JavaScript
/**
|
|
* Main controller for Ghost frontend
|
|
*/
|
|
|
|
/*global require, module */
|
|
|
|
var debug = require('debug')('ghost:channels:single'),
|
|
api = require('../../api'),
|
|
utils = require('../../utils'),
|
|
filters = require('../../filters'),
|
|
templates = require('./templates'),
|
|
handleError = require('./error'),
|
|
formatResponse = require('./format-response'),
|
|
postLookup = require('./post-lookup'),
|
|
setResponseContext = require('./context'),
|
|
setRequestIsSecure = require('./secure'),
|
|
|
|
frontendControllers;
|
|
|
|
/*
|
|
* Sets the response context around a post and renders it
|
|
* with the current theme's post view. Used by post preview
|
|
* and single post methods.
|
|
* Returns a function that takes the post to be rendered.
|
|
*/
|
|
function renderPost(req, res) {
|
|
debug('renderPost called');
|
|
return function renderPost(post) {
|
|
var view = templates.single(req.app.get('activeTheme'), post),
|
|
response = formatResponse.single(post);
|
|
|
|
setResponseContext(req, res, response);
|
|
debug('Rendering view: ' + view);
|
|
res.render(view, response);
|
|
};
|
|
}
|
|
|
|
frontendControllers = {
|
|
preview: function preview(req, res, next) {
|
|
var params = {
|
|
uuid: req.params.uuid,
|
|
status: 'all',
|
|
include: 'author,tags'
|
|
};
|
|
|
|
api.posts.read(params).then(function then(result) {
|
|
var post = result.posts[0];
|
|
|
|
if (!post) {
|
|
return next();
|
|
}
|
|
|
|
if (post.status === 'published') {
|
|
return res.redirect(301, utils.url.urlFor('post', {post: post}));
|
|
}
|
|
|
|
setRequestIsSecure(req, post);
|
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
.then(renderPost(req, res));
|
|
}).catch(handleError(next));
|
|
},
|
|
single: function single(req, res, next) {
|
|
// Query database to find post
|
|
return postLookup(req.path).then(function then(lookup) {
|
|
var post = lookup ? lookup.post : false;
|
|
|
|
if (!post) {
|
|
return next();
|
|
}
|
|
|
|
// CASE: postlookup can detect options for example /edit, unknown options get ignored and end in 404
|
|
if (lookup.isUnknownOption) {
|
|
return next();
|
|
}
|
|
|
|
// CASE: last param is of url is /edit, redirect to admin
|
|
if (lookup.isEditURL) {
|
|
return res.redirect(utils.url.urlJoin(utils.url.getSubdir(), '/ghost/editor', post.id, '/'));
|
|
}
|
|
|
|
// CASE: permalink is not valid anymore, we redirect him permanently to the correct one
|
|
if (post.url !== req.path) {
|
|
return res.redirect(301, post.url);
|
|
}
|
|
|
|
setRequestIsSecure(req, post);
|
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
.then(renderPost(req, res));
|
|
}).catch(handleError(next));
|
|
}
|
|
};
|
|
|
|
module.exports = frontendControllers;
|