2021-02-20 03:06:18 +08:00
|
|
|
let cdn, baseUrl, baseUri, baseUriMatcher;
|
2020-06-04 00:45:26 +08:00
|
|
|
let S3BaseUrl, S3CDN;
|
2016-09-23 02:39:36 +08:00
|
|
|
|
2022-02-01 04:31:13 +08:00
|
|
|
let snapshot;
|
|
|
|
|
2016-09-23 02:39:36 +08:00
|
|
|
export default function getURL(url) {
|
2020-06-05 20:36:48 +08:00
|
|
|
if (baseUri === undefined) {
|
2021-03-05 05:46:22 +08:00
|
|
|
setPrefix($('meta[name="discourse-base-uri"]').attr("content") || "");
|
2016-09-23 02:39:36 +08:00
|
|
|
}
|
|
|
|
|
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-03-05 05:46:22 +08:00
|
|
|
const found = baseUriMatcher.test(url);
|
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) {
|
2022-04-01 05:02:01 +08:00
|
|
|
return baseUrl + withoutPrefix(path);
|
2020-06-04 00:45:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isAbsoluteURL(url) {
|
|
|
|
return url.startsWith(baseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withoutPrefix(path) {
|
2021-02-20 03:06:18 +08:00
|
|
|
if (!baseUri) {
|
|
|
|
return path;
|
|
|
|
} else {
|
2021-03-05 05:46:22 +08:00
|
|
|
return path.replace(baseUriMatcher, "$1");
|
2021-02-20 03:06:18 +08:00
|
|
|
}
|
2020-06-04 00:45:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setPrefix(configBaseUri) {
|
|
|
|
baseUri = configBaseUri;
|
2021-03-05 05:46:22 +08:00
|
|
|
baseUriMatcher = new RegExp(`^${baseUri}(/|$)`);
|
2020-06-04 00:45:26 +08:00
|
|
|
}
|
|
|
|
|
2022-02-01 04:31:13 +08:00
|
|
|
export function setupURL(configCdn, configBaseUrl, configBaseUri, opts) {
|
|
|
|
opts = opts || {};
|
2020-06-04 00:45:26 +08:00
|
|
|
cdn = configCdn;
|
|
|
|
baseUrl = configBaseUrl;
|
2021-03-05 05:46:22 +08:00
|
|
|
setPrefix(configBaseUri);
|
2022-02-01 04:31:13 +08:00
|
|
|
|
|
|
|
if (opts?.snapshot) {
|
|
|
|
snapshot = {
|
|
|
|
cdn,
|
|
|
|
baseUri,
|
|
|
|
baseUrl,
|
|
|
|
configBaseUrl,
|
|
|
|
baseUriMatcher,
|
|
|
|
};
|
|
|
|
}
|
2020-06-04 00:45:26 +08:00
|
|
|
}
|
|
|
|
|
2022-02-01 04:31:13 +08:00
|
|
|
// In a test environment we might change these values and, after tests, want to restore them.
|
|
|
|
export function restoreBaseUri() {
|
|
|
|
if (snapshot) {
|
|
|
|
cdn = snapshot.cdn;
|
|
|
|
baseUri = snapshot.baseUri;
|
|
|
|
baseUrl = snapshot.baseUrl;
|
|
|
|
baseUriMatcher = snapshot.baseUriMatcher;
|
|
|
|
S3BaseUrl = snapshot.S3BaseUrl;
|
|
|
|
S3CDN = snapshot.S3CDN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupS3CDN(configS3BaseUrl, configS3CDN, opts) {
|
2020-06-04 00:45:26 +08:00
|
|
|
S3BaseUrl = configS3BaseUrl;
|
|
|
|
S3CDN = configS3CDN;
|
2022-02-01 04:31:13 +08:00
|
|
|
if (opts?.snapshot) {
|
|
|
|
snapshot = snapshot || {};
|
|
|
|
snapshot.S3BaseUrl = S3BaseUrl;
|
|
|
|
snapshot.S3CDN = S3CDN;
|
|
|
|
}
|
2020-06-04 00:45:26 +08:00
|
|
|
}
|
2021-06-15 10:30:36 +08:00
|
|
|
|
|
|
|
// We can use this to identify when navigating on the same host but outside of the
|
|
|
|
// prefix directory. For example from `/forum` to `/about-us` which is not discourse
|
|
|
|
export function samePrefix(url) {
|
|
|
|
if (baseUri === undefined) {
|
|
|
|
setPrefix($('meta[name="discourse-base-uri"]').attr("content") || "");
|
|
|
|
}
|
|
|
|
let origin = window.location.origin;
|
|
|
|
let cmp = url[0] === "/" ? baseUri || "/" : origin + baseUri || origin;
|
|
|
|
return url.indexOf(cmp) === 0;
|
|
|
|
}
|