Ghost/core/frontend/meta/paginated-url.js
Hannah Wolfe b29852b012
🔥 Removed support for http/https mixed mode (#14783)
closes: https://github.com/TryGhost/Toolbox/issues/324
refs: https://github.com/TryGhost/Ghost/issues/14446

- Currently, if url is configured to http but a request is marked secure, Ghost will handle upgrading all internal URLs to https so that there are no mixed content warnings
- From 5.0 that feature is going away, in favour of strictly honouring the configured URL
- Ghost will serve URLs exactly as configured and won't upgrade http to https anymore
- This use case was common when Ghost was first built, but in 2022 the web is mostly https.
- The code needed to support the feature creates a lot of additional complexity & maintenance overhead, so removing this gives us space to do more cool and useful stuff in 2022
2022-05-11 14:53:23 +01:00

42 lines
1.6 KiB
JavaScript

const _ = require('lodash');
const urlUtils = require('../../shared/url-utils');
function getPaginatedUrl(page, data, absolute) {
// If we don't have enough information, return null right away
if (!data || !data.relativeUrl || !data.pagination) {
return null;
}
// routeKeywords.page: 'page'
const pagePath = urlUtils.urlJoin('/page/');
// Try to match the base url, as whatever precedes the pagePath
// routeKeywords.page: 'page'
const baseUrlPattern = new RegExp('(.+)?(/page/\\d+/)');
const baseUrlMatch = data.relativeUrl.match(baseUrlPattern);
// If there is no match for pagePath, use the original url, without the trailing slash
const baseUrl = baseUrlMatch ? baseUrlMatch[1] : data.relativeUrl.slice(0, -1);
let newRelativeUrl;
if (page === 'next' && data.pagination.next) {
newRelativeUrl = urlUtils.urlJoin(pagePath, data.pagination.next, '/');
} else if (page === 'prev' && data.pagination.prev) {
newRelativeUrl = data.pagination.prev > 1 ? urlUtils.urlJoin(pagePath, data.pagination.prev, '/') : '/';
} else if (_.isNumber(page)) {
newRelativeUrl = page > 1 ? urlUtils.urlJoin(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 ? urlUtils.urlJoin(baseUrl, newRelativeUrl) : newRelativeUrl;
return urlUtils.urlFor({relativeUrl: newRelativeUrl}, absolute);
}
module.exports = getPaginatedUrl;