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}}'
|
|
|
|
|
2020-04-08 18:56:37 +03:00
|
|
|
const {logging, i18n, api, hbs, checks} = require('../services/proxy');
|
2020-03-30 23:23:02 +03:00
|
|
|
const get = require('lodash/get');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const moment = require('moment');
|
2015-03-21 15:14:51 +03:00
|
|
|
|
2020-03-30 23:23:02 +03:00
|
|
|
const createFrame = hbs.handlebars.createFrame;
|
2017-08-27 19:00:32 +03:00
|
|
|
|
2020-03-30 23:23:02 +03:00
|
|
|
const buildApiOptions = function buildApiOptions(options, post) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const publishedAt = moment(post.published_at).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
const slug = post.slug;
|
|
|
|
const op = options.name === 'prev_post' ? '<=' : '>';
|
|
|
|
const order = options.name === 'prev_post' ? 'desc' : 'asc';
|
|
|
|
|
|
|
|
const apiOptions = {
|
|
|
|
/**
|
2021-05-04 15:43:06 +03:00
|
|
|
* @deprecated: single authors was superceded by multiple authors in Ghost 1.22.0
|
2020-04-29 18:44:27 +03:00
|
|
|
*/
|
|
|
|
include: 'author,authors,tags',
|
|
|
|
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
|
|
|
|
filter: "slug:-" + slug + "+published_at:" + op + "'" + publishedAt + "'" // eslint-disable-line quotes
|
|
|
|
};
|
2017-10-13 17:44:39 +03:00
|
|
|
|
2020-03-30 23:23:02 +03:00
|
|
|
if (get(options, 'hash.in')) {
|
|
|
|
if (options.hash.in === 'primary_tag' && get(post, 'primary_tag.slug')) {
|
2017-10-18 20:12:20 +03:00
|
|
|
apiOptions.filter += '+primary_tag:' + post.primary_tag.slug;
|
2020-03-30 23:23:02 +03:00
|
|
|
} else if (options.hash.in === 'primary_author' && get(post, 'primary_author.slug')) {
|
2018-03-27 17:16:15 +03:00
|
|
|
apiOptions.filter += '+primary_author:' + post.primary_author.slug;
|
2020-03-30 23:23:02 +03:00
|
|
|
} else if (options.hash.in === 'author' && get(post, 'author.slug')) {
|
2017-10-18 20:12:20 +03:00
|
|
|
apiOptions.filter += '+author:' + post.author.slug;
|
|
|
|
}
|
2017-10-13 17:44:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiOptions;
|
|
|
|
};
|
|
|
|
|
2020-03-30 23:23:02 +03:00
|
|
|
const 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) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const related = result.posts[0];
|
2017-08-27 19:00:32 +03:00
|
|
|
|
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
|
2020-03-30 23:23:02 +03:00
|
|
|
if (!checks.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
|
|
|
};
|