11/*
2- Copyright 2025 The KubeLB Authors.
2+ Copyright 2026 The KubeLB Authors.
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -19,37 +19,59 @@ package cmd
1919import (
2020 "fmt"
2121 "os"
22+ "path/filepath"
23+ "strings"
24+ "time"
2225
2326 "github.com/spf13/cobra"
2427 "github.com/spf13/cobra/doc"
2528)
2629
2730func docsCmd () * cobra.Command {
2831 var outputDir string
32+ var hugoFormat bool
2933
3034 cmd := & cobra.Command {
3135 Use : "docs" ,
3236 Short : "Generate markdown documentation for all commands" ,
3337 Long : `Generate markdown documentation for all CLI commands and their parameters.
34- This creates individual markdown files for each command with complete usage information.` ,
35- RunE : func (root * cobra.Command , _ []string ) error {
38+ This creates individual markdown files for each command with complete usage information.
39+
40+ Use --hugo flag to generate Hugo-compatible documentation with front matter
41+ for integration with static site generators.` ,
42+ RunE : func (cmd * cobra.Command , _ []string ) error {
3643 if err := os .MkdirAll (outputDir , 0755 ); err != nil {
3744 return fmt .Errorf ("failed to create output directory: %w" , err )
3845 }
39- prepender := empty
40- linkHandler := identity
41- root .DisableAutoGenTag = true
42- err := doc .GenMarkdownTreeCustom (root , outputDir , prepender , linkHandler )
46+
47+ var prepender func (string ) string
48+ var linkHandler func (string ) string
49+
50+ if hugoFormat {
51+ prepender = hugoPrepender
52+ linkHandler = hugoLinkHandler
53+ } else {
54+ prepender = empty
55+ linkHandler = identity
56+ }
57+
58+ rootCmd := cmd .Root ()
59+ rootCmd .DisableAutoGenTag = true
60+ err := doc .GenMarkdownTreeCustom (rootCmd , outputDir , prepender , linkHandler )
4361 if err != nil {
4462 return fmt .Errorf ("failed to generate documentation: %w" , err )
4563 }
4664
4765 fmt .Printf ("Documentation generated successfully in: %s\n " , outputDir )
66+ if hugoFormat {
67+ fmt .Println ("Hugo format enabled - files include front matter and Hugo-compatible links" )
68+ }
4869 return nil
4970 },
5071 }
5172
5273 cmd .Flags ().StringVarP (& outputDir , "output" , "o" , "./docs" , "Output directory for generated documentation" )
74+ cmd .Flags ().BoolVar (& hugoFormat , "hugo" , false , "Generate Hugo-compatible docs with front matter" )
5375 return cmd
5476}
5577
@@ -60,3 +82,30 @@ func identity(s string) string {
6082func empty (_ string ) string {
6183 return ""
6284}
85+
86+ // hugoPrepender adds Hugo front matter to generated docs
87+ func hugoPrepender (filename string ) string {
88+ // Extract command name from filename (e.g., "kubelb_tunnel_connect.md" -> "kubelb tunnel connect")
89+ name := strings .TrimSuffix (filepath .Base (filename ), ".md" )
90+ title := strings .ReplaceAll (name , "_" , " " )
91+
92+ // Calculate weight based on command depth (more underscores = higher weight = appears later)
93+ depth := strings .Count (name , "_" )
94+ weight := 10 + (depth * 20 )
95+
96+ date := time .Now ().Format ("2006-01-02T00:00:00+00:00" )
97+
98+ return fmt .Sprintf (`+++
99+ title = "%s"
100+ date = %s
101+ weight = %d
102+ +++
103+
104+ ` , title , date , weight )
105+ }
106+
107+ // hugoLinkHandler converts links to Hugo-compatible format
108+ // Changes: "kubelb_tunnel.md" -> "../kubelb_tunnel" (relative path, no .md extension)
109+ func hugoLinkHandler (name string ) string {
110+ return "../" + strings .TrimSuffix (name , ".md" )
111+ }
0 commit comments