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

Function generateTableOfContents

src/scripts/build-docs-markdown.ts:23–50  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

21}
22
23function generateTableOfContents(content: string): string {
24 // Extract headings from the markdown content
25 const headingRegex = /^(#{2,6})\s+(.+)$/gm
26 const headings: { level: number; text: string; anchor: string }[] = []
27 let match
28
29 while ((match = headingRegex.exec(content)) !== null) {
30 const level = match[1].length
31 const text = match[2].trim()
32 const anchor = generateAnchor(text)
33
34 headings.push({ level, text, anchor })
35 }
36
37 if (headings.length === 0) {
38 return ""
39 }
40
41 // Build nested TOC structure
42 const tocItems = buildTocStructure(headings)
43
44 // Generate TOC markdown
45 let toc = "## Table of Contents\n\n"
46 toc += generateTocMarkdown(tocItems)
47 toc += "\n"
48
49 return toc
50}
51
52function generateAnchor(text: string): string {
53 return text

Callers 1

convertMdxToMarkdownFunction · 0.85

Calls 3

generateAnchorFunction · 0.85
buildTocStructureFunction · 0.85
generateTocMarkdownFunction · 0.85

Tested by

no test coverage detected