2025-09-05 14:59:21 +08:00

67 lines
1.4 KiB
JavaScript

const navBottomLink = (link) => {
if (!link.children) {
return link._path;
}
for (const child of link?.children || []) {
const result = navBottomLink(child);
if (result) {
return result;
}
}
};
const navDirFromPath = (path, tree) => {
for (const file of tree) {
if (file._path === path && !file._id) {
return file.children;
}
if (file.children) {
const result = navDirFromPath(path, file.children);
if (result) {
return result;
}
}
}
};
const navPageFromPath = (path, tree) => {
for (const file of tree) {
if (file._path === path) {
return file;
}
if (file.children) {
const result = navPageFromPath(path, file.children);
if (result) {
return result;
}
}
}
};
const navKeyFromPath = (path, key, tree) => {
let value;
const goDeep = (path2, tree2) => {
for (const file of tree2) {
if (path2 !== "/" && file._path === "/") {
continue;
}
if (path2?.startsWith(file._path) && file[key]) {
value = file[key];
}
if (file._path === path2) {
return;
}
if (file.children) {
goDeep(path2, file.children);
}
}
};
goDeep(path, tree);
return value;
};
export const useContentHelpers = () => {
return {
navBottomLink,
navDirFromPath,
navPageFromPath,
navKeyFromPath
};
};