react-native-navigation的迁移库

MarkdownCreator.ts 2.1KB

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