2018-05-23 13:14:38 +02:00
|
|
|
const { rtrimSlashes } = require('lib/path-utils');
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
const urlUtils = {};
|
|
|
|
|
|
|
|
urlUtils.hash = function(url) {
|
|
|
|
const s = url.split('#');
|
|
|
|
if (s.length <= 1) return '';
|
|
|
|
return s[s.length - 1];
|
|
|
|
}
|
|
|
|
|
2018-05-23 13:14:38 +02:00
|
|
|
urlUtils.urlWithoutPath = function(url) {
|
|
|
|
const parsed = require('url').parse(url, true);
|
|
|
|
return parsed.protocol + '//' + parsed.host;
|
|
|
|
}
|
|
|
|
|
|
|
|
urlUtils.urlProtocol = function(url) {
|
|
|
|
const parsed = require('url').parse(url, true);
|
|
|
|
return parsed.protocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
urlUtils.prependBaseUrl = function(url, baseUrl) {
|
|
|
|
baseUrl = rtrimSlashes(baseUrl).trim(); // All the code below assumes that the baseUrl does not end up with a slash
|
|
|
|
url = url.trim();
|
|
|
|
|
|
|
|
if (!url) url = '';
|
|
|
|
if (!baseUrl) return url;
|
2018-05-24 13:44:13 +02:00
|
|
|
if (urlUtils.urlProtocol(url)) return url; // Don't prepend the base URL if the URL already has a scheme
|
2018-05-23 13:14:38 +02:00
|
|
|
|
|
|
|
if (url.length >= 2 && url.indexOf('//') === 0) { // If it starts with // it's a protcol-relative URL
|
|
|
|
return urlUtils.urlProtocol(baseUrl) + url;
|
|
|
|
} else if (url && url[0] === '/') { // If it starts with a slash, it's an absolute URL so it should be relative to the domain (and not to the full baseUrl)
|
|
|
|
return urlUtils.urlWithoutPath(baseUrl) + url;
|
|
|
|
} else {
|
|
|
|
return baseUrl + (url ? '/' + url : '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
module.exports = urlUtils;
|