33package cli
44
55import (
6+ "bytes"
67 "os"
78 "path"
89 "strings"
@@ -13,37 +14,55 @@ import (
1314
1415var isDocsBuild = true
1516
16- func generateDocs (cmd * cobra.Command , filename string , title string ) {
17+ func generateDocs (cmd * cobra.Command , title string ) {
1718 cmd .DisableAutoGenTag = true
1819
1920 // create docs directory
2021 _ = os .Mkdir ("./docs" , 0755 )
2122
22- err := doc .GenMarkdownTreeCustom (cmd , "./docs" ,
23- func (_ string ) string {
24- return `---
25- title: ` + title + `
26- ---
27-
28- `
29- },
30- func (name string ) string {
31- // err := doc.GenMarkdownCustom(cmd, out, func(name string) string {
32- base := strings .TrimSuffix (name , path .Ext (name ))
33- return "/cli/" + strings .ToLower (base ) + "/"
34- })
23+ out := new (bytes.Buffer )
24+
25+ err := doc .GenMarkdownCustom (cmd , out , func (name string ) string {
26+ // err := doc.GenMarkdownCustom(cmd, out, func(name string) string {
27+ base := strings .TrimSuffix (name , path .Ext (name ))
28+ return "/cli/" + strings .ToLower (base ) + "/"
29+ })
3530 if err != nil {
3631 panic (err )
3732 }
3833
34+ // Define the text to be replaced and the replacement text
35+ oldText := []byte ("## " + title )
36+ newText := []byte ("---\n title: " + title + "\n ---" )
37+
38+ // Perform the replacement on the buffer's content
39+ updatedContent := bytes .Replace (out .Bytes (), oldText , newText , 1 )
40+
41+ // Reset the buffer and write the updated content back to it
42+ out .Reset ()
43+ out .Write (updatedContent )
44+
45+ // write markdown to file
46+ file , err := os .Create ("./docs/" + strings .ReplaceAll (title , " " , "_" ) + ".md" )
47+ if err != nil {
48+ panic (err )
49+ }
50+
51+ _ , err = file .Write (out .Bytes ())
52+ if err != nil {
53+ panic (err )
54+ }
55+
56+ defer file .Close ()
57+
3958 // if command has subcommands, generate markdown for each subcommand
4059 if cmd .HasSubCommands () {
4160 for _ , c := range cmd .Commands () {
4261 // if c.Use starts with "help", skip it
43- if strings . HasPrefix ( c . Use , "help" ) {
62+ if c . Name () == "help" {
4463 continue
4564 }
46- generateDocs (c , filename , title + " " + c .Use )
65+ generateDocs (c , title + " " + c .Name () )
4766 }
4867 }
4968}
0 commit comments