2021-02-19 14:06:18 -05:00
|
|
|
let cdn, baseUrl, baseUri, baseUriMatcher;
|
2020-06-03 12:45:26 -04:00
|
|
|
let S3BaseUrl, S3CDN;
|
2016-09-22 14:39:36 -04:00
|
|
|
|
|
|
|
export default function getURL(url) {
|
2020-06-05 08:36:48 -04:00
|
|
|
if (baseUri === undefined) {
|
2016-09-22 14:39:36 -04:00
|
|
|
baseUri = $('meta[name="discourse-base-uri"]').attr("content") || "";
|
2021-02-19 14:06:18 -05:00
|
|
|
baseUriMatcher = new RegExp(`^${baseUri}`);
|
2016-09-22 14:39:36 -04:00
|
|
|
}
|
|
|
|
|
2020-08-25 05:42:50 -04:00
|
|
|
if (!url) {
|
|
|
|
return baseUri === "/" ? "" : baseUri;
|
|
|
|
}
|
|
|
|
|
2016-09-22 14:39:36 -04:00
|
|
|
// if it's a non relative URL, return it.
|
2020-09-22 10:28:28 -04:00
|
|
|
if (url !== "/" && !/^\/[^\/]/.test(url)) {
|
|
|
|
return url;
|
|
|
|
}
|
2016-09-22 14:39:36 -04:00
|
|
|
|
2021-01-21 16:43:14 -05:00
|
|
|
const found = url.startsWith(baseUri);
|
2016-09-22 14:39:36 -04:00
|
|
|
|
2021-01-21 16:43:14 -05:00
|
|
|
if (found) {
|
2020-09-22 10:28:28 -04:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
if (url[0] !== "/") {
|
|
|
|
url = "/" + url;
|
|
|
|
}
|
2016-09-22 14:39:36 -04:00
|
|
|
|
|
|
|
return baseUri + url;
|
|
|
|
}
|
2020-06-03 12:45:26 -04:00
|
|
|
|
|
|
|
export function getURLWithCDN(url) {
|
|
|
|
url = getURL(url);
|
|
|
|
// only relative urls
|
|
|
|
if (cdn && /^\/[^\/]/.test(url)) {
|
|
|
|
url = cdn + url;
|
|
|
|
} else if (S3CDN) {
|
|
|
|
url = url.replace(S3BaseUrl, S3CDN);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAbsoluteURL(path) {
|
|
|
|
return baseUrl + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isAbsoluteURL(url) {
|
|
|
|
return url.startsWith(baseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withoutPrefix(path) {
|
2021-02-19 14:06:18 -05:00
|
|
|
if (!baseUri) {
|
|
|
|
return path;
|
|
|
|
} else {
|
|
|
|
return path.replace(baseUriMatcher, "");
|
|
|
|
}
|
2020-06-03 12:45:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setPrefix(configBaseUri) {
|
|
|
|
baseUri = configBaseUri;
|
2021-02-19 14:06:18 -05:00
|
|
|
baseUriMatcher = new RegExp(`^${baseUri}`);
|
2020-06-03 12:45:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setupURL(configCdn, configBaseUrl, configBaseUri) {
|
|
|
|
cdn = configCdn;
|
|
|
|
baseUrl = configBaseUrl;
|
|
|
|
baseUri = configBaseUri;
|
2021-02-19 14:06:18 -05:00
|
|
|
baseUriMatcher = new RegExp(`^${baseUri}`);
|
2020-06-03 12:45:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setupS3CDN(configS3BaseUrl, configS3CDN) {
|
|
|
|
S3BaseUrl = configS3BaseUrl;
|
|
|
|
S3CDN = configS3CDN;
|
|
|
|
}
|