-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_markdown.go
72 lines (56 loc) · 1.28 KB
/
gen_markdown.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
const frontMatterTemplate = `---
title: "%s"
noHeading: true
---
`
func genMarkdownCmd() *cobra.Command {
var outputDirectory string
argsFunc := func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("expected 1 argument, got %d arguments", len(args))
}
outputDirectory = args[0]
return nil
}
cmdFunc := func(cmd *cobra.Command, args []string) error {
err := os.MkdirAll(outputDirectory, 0755)
if err != nil {
return fmt.Errorf("create output directory %s for markdown pages: %w", outputDirectory, err)
}
err = doc.GenMarkdownTreeCustom(
New(),
outputDirectory,
func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
title := strings.Replace(base, "_", " ", -1)
return fmt.Sprintf(frontMatterTemplate, title)
},
func(name string) string {
return name
},
)
if err != nil {
return fmt.Errorf("generate markdown tree: %w", err)
}
return nil
}
cmd := &cobra.Command{
Use: "gen-markdown",
Short: "Generate markdown documentation",
Hidden: true,
Args: argsFunc,
RunE: cmdFunc,
}
return cmd
}