Skip to content

Commit 5c2438c

Browse files
docs: Fix ANSI escape sequences in pack CLI leaking into documentation (#891) (#940)
The get_pack_commands.go generator uses cobra/doc.GenMarkdownTreeCustom to produce markdown from the pack command tree. Some pack commands include ANSI color codes in their description strings (e.g. \x1b[94m...\x1b[0m) to improve terminal --help readability. These codes were passing through the generator unmodified and appearing as noise in the rendered documentation. Added a post-generation step that walks the output directory and strips SGR ANSI escape sequences (\x1b\[[0-9;]*m) from all generated .md files. This is the appropriate place to handle the conversion — the generator is the boundary between terminal output and markdown, and stripping color codes there keeps the fix localized without affecting the upstream CLI's terminal experience. Signed-off-by: Lorenzo Campanella <enzocampanella98@gmail.com>
1 parent e2aee84 commit 5c2438c

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

tools/get_pack_commands.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import (
77
"os"
88
"path"
99
"path/filepath"
10+
"regexp"
1011
"strings"
1112

1213
"github.com/buildpacks/pack/cmd"
1314
"github.com/buildpacks/pack/pkg/logging"
1415
"github.com/spf13/cobra/doc"
1516
)
1617

18+
var ansiEscape = regexp.MustCompile(`\x1b\[[0-9;]*m`)
19+
1720
const (
1821
cliDir = "/docs/for-platform-operators/how-to/integrate-ci/pack/cli/"
1922
outputPath = "../content" + cliDir
@@ -64,6 +67,27 @@ func main() {
6467
if err != nil {
6568
log.Fatal(err)
6669
}
70+
71+
if err := stripANSIFromDir(outputPath); err != nil {
72+
log.Fatal(err)
73+
}
74+
}
75+
76+
func stripANSIFromDir(dir string) error {
77+
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
78+
if err != nil || info.IsDir() || filepath.Ext(path) != ".md" {
79+
return err
80+
}
81+
data, err := os.ReadFile(path)
82+
if err != nil {
83+
return err
84+
}
85+
cleaned := ansiEscape.ReplaceAll(data, nil)
86+
if len(cleaned) == len(data) {
87+
return nil
88+
}
89+
return os.WriteFile(path, cleaned, info.Mode())
90+
})
6791
}
6892

6993
type packLogger struct {

0 commit comments

Comments
 (0)