Skip to content

Commit e43ef58

Browse files
committed
Escape text in the command markdown documentation
Some characters have a special meaning, like the "#" for headings. By default, lines will be concatenated. Add explicit line breaks. Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent f47b60e commit e43ef58

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

cmd/limactl/gendoc.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,26 @@ and $LIMA_WORKDIR.
101101
return doc.GenManTree(cmd.Root(), header, dir)
102102
}
103103

104+
func escapeMarkdown(text string) string {
105+
lines := strings.Split(text, "\n")
106+
for i := range lines {
107+
// Need to escape backticks first, before adding more
108+
for _, c := range strings.Split("\\`*_[]()#+-.|", "") {
109+
lines[i] = strings.ReplaceAll(lines[i], c, "\\"+c)
110+
}
111+
if i < len(lines)-1 {
112+
if lines[i] != "" && lines[i+1] != "" {
113+
lines[i] += " " // line break
114+
}
115+
}
116+
}
117+
return strings.Join(lines, "\n")
118+
}
119+
104120
func genDocsy(cmd *cobra.Command, dir string) error {
121+
for _, c := range cmd.Root().Commands() {
122+
c.Long = escapeMarkdown(c.Long)
123+
}
105124
return doc.GenMarkdownTreeCustom(cmd.Root(), dir, func(s string) string {
106125
// Replace limactl_completion_bash to completion bash for docsy title
107126
name := filepath.Base(s)

0 commit comments

Comments
 (0)