...
1
16
17 package main
18
19 import (
20 "os"
21 "path/filepath"
22 "strings"
23
24 "github.com/spf13/cobra"
25 )
26
27
28 func MarkdownPostProcessing(cmd *cobra.Command, dir string, processor func(string) string) error {
29 for _, c := range cmd.Commands() {
30 if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
31 continue
32 }
33 if err := MarkdownPostProcessing(c, dir, processor); err != nil {
34 return err
35 }
36 }
37
38 basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
39 filename := filepath.Join(dir, basename)
40
41 markdownBytes, err := os.ReadFile(filename)
42 if err != nil {
43 return err
44 }
45
46 processedMarkDown := processor(string(markdownBytes))
47
48 return os.WriteFile(filename, []byte(processedMarkDown), 0644)
49 }
50
51
52
53
54 func cleanupForInclude(md string) string {
55 lines := strings.Split(md, "\n")
56
57 cleanMd := ""
58 for i, line := range lines {
59 if i == 0 {
60 continue
61 }
62 if line == "### SEE ALSO" {
63 break
64 }
65
66 cleanMd += line
67 if i < len(lines)-1 {
68 cleanMd += "\n"
69 }
70 }
71
72 return cleanMd
73 }
74
View as plain text