Skip to content

Add breadcrumbs #1451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions src/components/Breadcrumbs.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
import Section from "@ui/Section.astro";

export interface Props {
linksData: any;
currentPath: string;
homeLabel?: string;
homePath?: string;
separator?: string;
}

const {
linksData,
currentPath,
homeLabel = "Home",
homePath = "/",
separator = "/"
} = Astro.props;

interface BreadcrumbItem {
name: string;
path: string;
isActive: boolean;
}

function normalizePath(path: string): string {
console.log(path)
if (!path) return ""
if (path === "/") return "/";
return path.replace(/\/+$/, "");
}

const normalizedCurrentPath = normalizePath(currentPath);

// Recursive function to build breadcrumb trail
function findBreadcrumbTrail(items: any[], currentPath: string, parentName?: string): BreadcrumbItem[] {
for (const item of items) {
const itemPath = normalizePath(item.path);

if (itemPath === currentPath) {
const breadcrumb: BreadcrumbItem = {
name: item.name,
path: item.path,
isActive: true
};

if (parentName) {
return [
{ name: parentName, path: '', isActive: false },
breadcrumb
];
}

return [breadcrumb];
}

if (item.items) {
const subTrail = findBreadcrumbTrail(item.items, currentPath, item.name);
if (subTrail.length > 0) {
return subTrail;
}
}
}

return [];
}

// Build the breadcrumb trail
const breadcrumbs: BreadcrumbItem[] = [];

breadcrumbs.push({
name: homeLabel,
path: homePath,
isActive: normalizedCurrentPath === normalizePath(homePath)
});

if (normalizedCurrentPath !== normalizePath(homePath) && linksData?.header) {
const trail = findBreadcrumbTrail(linksData.header, normalizedCurrentPath);
breadcrumbs.push(...trail);
}

if (breadcrumbs.length === 1 && normalizedCurrentPath !== normalizePath(homePath)) {
const pathSegments = normalizedCurrentPath.split('/').filter(Boolean);
const lastSegment = pathSegments[pathSegments.length - 1];

if (lastSegment) {
breadcrumbs.push({
name: lastSegment.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase()),
path: currentPath,
isActive: true
});
}
}
---

<Section>
{ currentPath !== homePath &&
<nav aria-label="Breadcrumb" class="px-4">
<ol class="flex flex-wrap items-center text-sm list-none m-0 p-0">
{breadcrumbs.map((crumb, index) => (
<li class="flex items-center">
{crumb.isActive ? (
<span class="text-gray-900 font-medium px-2 py-1" aria-current="page">
{crumb.name}
</span>
) : crumb.path ? (
<a href={crumb.path} class="underline hover:text-button-hover px-2 py-1 rounded transition-colors duration-200 hover:underline">
{crumb.name}
</a>
) : (
<span class="text-gray-500 italic px-2 py-1">
{crumb.name}
</span>
)}
{index < breadcrumbs.length - 1 && (
<span class="mx-2 text-gray-400 select-none" aria-hidden="true">
{separator}
</span>
)}
</li>
))}
</ol>
</nav>
}
</Section>
13 changes: 13 additions & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ import BaseHead from "@components/BaseHead.astro";
import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro";
import Offline from "@components/Offline.astro";
import Breadcrumbs from '@components/Breadcrumbs.astro';

import linksData from '@src/data/links.json';

import "@fortawesome/fontawesome-free/css/all.min.css";
import "@styles/tailwind.css";
import "@styles/global.css";
import "@styles/search.css";


export interface Props {
title: string;
description: string;
}

const currentPath = Astro.url.pathname;

const { title, description } = Astro.props;

if (!title || !description) {
Expand All @@ -37,6 +43,13 @@ const externalDomain = new URL(Astro.site || "").hostname;

<main class="main pt-28" role="main">
<Offline />
<Breadcrumbs
linksData={linksData}
currentPath={currentPath}
homeLabel="EuroPython 2025"
homePath="/"
separator="›"
/>
<slot />
</main>

Expand Down