Ghost/core/server/controllers/frontend/post-lookup.js
Aileen Nowak 2f3081fa9f Make AMP optional (#7830)
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
2017-01-17 16:40:06 +01:00

71 lines
2.2 KiB
JavaScript

var _ = require('lodash'),
Promise = require('bluebird'),
url = require('url'),
routeMatch = require('path-match')(),
api = require('../../api'),
config = require('../../config'),
optionsFormat = '/:options?';
function getOptionsFormat(linkStructure) {
return linkStructure.replace(/\/$/, '') + optionsFormat;
}
function postLookup(postUrl) {
var postPath = url.parse(postUrl).path,
postPermalink = config.get('theme').permalinks,
pagePermalink = '/:slug/',
isEditURL = false,
matchFuncPost,
matchFuncPage,
postParams,
params;
// Convert saved permalink into a path-match function
matchFuncPost = routeMatch(getOptionsFormat(postPermalink));
matchFuncPage = routeMatch(getOptionsFormat(pagePermalink));
postParams = matchFuncPost(postPath);
// Check if the path matches the permalink structure.
// If there are no matches found, test to see if this is a page instead
params = postParams || matchFuncPage(postPath);
// if there are no matches for either then return empty
if (params === false) {
return Promise.resolve();
}
// If params contains options, and it is equal to 'edit', this is an edit URL
if (params.options && params.options.toLowerCase() === 'edit') {
isEditURL = true;
}
// Query database to find post
return api.posts.read(_.extend(_.pick(params, 'slug', 'id'), {include: 'author,tags'})).then(function then(result) {
var post = result.posts[0];
if (!post) {
return Promise.resolve();
}
// CASE: we originally couldn't match the post based on date permalink and we tried to check if its a page
if (!post.page && !postParams) {
return Promise.resolve();
}
// CASE: we only support /:slug format for pages
if (post.page && matchFuncPage(postPath) === false) {
return Promise.resolve();
}
return {
post: post,
isEditURL: isEditURL,
isUnknownOption: isEditURL ? false : params.options ? true : false
};
});
}
module.exports = postLookup;