MCPcopy Create free account
hub / github.com/danielpetho/fancy / buildTocStructure

Function buildTocStructure

src/scripts/build-docs-markdown.ts:61–88  ·  view source on GitHub ↗
(headings: { level: number; text: string; anchor: string }[])

Source from the content-addressed store, hash-verified

59}
60
61function buildTocStructure(headings: { level: number; text: string; anchor: string }[]): TocItem[] {
62 const root: TocItem[] = []
63 const stack: TocItem[] = []
64
65 for (const heading of headings) {
66 const item: TocItem = {
67 text: heading.text,
68 anchor: heading.anchor,
69 level: heading.level,
70 children: []
71 }
72
73 // Find the appropriate parent
74 while (stack.length > 0 && stack[stack.length - 1].level >= heading.level) {
75 stack.pop()
76 }
77
78 if (stack.length === 0) {
79 root.push(item)
80 } else {
81 stack[stack.length - 1].children.push(item)
82 }
83
84 stack.push(item)
85 }
86
87 return root
88}
89
90function generateTocMarkdown(items: TocItem[], depth: number = 0): string {
91 let markdown = ""

Callers 1

generateTableOfContentsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected