Ghost/ghost/config-url-helpers/lib/config-url-helpers.js

79 lines
1.7 KiB
JavaScript
Raw Normal View History

const deduplicateSubdirectory = require('./utils/deduplicate-subdirectory');
/**
* Returns a subdirectory URL, if defined so in the config.
2021-06-17 14:04:20 +03:00
* @callback getSubdirFn
* @return {string} a subdirectory if configured.
*/
function getSubdir() {
// Parse local path location
let {pathname} = new URL(this.get('url'));
let subdir;
// Remove trailing slash
if (pathname !== '/') {
pathname = pathname.replace(/\/$/, '');
}
subdir = pathname === '/' ? '' : pathname;
return subdir;
}
/**
* Returns the base URL of the site as set in the config.
*
* Secure:
* If the request is secure, we want to force returning the site url as https.
* Imagine Ghost runs with http, but nginx allows SSL connections.
*
2021-06-17 14:04:20 +03:00
* @callback getSiteUrlFn
* @param {boolean} [secure] optionally force the url to be secure
* @return {string} returns the url as defined in config, but always with a trailing `/`
*/
function getSiteUrl(secure = false) {
let siteUrl = this.get('url');
if (secure) {
siteUrl = siteUrl.replace('http://', 'https://');
}
if (!siteUrl.match(/\/$/)) {
siteUrl += '/';
}
return siteUrl;
}
/**
*
2021-06-17 14:04:20 +03:00
* @callback getAdminUrlFn
* @returns {string} returns the url as defined in config, but always with a trailing `/`
*/
function getAdminUrl() {
let adminUrl = this.get('admin:url');
const subdir = this.getSubdir();
if (!adminUrl) {
return;
}
if (!adminUrl.match(/\/$/)) {
adminUrl += '/';
}
adminUrl = `${adminUrl}${subdir}`;
if (!adminUrl.match(/\/$/)) {
adminUrl += '/';
}
adminUrl = deduplicateSubdirectory(adminUrl, this.getSiteUrl());
return adminUrl;
}
module.exports = {
getSubdir,
getSiteUrl,
getAdminUrl
};