@@ -2,34 +2,63 @@ import { TSDocParser, type ParserContext, type DocComment, TSDocConfiguration, T
2
2
import * as path from 'path' ;
3
3
import * as fs from 'fs' ;
4
4
import { Formatter } from './formatter' ;
5
+
6
+ /**
7
+ * A class for parsing and rendering TSDoc documentation comments.
8
+ * Provides functionality to parse TypeScript source files and extract their documentation,
9
+ * as well as render the documentation in a formatted way.
10
+ */
5
11
export class DocGen {
6
12
13
+ /**
14
+ * TSDoc tag definition for demo blocks.
15
+ * This custom block tag allows marking code sections as demos in the documentation.
16
+ */
17
+ public static CUSTOM_BLOCK_DEFINITION_DEMO = new TSDocTagDefinition ( {
18
+ tagName : '@demo' ,
19
+ syntaxKind : TSDocTagSyntaxKind . BlockTag
20
+ } ) ;
21
+
22
+ /**
23
+ * Parses a TypeScript source file to extract its documentation.
24
+ *
25
+ * @param doc - The path to the TypeScript source file to parse
26
+ * @returns A ParserContext containing the parsed documentation
27
+ */
7
28
parseDoc ( doc : string ) : ParserContext {
8
29
const inputFilename : string = path . resolve ( doc ) ;
9
30
const inputBuffer : string = fs . readFileSync ( inputFilename ) . toString ( ) ;
10
31
11
32
const customConfiguration : TSDocConfiguration = new TSDocConfiguration ( ) ;
12
33
13
- const customBlockDefinition : TSDocTagDefinition = new TSDocTagDefinition ( {
14
- tagName : '@demo' ,
15
- syntaxKind : TSDocTagSyntaxKind . BlockTag
16
- } ) ;
17
-
18
34
customConfiguration . addTagDefinitions ( [
19
- customBlockDefinition
35
+ DocGen . CUSTOM_BLOCK_DEFINITION_DEMO
20
36
] ) ;
21
-
37
+
22
38
const tsdocParser : TSDocParser = new TSDocParser ( customConfiguration ) ;
23
39
const parserContext : ParserContext = tsdocParser . parseString ( inputBuffer ) ;
24
40
return parserContext ;
25
41
}
26
42
27
- render ( context : ParserContext ) : string {
43
+ /**
44
+ * Renders all documentation nodes from a parsed documentation context.
45
+ *
46
+ * @param context - The parsed documentation context to render
47
+ * @returns A formatted string containing the rendered documentation
48
+ */
49
+ render ( context : ParserContext ) : string {
28
50
const docComment : DocComment = context . docComment ;
29
51
const result = Formatter . renderDocNodes ( docComment . getChildNodes ( ) ) ;
30
52
return result ;
31
53
}
32
54
55
+ /**
56
+ * Renders only the custom documentation blocks from a parsed documentation context.
57
+ * This specifically focuses on rendering blocks marked with custom tags like @demo.
58
+ *
59
+ * @param context - The parsed documentation context to render
60
+ * @returns A formatted string containing only the custom documentation blocks
61
+ */
33
62
renderDocs ( context : ParserContext ) {
34
63
const docComment : DocComment = context . docComment ;
35
64
const result = Formatter . renderDocNodes ( docComment . customBlocks ) ;
0 commit comments