From d9c5754dfc0a0f42d8b11b9562540d17bd9d639b Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Fri, 30 Oct 2020 07:59:49 -0400 Subject: [PATCH] feat(helm): Allow generating markdown docs headers For backwards-compatibility, the generation of markdown headers is only enabled through the --generate-headers flag. Signed-off-by: Marc Khouzam --- cmd/helm/docs.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/cmd/helm/docs.go b/cmd/helm/docs.go index 621b17ca1..df558d3e8 100644 --- a/cmd/helm/docs.go +++ b/cmd/helm/docs.go @@ -16,8 +16,11 @@ limitations under the License. package main import ( + "fmt" "io" + "path" "path/filepath" + "strings" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -38,9 +41,10 @@ It can also generate bash autocompletions. ` type docsOptions struct { - dest string - docTypeString string - topCmd *cobra.Command + dest string + docTypeString string + topCmd *cobra.Command + generateHeaders bool } func newDocsCmd(out io.Writer) *cobra.Command { @@ -62,6 +66,7 @@ func newDocsCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.StringVar(&o.dest, "dir", "./", "directory to which documentation is written") f.StringVar(&o.docTypeString, "type", "markdown", "the type of documentation to generate (markdown, man, bash)") + f.BoolVar(&o.generateHeaders, "generate-headers", false, "generate standard headers for markdown files") return cmd } @@ -69,6 +74,18 @@ func newDocsCmd(out io.Writer) *cobra.Command { func (o *docsOptions) run(out io.Writer) error { switch o.docTypeString { case "markdown", "mdown", "md": + if o.generateHeaders { + standardLinks := func(s string) string { return s } + + hdrFunc := func(filename string) string { + base := filepath.Base(filename) + name := strings.TrimSuffix(base, path.Ext(base)) + title := strings.Title(strings.Replace(name, "_", " ", -1)) + return fmt.Sprintf("---\ntitle: \"%s\"\n---\n\n", title) + } + + return doc.GenMarkdownTreeCustom(o.topCmd, o.dest, hdrFunc, standardLinks) + } return doc.GenMarkdownTree(o.topCmd, o.dest) case "man": manHdr := &doc.GenManHeader{Title: "HELM", Section: "1"}