Core: Support findSiblings() page tree utility

This commit is contained in:
Fuma Nama
2025-12-30 18:38:06 +08:00
parent 25f1f0c6ff
commit 590d36a6c1
5 changed files with 28 additions and 9 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"fumadocs-core": patch
---
Support `findSiblings()` page tree utility
+20 -6
View File
@@ -65,22 +65,30 @@ export function getPageTreeRoots(
}
/**
* Get other page tree nodes that lives under the same parent
* Get other item nodes that lives under the same parent.
*/
export function getPageTreePeers(
treeOrTrees: PageTree.Root | Record<string, PageTree.Root>,
url: string,
): PageTree.Item[] {
return findSiblings(treeOrTrees, url).filter((item) => item.type === 'page');
}
/**
* Get other tree nodes that lives under the same parent.
*/
export function findSiblings(
treeOrTrees: PageTree.Root | Record<string, PageTree.Root>,
url: string,
): PageTree.Node[] {
// Check if it's a single tree or multiple trees (i18n)
if ('children' in treeOrTrees) {
// Single tree case
const tree = treeOrTrees as PageTree.Root;
const parent = findParentFromTree(tree, url);
const parent = findParent(tree, url);
if (!parent) return [];
return parent.children.filter(
(item) => item.type === 'page' && item.url !== url,
) as PageTree.Item[];
return parent.children;
}
// Multiple trees case
@@ -92,7 +100,7 @@ export function getPageTreePeers(
return [];
}
function findParentFromTree(
export function findParent(
from: PageTree.Root | PageTree.Folder,
url: string,
): PageTree.Root | PageTree.Folder | undefined {
@@ -157,6 +165,12 @@ export function findPath(
const VisitBreak = Symbol('VisitBreak');
/**
* Perform a depth-first search on page tree visiting every node.
*
* @param root - the root of page tree to visit.
* @param visitor - function to receive nodes, return `skip` to skip the children of current node, `break` to stop the search entirely.
*/
export function visit<Root extends PageTree.Node | PageTree.Root>(
root: Root,
visitor: <T extends PageTree.Node | PageTree.Root>(
@@ -292,7 +292,7 @@ function createPageTreeBuilderUtils(ctx: PageTreeBuilderContext) {
const folderName = basename(folderPath);
return pathToName(group.exec(folderName)?.[1] ?? folderName);
})(),
icon: metadata.icon,
icon: metadata.icon ?? index?.icon,
root: metadata.root,
defaultOpen: metadata.defaultOpen,
description: metadata.description,
@@ -15,7 +15,7 @@ export function lucideIconsPlugin(
return iconPlugin((icon = defaultIcon) => {
if (icon === undefined) return;
const Icon = icons[icon as keyof typeof icons];
if (!icon) {
if (!Icon) {
console.warn(`[lucide-icons-plugin] Unknown icon detected: ${icon}.`);
return;
}
+1 -1
View File
@@ -12,7 +12,7 @@ interface Output {
data: unknown;
}
const regex = /^---\r?\n(.+?)\r?\n---\r?\n/s;
const regex = /^---\r?\n(.+?)\r?\n---\r?\n?/s;
/**
* parse frontmatter, it supports only yaml format