2015-03-21 12:14:51 +00: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}}'
|
2022-04-05 14:28:20 +01:00
|
|
|
const {api} = require('../services/proxy');
|
2022-04-05 17:38:46 +01:00
|
|
|
const {hbs} = require('../services/handlebars');
|
2022-04-05 14:28:20 +01:00
|
|
|
const {checks} = require('../services/data');
|
2015-03-21 12:14:51 +00:00
|
|
|
|
2021-09-26 21:01:13 +01:00
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-03-30 21:23:02 +01:00
|
|
|
const get = require('lodash/get');
|
|
|
|
const moment = require('moment');
|
2015-03-21 12:14:51 +00:00
|
|
|
|
2021-09-23 20:46:22 +01:00
|
|
|
const messages = {
|
2021-10-04 16:50:07 +01:00
|
|
|
mustBeCalledAsBlock: 'The {\\{{helperName}}} helper must be called as a block. E.g. {{#{helperName}}}...{{/{helperName}}}'
|
2021-09-23 20:46:22 +01:00
|
|
|
};
|
|
|
|
|
2020-03-30 21:23:02 +01:00
|
|
|
const createFrame = hbs.handlebars.createFrame;
|
2017-08-27 18:00:32 +02:00
|
|
|
|
2020-03-30 21:23:02 +01:00
|
|
|
const buildApiOptions = function buildApiOptions(options, post) {
|
2020-04-29 16:44:27 +01: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 13:43:06 +01:00
|
|
|
* @deprecated: single authors was superceded by multiple authors in Ghost 1.22.0
|
2020-04-29 16:44:27 +01:00
|
|
|
*/
|
2022-01-26 16:55:04 +05:30
|
|
|
include: 'author,authors,tags,tiers',
|
2020-04-29 16:44:27 +01: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
|
2022-03-03 16:18:05 +01:00
|
|
|
filter: "slug:-" + slug + "+published_at:" + op + "'" + publishedAt + "'", // eslint-disable-line quotes
|
|
|
|
context: {member: options.data.member}
|
2020-04-29 16:44:27 +01:00
|
|
|
};
|
2017-10-13 15:44:39 +01:00
|
|
|
|
2020-03-30 21:23:02 +01:00
|
|
|
if (get(options, 'hash.in')) {
|
|
|
|
if (options.hash.in === 'primary_tag' && get(post, 'primary_tag.slug')) {
|
2017-10-19 01:12:20 +08:00
|
|
|
apiOptions.filter += '+primary_tag:' + post.primary_tag.slug;
|
2020-03-30 21:23:02 +01:00
|
|
|
} else if (options.hash.in === 'primary_author' && get(post, 'primary_author.slug')) {
|
2018-03-27 16:16:15 +02:00
|
|
|
apiOptions.filter += '+primary_author:' + post.primary_author.slug;
|
2020-03-30 21:23:02 +01:00
|
|
|
} else if (options.hash.in === 'author' && get(post, 'author.slug')) {
|
2017-10-19 01:12:20 +08:00
|
|
|
apiOptions.filter += '+author:' + post.author.slug;
|
|
|
|
}
|
2017-10-13 15:44:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiOptions;
|
|
|
|
};
|
|
|
|
|
2022-03-03 16:18:05 +01:00
|
|
|
/**
|
2022-04-05 14:28:20 +01:00
|
|
|
* @param {*} options
|
|
|
|
* @param {*} data
|
2022-03-03 16:18:05 +01:00
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-05-11 09:51:38 +01:00
|
|
|
const fetch = async function fetch(options, data) {
|
2018-10-17 09:23:59 +02:00
|
|
|
const self = this;
|
|
|
|
const apiOptions = buildApiOptions(options, this);
|
2017-08-27 18:00:32 +02:00
|
|
|
|
2022-05-11 09:51:38 +01:00
|
|
|
try {
|
|
|
|
const response = await api.postsPublic.browse(apiOptions);
|
|
|
|
|
|
|
|
const related = response.posts[0];
|
|
|
|
|
|
|
|
if (related) {
|
|
|
|
return options.fn(related, {data: data});
|
|
|
|
} else {
|
2017-08-27 18:00:32 +02:00
|
|
|
return options.inverse(self, {data: data});
|
2022-05-11 09:51:38 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logging.error(error);
|
|
|
|
data.error = error.message;
|
|
|
|
return options.inverse(self, {data: data});
|
|
|
|
}
|
2015-03-21 12:14:51 +00: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.
|
|
|
|
|
2022-03-03 16:18:05 +01:00
|
|
|
/**
|
2022-04-05 14:28:20 +01:00
|
|
|
* @param {*} options
|
2022-03-03 16:18:05 +01:00
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-05-11 09:51:38 +01:00
|
|
|
module.exports = async function prevNext(options) {
|
2015-03-21 12:14:51 +00:00
|
|
|
options = options || {};
|
2015-04-22 19:56:56 +01:00
|
|
|
|
2019-01-21 11:48:18 +01:00
|
|
|
const data = createFrame(options.data);
|
|
|
|
const context = options.data.root.context;
|
2017-08-27 18:00:32 +02:00
|
|
|
|
2017-10-13 15:44:39 +01:00
|
|
|
// Guard against incorrect usage of the helpers
|
|
|
|
if (!options.fn || !options.inverse) {
|
2021-09-23 20:46:22 +01:00
|
|
|
data.error = tpl(messages.mustBeCalledAsBlock, {helperName: options.name});
|
2017-08-27 18:00:32 +02:00
|
|
|
logging.warn(data.error);
|
2022-05-11 09:51:38 +01:00
|
|
|
return;
|
2017-08-27 18:00:32 +02:00
|
|
|
}
|
2015-04-22 19:56:56 +01:00
|
|
|
|
2019-01-21 11:48:18 +01:00
|
|
|
if (context.includes('preview')) {
|
2022-05-11 09:51:38 +01:00
|
|
|
return options.inverse(this, {data: data});
|
2019-01-21 11:48:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Guard against trying to execute prev/next on pages, or other resources
|
2020-03-30 21:23:02 +01:00
|
|
|
if (!checks.isPost(this) || this.page) {
|
2022-05-11 09:51:38 +01:00
|
|
|
return options.inverse(this, {data: data});
|
2015-03-21 12:14:51 +00:00
|
|
|
}
|
2017-10-13 15:44:39 +01: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 12:14:51 +00:00
|
|
|
};
|
2021-10-04 16:50:07 +01:00
|
|
|
|
|
|
|
module.exports.async = true;
|
|
|
|
module.exports.alias = 'next_post';
|