-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocs.go
60 lines (51 loc) · 1.05 KB
/
docs.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
//go:build docs
package main
import (
"fmt"
"os"
"path"
"strings"
log "github.com/rs/zerolog"
"github.com/spf13/cobra/doc"
"github.com/maxgio92/yap/cmd"
"github.com/maxgio92/yap/internal/commands/options"
)
const (
cmdline = "yap"
docsDir = "docs"
fileTemplate = `---
title: %s
---
`
)
var (
filePrepender = func(filename string) string {
title := strings.TrimPrefix(
strings.TrimSuffix(strings.ReplaceAll(filename, "_", " "), ".md"),
fmt.Sprintf("%s/", docsDir),
)
return fmt.Sprintf(fileTemplate, title)
}
linkHandler = func(filename string) string {
if filename == cmdline+".md" {
return "_index.md"
}
return filename
}
)
func main() {
if err := doc.GenMarkdownTreeCustom(
cmd.NewRootCmd(options.NewCommonOptions(options.WithLogger(log.New(os.Stderr).Level(log.InfoLevel)))),
docsDir,
filePrepender,
linkHandler,
); err != nil {
fmt.Println(err)
os.Exit(1)
}
err := os.Rename(path.Join(docsDir, cmdline+".md"), path.Join(docsDir, "_index.md"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}