(headings: { level: number; text: string; anchor: string }[])
| 59 | } |
| 60 | |
| 61 | function 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 | |
| 90 | function generateTocMarkdown(items: TocItem[], depth: number = 0): string { |
| 91 | let markdown = "" |
no outgoing calls
no test coverage detected