@@ -8,22 +8,31 @@ import {
88 renderMarkdownToHast ,
99 type DocEntry ,
1010} from 'fumadocs-typescript' ;
11+ import { getProgram } from 'fumadocs-typescript' ;
12+ import type { Program } from 'typescript' ;
13+ import { z } from 'zod' ;
1114import { createElement , expressionToAttribute } from './utils' ;
1215import type { DocGenerator } from './remark-docgen' ;
1316
1417export type TypescriptGeneratorOptions = GenerateDocumentationOptions ;
1518
16- export interface TypescriptGeneratorInput {
17- file : string ;
18- name : string ;
19+ export type TypescriptGeneratorInput = z . output <
20+ typeof typescriptGeneratorSchema
21+ > ;
22+
23+ export const typescriptGeneratorSchema = z . object ( {
24+ file : z . string ( { description : 'Target TypeScript file name' } ) ,
25+ name : z . string ( { description : 'Exported type name' } ) ,
1926
2027 /**
2128 * Component name which accepts the `type` property
2229 *
2330 * @defaultValue 'TypeTable'
2431 */
25- component ?: string ;
26- }
32+ component : z
33+ . string ( { description : 'Component name which accepts the `type` property' } )
34+ . default ( 'TypeTable' ) ,
35+ } ) ;
2736
2837export interface VirtualTypeTableProps {
2938 type : Record <
@@ -47,8 +56,16 @@ export interface VirtualTypeTableProps {
4756 * @param options - configuration
4857 */
4958export function typescriptGenerator (
50- options ? : TypescriptGeneratorOptions ,
59+ options : TypescriptGeneratorOptions = { } ,
5160) : DocGenerator {
61+ let program : Program | undefined ;
62+
63+ function loadProgram ( ) : Program {
64+ return options . config && 'program' in options . config
65+ ? options . config . program
66+ : getProgram ( options . config ) ;
67+ }
68+
5269 function mapProperty ( entry : DocEntry ) : Property {
5370 const value = valueToEstree ( {
5471 type : entry . type ,
@@ -91,15 +108,23 @@ export function typescriptGenerator(
91108
92109 return {
93110 name : 'typescript' ,
111+ onFile ( ) {
112+ if ( process . env . NODE_ENV === 'development' || ! program ) {
113+ program = loadProgram ( ) ;
114+ }
115+ } ,
94116 run ( input , ctx ) {
95117 const {
96118 file,
97119 name,
98120 component = 'TypeTable' ,
99- } = input as TypescriptGeneratorInput ;
121+ } = typescriptGeneratorSchema . parse ( input ) ;
100122 const dest = path . resolve ( ctx . cwd , file ) ;
101123
102- const doc = generateDocumentation ( dest , name , options ) ;
124+ const doc = generateDocumentation ( dest , name , {
125+ ...options ,
126+ config : { program : program ?? loadProgram ( ) } ,
127+ } ) ;
103128 if ( ! doc ) throw new Error ( `Failed to find type ${ name } in ${ dest } ` ) ;
104129
105130 return createElement ( component , [
0 commit comments