mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 02:53:41 +08:00
dac0e5b3cc
Fixes for getURL() in subfolders and basePath in translations
62 lines
1.3 KiB
JavaScript
62 lines
1.3 KiB
JavaScript
let cdn, baseUrl, baseUri;
|
|
let S3BaseUrl, S3CDN;
|
|
|
|
export default function getURL(url) {
|
|
if (baseUri === undefined) {
|
|
baseUri = $('meta[name="discourse-base-uri"]').attr("content") || "";
|
|
}
|
|
|
|
if (!url) {
|
|
return baseUri === "/" ? "" : baseUri;
|
|
}
|
|
|
|
// if it's a non relative URL, return it.
|
|
if (url !== "/" && !/^\/[^\/]/.test(url)) return url;
|
|
|
|
const found = url.indexOf(baseUri);
|
|
|
|
if (found >= 0 && found < 3) return url;
|
|
if (url[0] !== "/") url = "/" + url;
|
|
|
|
return baseUri + url;
|
|
}
|
|
|
|
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;
|
|
}
|