react-native-navigation的迁移库

MarkdownWriter.ts 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as Handlebars from 'handlebars';
  2. import * as fs from 'fs';
  3. import { ClassContext } from './ClassParser';
  4. export class MarkdownWriter {
  5. private classFn;
  6. private menuFn;
  7. constructor(private templatesDir: string, private outputDir: string) {
  8. this.classFn = this.setupClassHandlebars();
  9. this.menuFn = this.setupMenuHandlebars();
  10. }
  11. public writeClasses(classContexts: ClassContext[]) {
  12. classContexts.forEach((c) => {
  13. const classMarkdown = this.classFn(c);
  14. fs.writeFileSync(`${this.outputDir}/${c.name}.md`, classMarkdown, { encoding: 'utf8' });
  15. });
  16. }
  17. public writeMenu(classContexts: ClassContext[]) {
  18. const menuMarkdown = this.menuFn({
  19. classes: classContexts.map((c) => ({ name: c.name, path: `/api/${c.name}` }))
  20. });
  21. fs.writeFileSync(`${this.outputDir}/_sidebar.md`, menuMarkdown, { encoding: 'utf8' });
  22. fs.writeFileSync(`${this.outputDir}/README.md`, menuMarkdown, { encoding: 'utf8' });
  23. }
  24. private setupClassHandlebars() {
  25. const classTemplate = fs.readFileSync(`${this.templatesDir}/class.hbs`).toString();
  26. const methodTemplate = fs.readFileSync(`${this.templatesDir}/method.hbs`).toString();
  27. const propertyTemplate = fs.readFileSync(`${this.templatesDir}/property.hbs`).toString();
  28. Handlebars.registerPartial('class', classTemplate);
  29. Handlebars.registerPartial('method', methodTemplate);
  30. Handlebars.registerPartial('property', propertyTemplate);
  31. return Handlebars.compile('{{> class}}', { strict: true, noEscape: true });
  32. }
  33. private setupMenuHandlebars() {
  34. const template = fs.readFileSync(`${this.templatesDir}/menu.hbs`).toString();
  35. return Handlebars.compile(template, { strict: true, noEscape: true });
  36. }
  37. }