|
@@ -0,0 +1,54 @@
|
|
1
|
+import * as Handlebars from 'handlebars';
|
|
2
|
+import * as Typedoc from 'typedoc';
|
|
3
|
+import * as fs from 'fs';
|
|
4
|
+
|
|
5
|
+const TEMPLATES_DIR = `./src/templates`;
|
|
6
|
+
|
|
7
|
+export class MarkdownCreator {
|
|
8
|
+ constructor(private handlebarsFn: HandlebarsTemplateDelegate<any>) { }
|
|
9
|
+
|
|
10
|
+ public create(reflection: Typedoc.DeclarationReflection) {
|
|
11
|
+ const context = {
|
|
12
|
+ name: reflection.name,
|
|
13
|
+ properties: this.readProperties(reflection),
|
|
14
|
+ methods: this.readMethods(reflection)
|
|
15
|
+ };
|
|
16
|
+ return this.handlebarsFn(context);
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ private readMethods(reflection: Typedoc.DeclarationReflection) {
|
|
20
|
+ const methodReflections = reflection.getChildrenByKind(Typedoc.ReflectionKind.Method);
|
|
21
|
+ methodReflections.sort((a, b) => a.sources[0].line - b.sources[0].line);
|
|
22
|
+ return methodReflections.map((methodReflection) => ({
|
|
23
|
+ name: methodReflection.name,
|
|
24
|
+ arguments: this.readArguments(methodReflection.signatures[0].parameters || []),
|
|
25
|
+ returnType: methodReflection.signatures[0].type.toString(),
|
|
26
|
+ source: `${methodReflection.sources[0].fileName}#${methodReflection.sources[0].line}`,
|
|
27
|
+ comment: methodReflection.signatures[0].comment ? methodReflection.signatures[0].comment.shortText : ''
|
|
28
|
+ }));
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ private readArguments(parameters: Typedoc.ParameterReflection[]) {
|
|
32
|
+ return parameters.map((parameter) => ({
|
|
33
|
+ name: parameter.name,
|
|
34
|
+ type: parameter.type.toString()
|
|
35
|
+ }));
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ private readProperties(reflection: Typedoc.DeclarationReflection) {
|
|
39
|
+ const propsReflections = reflection.getChildrenByKind(Typedoc.ReflectionKind.Property);
|
|
40
|
+ return propsReflections.map((propReflection) => ({
|
|
41
|
+ name: propReflection.name,
|
|
42
|
+ type: propReflection.type.toString()
|
|
43
|
+ }));
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ private setupTemplates() {
|
|
47
|
+ const mainTemplate = fs.readFileSync(`${TEMPLATES_DIR}/main.hbs`).toString();
|
|
48
|
+ const classTemplate = fs.readFileSync(`${TEMPLATES_DIR}/class.hbs`).toString();
|
|
49
|
+ const methodTemplate = fs.readFileSync(`${TEMPLATES_DIR}/method.hbs`).toString();
|
|
50
|
+ Handlebars.registerPartial('class', classTemplate);
|
|
51
|
+ Handlebars.registerPartial('method', methodTemplate);
|
|
52
|
+ return Handlebars.compile(mainTemplate, { strict: true });
|
|
53
|
+ }
|
|
54
|
+}
|