Get Tree
Returns the full structured navigation tree.
Returns the root-level Node[] array representing the entire folder and page hierarchy as it was built from your content directory. Each element can be a NodePage, NodeFolder, or NodeLabel.
import { getTree } from 'slugtree'
const tree = getTree()Node types
The tree is a union of three node shapes:
type Node = NodePage | NodeFolder | NodeLabel
interface NodePage {
type: 'page'
slug: string[]
href: string
title: string
description?: string
icon?: string
}
interface NodeFolder {
type: 'folder'
title: string
description?: string
slug: string[]
href: string | undefined
children: Node[]
icon?: string
}
interface NodeLabel {
type: 'label'
label: string
}Always check node.type before accessing type-specific fields like children or href.
Usage
Use getTree() in a Server Component to render a navigation sidebar:
import { getTree } from 'slugtree'
export default function Sidebar() {
const tree = getTree()
return (
<nav>
{tree.map((node) => {
if (node.type === 'label') return <p key={node.label}>{node.label}</p>
return (
<a key={node.href} href={node.href}>
{node.title}
</a>
)
})}
</nav>
)
}For reactive access inside Client Components, use useTree() from slugtree/react.