@tpmjs/tools-toc-generate
Generate a table of contents from markdown headings. Parses # ## ### style headings and creates a formatted TOC with anchor links. Useful for adding navigation to long markdown documents.
Test @tpmjs/tools-toc-generate (tocGenerateTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-toc-generatepnpm add @tpmjs/tools-toc-generateyarn add @tpmjs/tools-toc-generatebun add @tpmjs/tools-toc-generatedeno add npm:@tpmjs/tools-toc-generateimport { tocGenerateTool } from '@tpmjs/tools-toc-generate';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { tocGenerateTool } from '@tpmjs/tools-toc-generate';
const result = await generateText({
model: openai('gpt-4o'),
tools: { tocGenerateTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
markdownstringThe markdown content to generate table of contents from
maxDepthnumberMaximum heading depth to include (1-6). Default is 3.
Schema extracted: 1/1/2026, 8:18:26 AM
Generate table of contents from markdown headings.
#, ##, ###, etc.)npm install @tpmjs/tools-toc-generate
import { tocGenerateTool } from '@tpmjs/tools-toc-generate'; const result = await tocGenerateTool.execute({ markdown: ` # Introduction ## Overview ## Getting Started ### Installation ### Configuration # Advanced Topics ## API Reference `, maxDepth: 3, // Optional, default is 3 }); console.log(result.toc); // Output: // - [Introduction](#introduction) // - [Overview](#overview) // - [Getting Started](#getting-started) // - [Installation](#installation) // - [Configuration](#configuration) // - [Advanced Topics](#advanced-topics) // - [API Reference](#api-reference) console.log(result.headings); // [ // { level: 1, text: 'Introduction', slug: 'introduction', line: 2 }, // { level: 2, text: 'Overview', slug: 'overview', line: 3 }, // ... // ] console.log(result.depth); // { min: 1, max: 3, included: 7 }
tocGenerateTool.execute(input)markdown (string, required): The markdown content to parsemaxDepth (number, optional): Maximum heading depth to include (1-6, default: 3)Returns a TocResult object:
interface TocResult { toc: string; // Formatted TOC markdown headings: Heading[]; // Array of parsed headings depth: { min: number; // Minimum heading level found max: number; // Maximum heading level found included: number; // Number of headings included in TOC }; } interface Heading { level: number; // Heading level (1-6) text: string; // Heading text slug: string; // URL-safe slug line: number; // Line number in source }
const result = await tocGenerateTool.execute({ markdown: '# Title\n## Section 1\n## Section 2', }); console.log(result.toc); // - [Title](#title) // - [Section 1](#section-1) // - [Section 2](#section-2)
const result = await tocGenerateTool.execute({ markdown: '# Title\n## Section\n### Subsection\n#### Details', maxDepth: 2, // Only include h1 and h2 }); console.log(result.toc); // - [Title](#title) // - [Section](#section)
const result = await tocGenerateTool.execute({ markdown: '# Introduction {#intro}\n## Overview {#overview}', }); console.log(result.toc); // - [Introduction](#intro) // - [Overview](#overview)
MIT
Downloads/month
0
Quality Score