2026-02-26 14:24:48 +08:00
|
|
|
|
import { buildFileTree, extractHeadings, extractSection, type TocNode, type FileNode } from "./toc";
|
2026-03-13 15:50:50 +08:00
|
|
|
|
import {getIndexedData, getPathsByExtension} from "./file-index";
|
2026-02-26 14:24:48 +08:00
|
|
|
|
|
|
|
|
|
|
export { TocNode, FileNode, extractHeadings, buildFileTree, extractSection };
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 生成目录树(文件树 + 标题结构)
|
2026-03-13 15:50:50 +08:00
|
|
|
|
* 仅处理 .md 文件
|
2026-02-26 14:24:48 +08:00
|
|
|
|
*/
|
2026-02-26 14:30:09 +08:00
|
|
|
|
export async function generateToc(): Promise<{ fileTree: FileNode[]; pathHeadings: Record<string, TocNode[]> }> {
|
2026-03-13 15:50:50 +08:00
|
|
|
|
const mdPaths = await getPathsByExtension("md");
|
|
|
|
|
|
const fileTree = buildFileTree(mdPaths);
|
2026-02-26 14:24:48 +08:00
|
|
|
|
const pathHeadings: Record<string, TocNode[]> = {};
|
|
|
|
|
|
|
2026-03-13 15:50:50 +08:00
|
|
|
|
for (const path of mdPaths) {
|
|
|
|
|
|
const content = await getIndexedData(path);
|
2026-02-26 14:24:48 +08:00
|
|
|
|
if (content) {
|
|
|
|
|
|
pathHeadings[path] = extractHeadings(content);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return { fileTree, pathHeadings };
|
|
|
|
|
|
}
|