Skip to content

Commit

Permalink
add support for base url and slashes
Browse files Browse the repository at this point in the history
  • 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.
33 changes: 16 additions & 17 deletions src/utils/url-builder.ts
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);
};

0 comments on commit bdd88d4

Please sign in to comment.