aidxnCC/lib/docs/loader.ts

48 lines
No EOL
1.4 KiB
TypeScript

import { readFileSync } from 'fs'
import { join } from 'path'
import { parseTypeDocJSON } from './parser'
import type { TypeDocRoot, DocSection } from './types'
/**
* Loads and parses TypeDoc-generated API documentation from JSON file.
*
* @returns Array of documentation sections organized by category
*
* @remarks
* This function:
* 1. Reads the TypeDoc-generated JSON file from `public/docs/api.json`
* 2. Parses the raw TypeDoc data into structured documentation sections
* 3. Returns an empty array if the file is missing or invalid
*
* The TypeDoc JSON file should be generated by running:
* ```bash
* typedoc --options typedoc.json
* ```
*
* @example
* ```ts
* import { loadDocumentation } from '@/lib/docs/loader'
*
* // In a server component
* export default function DocsPage() {
* const sections = loadDocumentation()
* return <DocsList sections={sections} />
* }
* ```
*
* @throws Does not throw - errors are logged and an empty array is returned
*
* @category Documentation
* @public
*/
export function loadDocumentation(): DocSection[] {
try {
const filePath = join(process.cwd(), 'public/docs/api.json')
const fileContents = readFileSync(filePath, 'utf8')
const typeDocData: TypeDocRoot = JSON.parse(fileContents)
return parseTypeDocJSON(typeDocData)
} catch (error) {
console.error('Failed to load TypeDoc JSON:', error)
return []
}
}