diff --git a/doc/md_docs.go b/doc/md_docs.go index 6eae7ccfb..ff9ee20fa 100644 --- a/doc/md_docs.go +++ b/doc/md_docs.go @@ -62,10 +62,10 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) name := cmd.CommandPath() buf.WriteString("## " + name + "\n\n") - buf.WriteString(cmd.Short + "\n\n") + buf.WriteString(escapeAngleBrackets(cmd.Short) + "\n\n") if len(cmd.Long) > 0 { buf.WriteString("### Synopsis\n\n") - buf.WriteString(cmd.Long + "\n\n") + buf.WriteString(escapeAngleBrackets(cmd.Long) + "\n\n") } if cmd.Runnable() { @@ -87,7 +87,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) pname := parent.CommandPath() link := pname + markdownExtension link = strings.ReplaceAll(link, " ", "_") - fmt.Fprintf(buf, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short) + fmt.Fprintf(buf, "* [%s](%s)\t - %s\n", pname, linkHandler(link), escapeAngleBrackets(parent.Short)) cmd.VisitParents(func(c *cobra.Command) { if c.DisableAutoGenTag { cmd.DisableAutoGenTag = c.DisableAutoGenTag @@ -105,7 +105,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) cname := name + " " + child.Name() link := cname + markdownExtension link = strings.ReplaceAll(link, " ", "_") - fmt.Fprintf(buf, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short) + fmt.Fprintf(buf, "* [%s](%s)\t - %s\n", cname, linkHandler(link), escapeAngleBrackets(child.Short)) } buf.WriteString("\n") } @@ -116,6 +116,10 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) return err } +func escapeAngleBrackets(s string) string { + return strings.NewReplacer("<", "\\<", ">", "\\>").Replace(s) +} + // GenMarkdownTree will generate a markdown page for this command and all // descendants in the directory given. The header may be nil. // This function may not work correctly if your command names have `-` in them. diff --git a/doc/md_docs_test.go b/doc/md_docs_test.go index 1bf13aba4..599ff5eb2 100644 --- a/doc/md_docs_test.go +++ b/doc/md_docs_test.go @@ -91,6 +91,32 @@ func TestGenMdNoTag(t *testing.T) { checkStringOmits(t, output, "Auto generated") } +func TestGenMdEscapesAngleBrackets(t *testing.T) { + cmd := &cobra.Command{ + Use: "export ", + Short: "Export to ", + Long: "Export the database to .", + Run: emptyRun, + } + child := &cobra.Command{ + Use: "status", + Short: "Inspect ", + Run: emptyRun, + } + cmd.AddCommand(child) + + buf := new(bytes.Buffer) + if err := GenMarkdown(cmd, buf); err != nil { + t.Fatal(err) + } + output := buf.String() + + checkStringContains(t, output, "Export to \\") + checkStringContains(t, output, "Export the database to \\.") + checkStringContains(t, output, "Inspect \\") + checkStringContains(t, output, "```\nexport [flags]\n```") +} + func TestGenMdTree(t *testing.T) { c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"} tmpdir, err := os.MkdirTemp("", "test-gen-md-tree")