import Link from "next/link"; import { listDocuments } from "@mcpedia/core"; import { SECTIONS } from "@mcpedia/config/sections"; export const dynamic = "force-dynamic"; interface DocMeta { slug: string; title: string; section: string; tags: string[]; updatedAt: string; path: string; } /** * Build a hierarchical folder tree from a flat list of documents. * Each node is either a folder (has children) or a leaf doc. */ interface TreeNode { name: string; slug: string; doc?: DocMeta; children: TreeNode[]; docCount: number; // total docs in subtree } function buildFolderTree(docs: DocMeta[], section: string): TreeNode[] { const root: TreeNode[] = []; for (const doc of docs.filter((d) => d.section === section)) { const rel = doc.path.startsWith(`${section}/`) ? doc.path.slice(section.length + 1) : doc.path; const cleanPath = rel.replace(/\.md$/, ""); const parts = cleanPath.split("/"); let current = root; let currentPath = section; for (let i = 0; i < parts.length; i++) { const part = parts[i]; const isLeaf = i === parts.length - 1; currentPath = `${currentPath}/${part}`; let node = current.find((n) => n.name === part); if (!node) { node = { name: part, slug: currentPath, children: [], docCount: 0, }; current.push(node); } if (isLeaf) { node.doc = doc; node.docCount = 1; } current = node.children; } } // Recursively compute docCount for folder nodes function countDocs(node: TreeNode): number { if (node.doc) return 1; return node.children.reduce((sum, child) => sum + countDocs(child), 0); } for (const node of root) { node.docCount = countDocs(node); } return root; } function renderTree(nodes: TreeNode[], pathname: string) { return ( ); } export default async function SectionIndexPage({ params, }: { params: Promise<{ section: string }>; }) { const { section } = await params; const sectionInfo = SECTIONS.find((s) => s.id === section); if (!sectionInfo) { return (

Unknown section

); } const allDocs = await listDocuments(); const sectionDocs = allDocs.filter((d) => d.section === section); const tree = buildFolderTree(allDocs, section); return (
{sectionInfo.icon}

{sectionInfo.label}

{sectionDocs.length} document{sectionDocs.length !== 1 ? "s" : ""}

{sectionInfo.desc}

{tree.length > 0 ? (

Folder structure

{renderTree(tree, section)}
) : (

No documents found in this section.

)} {sectionDocs.length > 0 && (

Recently updated

{[...sectionDocs] .sort( (a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(), ) .map((d) => ( {d.title} ))}
)}
); }