1
1
import fs from "node:fs" ;
2
2
import path from "node:path" ;
3
3
import util from "node:util" ;
4
- import fg from "fast-glob" ;
5
4
5
+ import fg from "fast-glob" ;
6
6
import dox from "dox" ;
7
+ import prettier from "prettier" ;
7
8
import { ReflectionKind , type JSONOutput } from "typedoc" ;
8
9
import ts from "typescript" ;
9
10
@@ -61,7 +62,7 @@ type SimplifiedComment = {
61
62
codeLink : string ;
62
63
modes : Mode [ ] ;
63
64
summary : string ;
64
- reference : string ;
65
+ reference ? : string ;
65
66
example ?: string ;
66
67
signature ?: string ;
67
68
params : {
@@ -150,11 +151,15 @@ if (filePaths.length === 0) {
150
151
process . exit ( 1 ) ;
151
152
}
152
153
154
+ run ( ) ;
155
+
153
156
// Generate markdown documentation for all matching files
154
- filePaths . forEach ( ( filePath ) => {
155
- console . log ( `\nProcessing file: ${ filePath } ` ) ;
156
- generateMarkdownDocs ( filePath , apiFilter , outputDir , args . write ) ;
157
- } ) ;
157
+ async function run ( ) {
158
+ for ( let filePath of filePaths ) {
159
+ console . log ( `\nProcessing file: ${ filePath } ` ) ;
160
+ await generateMarkdownDocs ( filePath , apiFilter , outputDir , args . write ) ;
161
+ }
162
+ }
158
163
159
164
function buildRepoDocsLinks ( outputDir : string ) : Map < string , string > {
160
165
const lookup = new Map < string , string > ( ) ;
@@ -318,13 +323,13 @@ function getDeclarationDescription(child: JSONOutput.DeclarationReflection) {
318
323
. join ( "" ) ;
319
324
}
320
325
321
- function generateMarkdownDocs (
326
+ async function generateMarkdownDocs (
322
327
filepath : string ,
323
328
apiFilter : string [ ] | null ,
324
329
outputDir ?: string ,
325
330
writeFiles ?: boolean ,
326
331
) {
327
- let simplifiedComments = parseDocComments ( filepath , apiFilter ) ;
332
+ let simplifiedComments = await parseDocComments ( filepath , apiFilter ) ;
328
333
simplifiedComments . forEach ( ( comment ) => {
329
334
// Generate markdown content for each public function
330
335
let markdownContent = generateMarkdownForComment ( comment ) ;
@@ -495,17 +500,16 @@ function generateMarkdownForComment(comment: SimplifiedComment): string {
495
500
return markdown ;
496
501
}
497
502
498
- function parseDocComments ( filepath : string , apiFilter : string [ ] | null ) {
503
+ async function parseDocComments ( filepath : string , apiFilter : string [ ] | null ) {
499
504
let code = fs . readFileSync ( filepath ) . toString ( ) ;
500
505
let comments = dox . parseComments ( code , { raw : true } ) as ParsedComment [ ] ;
506
+ let filteredComments = comments . filter (
507
+ ( c ) =>
508
+ c . tags . some ( ( t ) => t . type === "public" ) &&
509
+ ( ! apiFilter || apiFilter . includes ( getApiName ( c ) ) ) ,
510
+ ) ;
501
511
502
- return comments
503
- . filter (
504
- ( c ) =>
505
- c . tags . some ( ( t ) => t . type === "public" ) &&
506
- ( ! apiFilter || apiFilter . includes ( getApiName ( c ) ) ) ,
507
- )
508
- . map ( ( c ) => simplifyComment ( c , filepath ) ) ;
512
+ return Promise . all ( filteredComments . map ( ( c ) => simplifyComment ( c , filepath ) ) ) ;
509
513
}
510
514
511
515
function getApiName ( comment : ParsedComment ) : string {
@@ -529,10 +533,10 @@ function getApiName(comment: ParsedComment): string {
529
533
throw new Error ( `Could not determine API name:\n${ comment . code } \n` ) ;
530
534
}
531
535
532
- function simplifyComment (
536
+ async function simplifyComment (
533
537
comment : ParsedComment ,
534
538
filepath : string ,
535
- ) : SimplifiedComment {
539
+ ) : Promise < SimplifiedComment > {
536
540
let name = getApiName ( comment ) ;
537
541
let unstable = name . startsWith ( "unstable_" ) ;
538
542
@@ -570,7 +574,7 @@ function simplifyComment(
570
574
) ;
571
575
}
572
576
573
- let signature = getSignature ( comment . code ) ;
577
+ let signature = await getSignature ( comment . code ) ;
574
578
575
579
let params : SimplifiedComment [ "params" ] = [ ] ;
576
580
comment . tags . forEach ( ( tag ) => {
@@ -642,7 +646,7 @@ function isParamTag(tag: Tag): tag is ParamTag {
642
646
643
647
// Parse the TypeScript code into an AST so we can remove the function body
644
648
// and just grab the signature
645
- function getSignature ( code : string ) {
649
+ async function getSignature ( code : string ) {
646
650
const ast = ts . createSourceFile ( "example.ts" , code , ts . ScriptTarget . Latest ) ;
647
651
if ( ast . statements . length === 0 ) {
648
652
throw new Error ( `Expected one or more statements: ${ code } ` ) ;
@@ -663,7 +667,9 @@ function getSignature(code: string) {
663
667
. createPrinter ( { newLine : ts . NewLineKind . LineFeed } )
664
668
. printNode ( ts . EmitHint . Unspecified , modifiedFunction , ast ) ;
665
669
666
- return newCode . replace ( "{ }" , "" ) . trim ( ) ;
670
+ let formatted = await prettier . format ( newCode , { parser : "typescript" } ) ;
671
+
672
+ return formatted . replace ( "{}" , "" ) . trim ( ) ;
667
673
}
668
674
669
675
// TODO: Handle variable statements for forwardRef components
0 commit comments