GenMDDoc generates markdown documentation for the given root command.
(root *cobra.Command)
| 68 | |
| 69 | // GenMDDoc generates markdown documentation for the given root command. |
| 70 | func GenMDDoc(root *cobra.Command) *cobra.Command { |
| 71 | cmd := &cobra.Command{ |
| 72 | Use: "gen-md-doc", |
| 73 | Hidden: true, |
| 74 | Short: fmt.Sprintf("Generate markdown documentation for %s", root.Name()), |
| 75 | RunE: func(cmd *cobra.Command, args []string) error { |
| 76 | dir, _ := cmd.Flags().GetString("out") |
| 77 | if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 78 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 79 | return err |
| 80 | } |
| 81 | } |
| 82 | disableAutoGenTag(root) |
| 83 | prepender := func(filename string) string { |
| 84 | name := filepath.Base(filename) |
| 85 | base := strings.TrimSuffix(name, path.Ext(name)) |
| 86 | title := strings.Replace(base, "_", " ", -1) |
| 87 | fmt.Printf(`Write "%s" to %s`+"\n", title, filename) |
| 88 | return fmt.Sprintf(MDDocFrontmatterTemplate, title, base) |
| 89 | } |
| 90 | |
| 91 | linkHandler := func(name string) string { |
| 92 | base := strings.TrimSuffix(name, path.Ext(name)) |
| 93 | return fmt.Sprintf(`{{< relref "%s" >}}`, strings.ToLower(base)) |
| 94 | } |
| 95 | return doc.GenMarkdownTreeCustom(root, dir, prepender, linkHandler) |
| 96 | }, |
| 97 | } |
| 98 | cmd.Flags().StringP("out", "o", "doc", "output directory") |
| 99 | return cmd |
| 100 | } |
| 101 | |
| 102 | type command struct { |
| 103 | Short string `json:"short,omitempty"` |