polen
Version:
A framework for delightful GraphQL developer portals
45 lines • 1.71 kB
JavaScript
import { Str } from '@wollybeard/kit';
/**
* Generate navbar items from a list of pages
*/
export const createNavbar = (pages) => {
const navbarItems = [];
// Group pages by their top-level directory/segment
const topLevelGroups = new Map();
pages.forEach(page => {
// Skip hidden pages
if (page.metadata.hidden)
return;
const firstSegment = page.route.logical.path[0];
if (firstSegment) {
if (!topLevelGroups.has(firstSegment)) {
topLevelGroups.set(firstSegment, []);
}
topLevelGroups.get(firstSegment).push(page);
}
});
// Add each top-level group to navbar
topLevelGroups.forEach((pages, segment) => {
// For directories, check if there's an index page
const indexPage = pages.find(p => p.route.logical.path.length === 1
&& p.route.file.path.relative.name === `index`);
// For single non-index files at top level
const singlePage = pages.length === 1 && pages[0].route.logical.path.length === 1
&& pages[0].route.file.path.relative.name !== `index`;
// Include in navbar if:
// 1. It's a directory with an index page, OR
// 2. It's a single top-level page (not index)
if (indexPage || singlePage) {
const title = Str.titlizeSlug(segment);
const pathExp = `/${segment}`;
navbarItems.push({
pathExp,
title,
});
}
});
// Sort navbar items alphabetically for consistency
navbarItems.sort((a, b) => a.title.localeCompare(b.title));
return navbarItems;
};
//# sourceMappingURL=navbar.js.map