mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
55 lines
1.3 KiB
Plaintext
55 lines
1.3 KiB
Plaintext
---
|
|
// Remove current url path and remove trailing slash if exists
|
|
const currentUrlPath = Astro.url.pathname.replace(/\/+$/, "");
|
|
|
|
// Get url array from path
|
|
// eg: /tags/tailwindcss => ['tags', 'tailwindcss']
|
|
const breadcrumbList = currentUrlPath.split("/").slice(1);
|
|
|
|
// if breadcrumb is Home > Posts > 1 <etc>
|
|
// replace Posts with Posts (page number)
|
|
breadcrumbList[0] === "posts" &&
|
|
breadcrumbList.splice(0, 2, `Posts (page ${breadcrumbList[1] || 1})`);
|
|
---
|
|
|
|
<nav class="breadcrumb" aria-label="breadcrumb">
|
|
<ul>
|
|
<li>
|
|
<a href="/">Home</a>
|
|
<span aria-hidden="true">></span>
|
|
</li>
|
|
{
|
|
breadcrumbList.map((breadcrumb, index) =>
|
|
index + 1 === breadcrumbList.length ? (
|
|
<li>
|
|
<span class={`${index > 0 ? "lowercase" : ""}`} aria-current="page">
|
|
{breadcrumb}
|
|
</span>
|
|
</li>
|
|
) : (
|
|
<li>
|
|
<a href={`/${breadcrumb}`}>{breadcrumb}</a>
|
|
<span aria-hidden="true">></span>
|
|
</li>
|
|
)
|
|
)
|
|
}
|
|
</ul>
|
|
</nav>
|
|
|
|
<style>
|
|
.breadcrumb {
|
|
@apply max-w-3xl mx-auto px-4 w-full mt-8 mb-1;
|
|
}
|
|
.breadcrumb ul li {
|
|
@apply inline;
|
|
}
|
|
.breadcrumb ul li a,
|
|
.breadcrumb ul li span {
|
|
@apply opacity-70 capitalize;
|
|
}
|
|
.breadcrumb ul li:not(:last-child) a {
|
|
@apply hover:opacity-100;
|
|
}
|
|
</style>
|