Open1
Astroのパンくずリスト実装
途中まで
frontmatter
---
import pages from "@data/pages.json";
// スラッシュの数を数える関数
function countSlashes(str) {
return (str.match(/\//g) || []).length;
}
// 並べ替えを実行
pages.sort((a, b) => countSlashes(a.href) - countSlashes(b.href));
console.log(Astro.url.pathname);
console.log(pages);
function generateBreadcrumbs(url, data) {
const breadcrumbs = [];
const allData = [];
data.forEach((item) => {
if (url.startsWith(item.href)) {
breadcrumbs.push(item.title.ja);
allData.push(item);
}
});
return { breadcrumbs, allData };
}
const breadcrumbs = generateBreadcrumbs(Astro.url.pathname, pages);
console.log(breadcrumbs.breadcrumbs.join(" > "));
console.log(breadcrumbs.allData);
---