-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,82 @@ | ||
export const urlPrefix: string = Deno.env.get("SB_URL_PREFIX") ?? | ||
(globalThis.silverBulletConfig | ||
? globalThis.silverBulletConfig.urlPrefix | ||
: null) ?? | ||
""; | ||
const getUrlPrefix = () : string => { | ||
const prefix : string = Deno.env.get("SB_URL_PREFIX") ?? | ||
(globalThis.silverBulletConfig ? | ||
globalThis.silverBulletConfig.urlPrefix | ||
: null) ?? ''; | ||
if (prefix === '') { | ||
return ''; | ||
} | ||
|
||
export const toRealUrl = <T extends (string | URL)>(url: T): T => { | ||
if (typeof url === "string") { | ||
const stringUrl = url as string; | ||
if (stringUrl.startsWith("http://") || stringUrl.startsWith("https://")) { | ||
const parsedUrl = new URL(stringUrl); | ||
parsedUrl.pathname = urlPrefix + parsedUrl.pathname; | ||
//console.log("Converted ", url, parsedUrl.href) | ||
return String(parsedUrl.href) as T; | ||
} else { | ||
if (!stringUrl.startsWith("/")) { | ||
console.log("Don't know how to deal with relative path: ", url); | ||
} | ||
//console.log("Converted ", url, urlPrefix + stringUrl) | ||
return (urlPrefix + stringUrl) as T; | ||
} | ||
} else if (url.protocol === "http:" || url.protocol === "https:") { | ||
const parsedUrl = new URL(url as URL); | ||
parsedUrl.pathname = urlPrefix + parsedUrl.pathname; | ||
//console.log("Converted ", url, parsedUrl) | ||
return parsedUrl as T; | ||
} else { | ||
return url; | ||
} | ||
let result = prefix; | ||
if (!prefix.startsWith('/')) { | ||
result = '/' + result; | ||
} | ||
if (prefix.endsWith('/')) { | ||
result = result.replace(/\/*$/, ''); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export const toInternalUrl = (url: string) => { | ||
if (url.startsWith("http://") || url.startsWith("https://")) { | ||
export const urlPrefix: string = getUrlPrefix(); | ||
|
||
const toRealUrlObject = (url : URL) : URL => { | ||
const parsedUrl = new URL(url); | ||
if (parsedUrl.pathname.startsWith(urlPrefix)) { | ||
parsedUrl.pathname = parsedUrl.pathname.substr(urlPrefix.length); | ||
return parsedUrl.href; | ||
if (typeof location !== 'undefined' && parsedUrl.origin == location.origin) { | ||
if (parsedUrl.pathname.startsWith(urlPrefix)) { | ||
//console.trace("Path starts with prefix already: ", url); | ||
} | ||
|
||
parsedUrl.pathname = urlPrefix + parsedUrl.pathname; | ||
//console.trace("Converted full URL ", url, parsedUrl.href) | ||
return parsedUrl; | ||
} else { | ||
console.log("Don't know how to deal with non-prefix: ", url); | ||
return url; | ||
} | ||
} else if (url.startsWith(urlPrefix)) { | ||
return url.substr(urlPrefix.length); | ||
} else { | ||
console.log("Don't know how to deal with non-prefix: ", url); | ||
return url; | ||
} | ||
//console.trace("Don't know how to deal with cross origin path: ", url); | ||
return url; | ||
} | ||
} | ||
|
||
export const toRealUrl = <T extends (string | URL)>(url : T) : T => { | ||
if (typeof url === 'string') { | ||
const stringUrl = url as string; | ||
if (stringUrl.startsWith('http://') || stringUrl.startsWith('https://')) { | ||
return toRealUrlObject(new URL(stringUrl)).href as T; | ||
} | ||
else if (!stringUrl.startsWith('/')) { | ||
//console.trace("Don't know how to deal with relative path: ", url); | ||
return url; | ||
} else { | ||
if (url.startsWith(urlPrefix)) { | ||
//console.trace("Path starts with prefix already: ", url); | ||
} | ||
//console.trace("Converted absolute path ", url, urlPrefix + stringUrl) | ||
return (urlPrefix + stringUrl) as T; | ||
} | ||
} | ||
else if (url.protocol === 'http:' || url.protocol === 'https:') { | ||
return toRealUrlObject(url) as T; | ||
} | ||
else { | ||
return url; | ||
} | ||
}; | ||
|
||
export const toInternalUrl = (url : string) => { | ||
if (url.startsWith('http://') || url.startsWith('https://')) { | ||
const parsedUrl = new URL(url); | ||
if (parsedUrl.pathname.startsWith(urlPrefix)) { | ||
parsedUrl.pathname = parsedUrl.pathname.substr(urlPrefix.length); | ||
return parsedUrl.href; | ||
} | ||
else { | ||
//console.trace("Don't know how to deal with non-prefix: ", url); | ||
return url; | ||
} | ||
} else if (url.startsWith(urlPrefix)) { | ||
return url.substr(urlPrefix.length); | ||
} | ||
else { | ||
//console.trace("Don't know how to deal with non-prefix: ", url); | ||
return url; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters