Browse Source

copied all work from typedoc-markdown-creator

Daniel Zlotin 6 years ago
parent
commit
9f79a7ab55

+ 4
- 1
package.json View File

@@ -73,7 +73,10 @@
73 73
     "semver": "5.x.x",
74 74
     "shell-utils": "1.x.x",
75 75
     "tslint": "5.x.x",
76
-    "typescript": "2.7.x"
76
+    "typescript": "2.7.x",
77
+    "typedoc": "0.x.x",
78
+    "handlebars": "4.x.x",
79
+    "ts-node": "5.x.x"
77 80
   },
78 81
   "babel": {
79 82
     "env": {

+ 54
- 0
scripts/gen-doc/MarkdownCreator.ts View File

@@ -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
+}

+ 26
- 0
scripts/gen-doc/ReflectionsReader.ts View File

@@ -0,0 +1,26 @@
1
+import * as Typedoc from 'typedoc';
2
+
3
+const OPTIONS = {
4
+  excludeExternals: true,
5
+  excludePrivate: true,
6
+  includeDeclarations: true,
7
+  module: 'commonjs',
8
+  readme: 'none',
9
+  target: 'ES6'
10
+};
11
+
12
+export class ReflectionsReader {
13
+  private typedocApp: Typedoc.Application;
14
+
15
+  constructor() {
16
+    this.typedocApp = new Typedoc.Application(OPTIONS);
17
+  }
18
+
19
+  public read(modulePath: string): Typedoc.DeclarationReflection {
20
+    const expandedFiles = this.typedocApp.expandInputFiles([modulePath]);
21
+    const projectReflection = this.typedocApp.convert(expandedFiles);
22
+    const externalModule = projectReflection.getChildrenByKind(Typedoc.ReflectionKind.ExternalModule)[0];
23
+    const classReflection = externalModule.getChildrenByKind(Typedoc.ReflectionKind.Class)[0];
24
+    return classReflection;
25
+  }
26
+}

+ 17
- 0
scripts/gen-doc/templates/class.hbs View File

@@ -0,0 +1,17 @@
1
+# {{name}}
2
+
3
+{{#if properties}}
4
+## Properties
5
+
6
+{{#each properties}}
7
+- {{name}} ({{type}})
8
+{{/each}}
9
+
10
+{{/if}}
11
+{{#if methods}}
12
+## Methods
13
+
14
+{{#each methods}}
15
+{{> method}}
16
+{{/each}}
17
+{{/if}}

+ 1
- 0
scripts/gen-doc/templates/main.hbs View File

@@ -0,0 +1 @@
1
+{{> class}}

+ 18
- 0
scripts/gen-doc/templates/method.hbs View File

@@ -0,0 +1,18 @@
1
+### `{{name}}({{#each arguments}}{{name}}: {{type}}{{#unless @last}}, {{/unless}}{{/each}}): {{returnType}}`
2
+
3
+[source]({{source}})
4
+
5
+{{#if comment}}
6
+{{comment}}
7
+
8
+{{/if}}
9
+{{#if arguments}}
10
+#### Arguments
11
+{{#each arguments}}
12
+- {{name}} ({{type}})
13
+{{/each}}
14
+
15
+{{/if}}
16
+#### Returns
17
+- ({{returnType}})
18
+