-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for base url and slashes
- Loading branch information
David Boyne
authored and
David Boyne
committed
Jul 11, 2024
1 parent
56d3a35
commit bdd88d4
Showing
1 changed file
with
16 additions
and
17 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,27 +1,26 @@ | ||
import config from '@eventcatalog'; | ||
|
||
const cleanUrl = (url: string) => { | ||
return url.replace(/\/+/g, '/'); | ||
} | ||
return url.replace(/\/+/g, '/'); | ||
}; | ||
|
||
// Custom URL builder as Astro does not support this stuff out the box | ||
export const buildUrl = (url: string, ignoreTrailingSlash = false) => { | ||
// Should a trailingSlash be added to urls? | ||
const trailingSlash = config.trailingSlash || false; | ||
// Should a trailingSlash be added to urls? | ||
const trailingSlash = config.trailingSlash || false; | ||
|
||
let newUrl = url; | ||
let newUrl = url; | ||
|
||
// If the base URL is not the root, we need to append it | ||
if(import.meta.env.BASE_URL !== '/') { | ||
newUrl = `${import.meta.env.BASE_URL}${url}`; | ||
} | ||
// If the base URL is not the root, we need to append it | ||
if (import.meta.env.BASE_URL !== '/') { | ||
newUrl = `${import.meta.env.BASE_URL}${url}`; | ||
} | ||
|
||
// Should we add a trailing slash to the url? | ||
if(trailingSlash && !ignoreTrailingSlash){ | ||
if(url.endsWith('/')) return newUrl; | ||
return cleanUrl(`${newUrl}/`); | ||
} | ||
// Should we add a trailing slash to the url? | ||
if (trailingSlash && !ignoreTrailingSlash) { | ||
if (url.endsWith('/')) return newUrl; | ||
return cleanUrl(`${newUrl}/`); | ||
} | ||
|
||
|
||
return cleanUrl(newUrl); | ||
} | ||
return cleanUrl(newUrl); | ||
}; |