Skip to content

Commit

Permalink
feat: add new prop customBaseUrl
Browse files Browse the repository at this point in the history
this will add a new base url, when astro baseUrl is present it will overwrite it
  • Loading branch information
felix-berlin committed Mar 7, 2024
1 parent 137c496 commit 73ab93e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Breadcrumbs.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface BreadcrumbsProps {
ellipsisAriaLabel?: string;
truncated?: boolean;
linkTextFormat?: "lower" | "capitalized" | "sentence";
customBaseUrl?: string;
}
export interface BreadcrumbItem {
Expand All @@ -30,6 +31,7 @@ const {
ellipsisAriaLabel = "Show hidden navigation",
truncated = false,
linkTextFormat,
customBaseUrl,
} = Astro.props as BreadcrumbsProps;
const paths = Astro.url.pathname.split("/").filter((crumb: any) => crumb);
Expand All @@ -43,6 +45,7 @@ const parts = generateCrumbs({
indexText,
hasTrailingSlash,
linkTextFormat,
customBaseUrl,
});
const isLastElement = (index: number, array: any[]) =>
Expand Down
36 changes: 35 additions & 1 deletion src/lib/generateCrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ type GenerateCrumbs = {
indexText: BreadcrumbsProps["indexText"];
hasTrailingSlash: boolean;
linkTextFormat: BreadcrumbsProps["linkTextFormat"];
customBaseUrl: BreadcrumbsProps["customBaseUrl"];
};

const stringStartsAndEndsWithSlash = (str?: string) => {
if (!str) return false;
return str.startsWith("/") && str.endsWith("/");
};

export const generateCrumbs = ({
Expand All @@ -15,6 +21,7 @@ export const generateCrumbs = ({
indexText,
hasTrailingSlash,
linkTextFormat,
customBaseUrl,
}: GenerateCrumbs) => {
/**
* If crumbs are passed, use them.
Expand All @@ -25,16 +32,43 @@ export const generateCrumbs = ({

const parts: Array<BreadcrumbItem> = [];
const baseUrl = import.meta.env.BASE_URL;
console.log("baseUrl: ", baseUrl);
const hasBaseUrl = baseUrl !== "/";
const validCustomBaseUrl = !!customBaseUrl;

// if (hasBaseUrl && validCustomBaseUrl) {
// baseUrl = customBaseUrl;
// hasBaseUrl = true;
// }

console.log("customBaseUrl: ", customBaseUrl);
// console.log("valid customBaseUrl: ", validCustomBaseUrl);

// If both Astro baseUrl and customBaseUrl are present, remove the first item from the paths array
// This is because the first item is the Astro base url and we don't want to duplicate it
if (hasBaseUrl && validCustomBaseUrl) {
paths = paths.slice(1);
}
console.log(paths);

// Set the custom base url as the first item in the paths array
if (validCustomBaseUrl) {
paths.unshift(customBaseUrl);
}
/**
* Loop through the paths and create a breadcrumb item for each.
*/
paths.forEach((text: string, index: number) => {
/**
* generateHref will create the href out of the paths array.
* Example: ["path1", "path2"] => /path1/path2
*/
const generateHref = `/${paths.slice(0, index + 1).join("/")}`;

const addTrailingSlash = hasTrailingSlash
? `${generateHref}/`
: generateHref;

const finalHref = addTrailingSlash;

// strip out any file extensions
Expand All @@ -54,7 +88,7 @@ export const generateCrumbs = ({
* If there is NO base URL, the index item is missing.
* Add it to the start of the array.
*/
if (!hasBaseUrl) {
if (!hasBaseUrl && !validCustomBaseUrl) {
parts.unshift({
text: indexText!,
href: baseUrl,
Expand Down

0 comments on commit 73ab93e

Please sign in to comment.