mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-15 19:52:01 +03:00
5f2c913fc1
refs #5091, #6612 - unify getNextUrl & getPrevUrl into getPaginatedUrl - ensure that it can generate a prev, next or exact page no url - ensure that it can figure out the base url - use the same code from the page_url helper - refactor the tests to ensure there's 100% coverage Following on from #6612, this ensures that pagination always works regardless of whether the channel is default or custom
36 lines
1.5 KiB
JavaScript
36 lines
1.5 KiB
JavaScript
var _ = require('lodash'),
|
|
config = require('../../config');
|
|
|
|
function getPaginatedUrl(page, data, absolute) {
|
|
// If we don't have enough information, return null right away
|
|
if (!data || !data.relativeUrl || !data.pagination) {
|
|
return null;
|
|
}
|
|
|
|
var pagePath = '/' + config.routeKeywords.page + '/',
|
|
// Try to match the base url, as whatever precedes the pagePath
|
|
baseUrlPattern = new RegExp('(.+)?(/' + config.routeKeywords.page + '/\\d+/)'),
|
|
baseUrlMatch = data.relativeUrl.match(baseUrlPattern),
|
|
// If there is no match for pagePath, use the original url, without the trailing slash
|
|
baseUrl = baseUrlMatch ? baseUrlMatch[1] : data.relativeUrl.slice(0, -1),
|
|
newRelativeUrl;
|
|
|
|
if (page === 'next' && data.pagination.next) {
|
|
newRelativeUrl = pagePath + data.pagination.next + '/';
|
|
} else if (page === 'prev' && data.pagination.prev) {
|
|
newRelativeUrl = data.pagination.prev > 1 ? pagePath + data.pagination.prev + '/' : '/';
|
|
} else if (_.isNumber(page)) {
|
|
newRelativeUrl = page > 1 ? pagePath + page + '/' : '/';
|
|
} else {
|
|
// If none of the cases match, return null right away
|
|
return null;
|
|
}
|
|
|
|
// baseUrl can be undefined, if there was nothing preceding the pagePath (e.g. first page of the index channel)
|
|
newRelativeUrl = baseUrl ? baseUrl + newRelativeUrl : newRelativeUrl;
|
|
|
|
return config.urlFor({relativeUrl: newRelativeUrl, secure: data.secure}, absolute);
|
|
}
|
|
|
|
module.exports = getPaginatedUrl;
|