@eslamdevui/ui
Version:
A UI Library for Modern Web Apps, powered by Vue & Tailwind CSS.
78 lines (77 loc) • 2.35 kB
JavaScript
export function mapContentNavigationItem(item, options, currentDepth = 0) {
const navMap = {
[options?.labelAttribute || "title"]: "label",
path: "to"
};
const link = Object.keys(item).reduce(
(link2, key) => {
if (item[key]) {
const mappedKey = navMap[key] || key;
link2[mappedKey] = item[key];
}
return link2;
},
{}
);
const shouldRecurse = typeof options?.deep === "undefined" || currentDepth < options.deep;
if (shouldRecurse && Array.isArray(item.children)) {
link.children = item.children.map(
(child) => mapContentNavigationItem(child, options, currentDepth + 1)
);
} else {
link.children = [];
}
return link;
}
export function mapContentNavigation(navigation, options) {
return navigation.map((item) => mapContentNavigationItem(item, options));
}
export function findPageHeadline(navigation, path, options) {
if (!navigation?.length || !path) {
return;
}
for (const link of navigation) {
if (options?.indexAsChild) {
if (link.children) {
const headline = findPageHeadline(link.children, path, options);
if (headline) {
return headline;
}
for (const child of link.children) {
if (child.path === path) {
return link.title;
}
}
}
} else {
if (link.children) {
for (const child of link.children) {
const isIndex = child.stem?.endsWith("/index");
if (child.path === path && !isIndex) {
return link.title;
}
}
const headline = findPageHeadline(link.children, path, options);
if (headline) {
return headline;
}
}
}
}
}
export function findPageBreadcrumb(navigation, path, options) {
if (!navigation?.length || !path) {
return [];
}
return navigation.reduce((breadcrumb, link) => {
if (path && (path + "/").startsWith(link.path + "/")) {
if (path !== link.path || options?.current || options?.indexAsChild && link.children) {
breadcrumb.push(link);
}
if (link.children) {
breadcrumb.push(...findPageBreadcrumb(link.children.filter((c) => c.path !== link.path || c.path === path && options?.current && options?.indexAsChild), path, options));
}
}
return breadcrumb;
}, []);
}