TSDoc Component Example

This page demonstrates how to use Nextra’s TSDoc component for automatic API documentation generation.

Basic TSDoc Usage

The TSDoc component in Nextra 4.3.0 allows you to automatically generate API documentation from TypeScript code and JSDoc comments.

Example TypeScript Function

Here’s an example of how TSDoc would document a TypeScript function:
/**
 * Adds content to the Cognee knowledge graph
 * 
 * @param content - The content to add (text, file path, or URL)
 * @param options - Optional configuration for the add operation
 * @returns Promise that resolves to the operation result
 * 
 * @example
 * ```typescript
 * const result = await addContent("Hello world", { 
 *   dataset: "main",
 *   format: "text" 
 * });
 * ```
 */
export async function addContent(
  content: string,
  options?: {
    dataset?: string;
    format?: 'text' | 'file' | 'url';
    metadata?: Record<string, any>;
  }
): Promise<{
  success: boolean;
  id: string;
  message: string;
}> {
  // Implementation would go here
  return {
    success: true,
    id: 'content_123',
    message: 'Content added successfully'
  };
}

Benefits of TSDoc

  1. Automatic Documentation - Documentation is generated directly from code
  2. Type Safety - TypeScript types are automatically included
  3. Always Up-to-Date - Documentation stays in sync with code changes
  4. Rich Formatting - Supports JSDoc tags, examples, and parameters
  5. Interactive - Users see exact function signatures and return types

Implementation Notes

To use the TSDoc component effectively:
  1. Install ts-morph: Required dependency for TypeScript parsing
    npm install ts-morph
    
  2. Import TSDoc: From nextra/tsdoc
    import { generateDefinition, TSDoc } from 'nextra/tsdoc'
    
  3. Generate Documentation: Use generateDefinition with your TypeScript code
    <TSDoc
      definition={generateDefinition({
        code: `// Your TypeScript code here`
      })}
    />
    

Next Steps

  • Test TSDoc with actual API route files
  • Configure custom rendering options
  • Add type link mappings for better navigation
  • Integrate with existing API documentation workflow
This approach ensures that API documentation is always accurate and reflects the actual implementation.