Skip to content

Commit

Permalink
fix: leading slash missing
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-berlin committed Sep 3, 2023
1 parent ff3066c commit 729c3a5
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/Breadcrumbs.astro
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const {
baseUrl,
} = Astro.props as BreadcrumbsProps;
const hasBaseUrl = baseUrl?.length;
/**
* Log a warning message to the console.
*
Expand Down Expand Up @@ -81,19 +83,15 @@ const endsWithSlash = (url: string | undefined): boolean => {
* If the base URL is not valid, return a relative URL.
*
*/
const getBaseUrl = (): string => {
if (endsWithSlash(baseUrl)) {
const getBaseUrl = (): string | undefined => {
if (endsWithSlash(baseUrl) && hasBaseUrl) {
coloredWarnLog("Base URL should not end with a slash. Falling back to relative URL.");
return "/"
}
if (!isValidUrl(baseUrl)) {
if (!isValidUrl(baseUrl) && hasBaseUrl) {
coloredWarnLog("Base URL is not valid. Falling back to relative URL.");
return "/";
}
return baseUrl ? baseUrl : "/";
return baseUrl;
};
let parts: Array<BreadcrumbItem> = [];
Expand All @@ -111,15 +109,16 @@ if (crumbs.length === 0) {
parts = [
{
text: indexText,
href: getBaseUrl(),
href: getBaseUrl() ?? "/",
},
];
/**
* Loop through the paths and create a breadcrumb item for each.
*/
paths.forEach((text: string, index: number) => {
const href = `${getBaseUrl()}/${paths.slice(0, index + 1).join("/")}`;
const generateHref = `/${paths.slice(0, index + 1).join("/")}`;
const finalHref = getBaseUrl() ?? "" + generateHref
// strip out any file extensions
const matches = text.match(/^(.+?)(\.[a-z0-9]+)?$/i);
Expand All @@ -132,7 +131,7 @@ if (crumbs.length === 0) {
...parts,
{
text: text.replace(/[-_]/g, " "),
href,
href: finalHref,
},
];
});
Expand Down

0 comments on commit 729c3a5

Please sign in to comment.