Skip to content

Commit

Permalink
chore: Add option to generate frontmatter to gendocs.
Browse files Browse the repository at this point in the history
This is needed for latter inclusion in knative.dev. Related to knative#639.
  • Loading branch information
rhuss committed Feb 19, 2020
1 parent b183aa2 commit 210fa2e
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion hack/generate-docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ package main

import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/spf13/cobra/doc"
"knative.dev/client/pkg/kn/core"
Expand All @@ -29,9 +33,45 @@ func main() {
if len(os.Args) > 1 {
dir = os.Args[1]
}
err := doc.GenMarkdownTree(rootCmd, dir+"/docs/cmd/")
var withFrontMatter bool
var err error
if len(os.Args) > 2 {
withFrontMatter, err = strconv.ParseBool(os.Args[2])
if err != nil {
log.Panicf("Invalid argument %s, has to be boolean to switch on/off generation of frontmatter", os.Args[2])
}
}
prependFunc := emptyString
if withFrontMatter {
prependFunc = addFrontMatter
}
err = doc.GenMarkdownTreeCustom(rootCmd, dir+"/docs/cmd/", prependFunc, identity)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func emptyString(filename string) string {
return ""
}

func addFrontMatter(fileName string) string {
// Convert to a title
title := filepath.Base(fileName)
title = title[0:len(title) - len(filepath.Ext(title))]
title = strings.ReplaceAll(title, "_", " ")
ret := `
---
title: "%s"
#linkTitle: "OPTIONAL_ALTERNATE_NAV_TITLE"
weight: 5
type: "docs"
---
`
return fmt.Sprintf(ret, title)
}

func identity(s string) string {
return s
}

0 comments on commit 210fa2e

Please sign in to comment.