2020-06-04 00:45:26 +08:00
|
|
|
let cdn, baseUrl, baseUri;
|
|
|
|
let S3BaseUrl, S3CDN;
|
2016-09-23 02:39:36 +08:00
|
|
|
|
|
|
|
export default function getURL(url) {
|
2020-06-05 20:36:48 +08:00
|
|
|
if (baseUri === undefined) {
|
2016-09-23 02:39:36 +08:00
|
|
|
baseUri = $('meta[name="discourse-base-uri"]').attr("content") || "";
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:42:50 +08:00
|
|
|
if (!url) {
|
|
|
|
return baseUri === "/" ? "" : baseUri;
|
|
|
|
}
|
|
|
|
|
2016-09-23 02:39:36 +08:00
|
|
|
// if it's a non relative URL, return it.
|
2020-09-22 22:28:28 +08:00
|
|
|
if (url !== "/" && !/^\/[^\/]/.test(url)) {
|
|
|
|
return url;
|
|
|
|
}
|
2016-09-23 02:39:36 +08:00
|
|
|
|
2021-01-22 05:43:14 +08:00
|
|
|
const found = url.startsWith(baseUri);
|
2016-09-23 02:39:36 +08:00
|
|
|
|
2021-01-22 05:43:14 +08:00
|
|
|
if (found) {
|
2020-09-22 22:28:28 +08:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
if (url[0] !== "/") {
|
|
|
|
url = "/" + url;
|
|
|
|
}
|
2016-09-23 02:39:36 +08:00
|
|
|
|
|
|
|
return baseUri + url;
|
|
|
|
}
|
2020-06-04 00:45:26 +08: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) {
|
|
|
|
const rootURL = (!baseUri ? "/" : baseUri).replace(/\/$/, "");
|
|
|
|
return path.replace(rootURL, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setPrefix(configBaseUri) {
|
|
|
|
baseUri = configBaseUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupURL(configCdn, configBaseUrl, configBaseUri) {
|
|
|
|
cdn = configCdn;
|
|
|
|
baseUrl = configBaseUrl;
|
|
|
|
baseUri = configBaseUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupS3CDN(configS3BaseUrl, configS3CDN) {
|
|
|
|
S3BaseUrl = configS3BaseUrl;
|
|
|
|
S3CDN = configS3CDN;
|
|
|
|
}
|