2015-03-21 15:14:51 +03:00
|
|
|
// ### prevNext helper exposes methods for prev_post and next_post - separately defined in helpers index.
|
|
|
|
// Example usages
|
|
|
|
// `{{#prev_post}}<a href ="{{url}}>previous post</a>{{/prev_post}}'
|
|
|
|
// `{{#next_post}}<a href ="{{url absolute="true">next post</a>{{/next_post}}'
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
2017-10-13 17:44:39 +03:00
|
|
|
_ = require('lodash'),
|
2017-04-04 19:07:35 +03:00
|
|
|
Promise = require('bluebird'),
|
2017-10-13 17:44:39 +03:00
|
|
|
moment = require('moment'),
|
2015-03-21 15:14:51 +03:00
|
|
|
|
2017-08-27 19:00:32 +03:00
|
|
|
logging = proxy.logging,
|
|
|
|
i18n = proxy.i18n,
|
|
|
|
createFrame = proxy.hbs.handlebars.createFrame,
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
api = proxy.api,
|
|
|
|
isPost = proxy.checks.isPost,
|
|
|
|
|
2017-10-13 17:44:39 +03:00
|
|
|
fetch,
|
|
|
|
buildApiOptions;
|
2017-04-04 19:07:35 +03:00
|
|
|
|
2017-10-13 17:44:39 +03:00
|
|
|
buildApiOptions = function buildApiOptions(options, post) {
|
|
|
|
var publishedAt = moment(post.published_at).format('YYYY-MM-DD HH:mm:ss'),
|
|
|
|
slug = post.slug,
|
|
|
|
op = options.name === 'prev_post' ? '<=' : '>',
|
|
|
|
order = options.name === 'prev_post' ? 'desc' : 'asc',
|
|
|
|
apiOptions = {
|
2018-03-27 17:16:15 +03:00
|
|
|
/**
|
2018-08-03 16:14:38 +03:00
|
|
|
* @deprecated: `author`, will be removed in Ghost 3.0
|
2018-03-27 17:16:15 +03:00
|
|
|
*/
|
|
|
|
include: 'author,authors,tags',
|
2017-10-13 17:44:39 +03:00
|
|
|
order: 'published_at ' + order,
|
|
|
|
limit: 1,
|
|
|
|
// This line deliberately uses double quotes because GQL cannot handle either double quotes
|
|
|
|
// or escaped singles, see TryGhost/GQL#34
|
2017-11-01 16:44:54 +03:00
|
|
|
filter: "slug:-" + slug + "+published_at:" + op + "'" + publishedAt + "'" // eslint-disable-line quotes
|
2017-10-13 17:44:39 +03:00
|
|
|
};
|
|
|
|
|
2017-10-18 20:12:20 +03:00
|
|
|
if (_.get(options, 'hash.in')) {
|
|
|
|
if (options.hash.in === 'primary_tag' && _.get(post, 'primary_tag.slug')) {
|
|
|
|
apiOptions.filter += '+primary_tag:' + post.primary_tag.slug;
|
2018-03-27 17:16:15 +03:00
|
|
|
} else if (options.hash.in === 'primary_author' && _.get(post, 'primary_author.slug')) {
|
|
|
|
apiOptions.filter += '+primary_author:' + post.primary_author.slug;
|
2017-10-18 20:12:20 +03:00
|
|
|
} else if (options.hash.in === 'author' && _.get(post, 'author.slug')) {
|
|
|
|
apiOptions.filter += '+author:' + post.author.slug;
|
|
|
|
}
|
2017-10-13 17:44:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiOptions;
|
|
|
|
};
|
|
|
|
|
|
|
|
fetch = function fetch(options, data) {
|
2018-10-17 10:23:59 +03:00
|
|
|
const self = this;
|
|
|
|
const apiOptions = buildApiOptions(options, this);
|
|
|
|
const apiVersion = data.root._locals.apiVersion;
|
2017-08-27 19:00:32 +03:00
|
|
|
|
2019-02-27 20:08:36 +03:00
|
|
|
// @TODO: https://github.com/TryGhost/Ghost/issues/10548
|
2019-02-27 16:51:54 +03:00
|
|
|
const controller = api[apiVersion].postsPublic || api[apiVersion].posts;
|
|
|
|
|
|
|
|
return controller
|
2017-10-13 17:44:39 +03:00
|
|
|
.browse(apiOptions)
|
2017-08-27 19:00:32 +03:00
|
|
|
.then(function handleSuccess(result) {
|
|
|
|
var related = result.posts[0];
|
|
|
|
|
2017-10-13 17:44:39 +03:00
|
|
|
if (related) {
|
|
|
|
return options.fn(related, {data: data});
|
2017-08-27 19:00:32 +03:00
|
|
|
} else {
|
|
|
|
return options.inverse(self, {data: data});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function handleError(err) {
|
|
|
|
logging.error(err);
|
|
|
|
data.error = err.message;
|
|
|
|
return options.inverse(self, {data: data});
|
|
|
|
});
|
2015-03-21 15:14:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// If prevNext method is called without valid post data then we must return a promise, if there is valid post data
|
|
|
|
// then the promise is handled in the api call.
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function prevNext(options) {
|
2015-03-21 15:14:51 +03:00
|
|
|
options = options || {};
|
2015-04-22 21:56:56 +03:00
|
|
|
|
2019-01-21 13:48:18 +03:00
|
|
|
const data = createFrame(options.data);
|
|
|
|
const context = options.data.root.context;
|
2017-08-27 19:00:32 +03:00
|
|
|
|
2017-10-13 17:44:39 +03:00
|
|
|
// Guard against incorrect usage of the helpers
|
|
|
|
if (!options.fn || !options.inverse) {
|
2017-08-27 19:00:32 +03:00
|
|
|
data.error = i18n.t('warnings.helpers.mustBeCalledAsBlock', {helperName: options.name});
|
|
|
|
logging.warn(data.error);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2015-04-22 21:56:56 +03:00
|
|
|
|
2019-01-21 13:48:18 +03:00
|
|
|
if (context.includes('preview')) {
|
|
|
|
return Promise.resolve(options.inverse(this, {data: data}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guard against trying to execute prev/next on pages, or other resources
|
|
|
|
if (!isPost(this) || this.page) {
|
2017-08-27 19:00:32 +03:00
|
|
|
return Promise.resolve(options.inverse(this, {data: data}));
|
2015-03-21 15:14:51 +03:00
|
|
|
}
|
2017-10-13 17:44:39 +03:00
|
|
|
|
|
|
|
// With the guards out of the way, attempt to build the apiOptions, and then fetch the data
|
|
|
|
return fetch.call(this, options, data);
|
2015-03-21 15:14:51 +03:00
|
|
|
};
|