processCommand processes the command documentation, filtering out unnecessary sections and formatting headers.
(content string, depth int)
| 94 | |
| 95 | // processCommand processes the command documentation, filtering out unnecessary sections and formatting headers. |
| 96 | func processCommand(content string, depth int) string { |
| 97 | lines := strings.Split(content, "\n") |
| 98 | // nolint: prealloc |
| 99 | var filtered []string |
| 100 | inSeeAlso := false |
| 101 | |
| 102 | for i, line := range lines { |
| 103 | trimmed := strings.TrimSpace(line) |
| 104 | |
| 105 | // Skip "SEE ALSO" section and everything after |
| 106 | if strings.Contains(trimmed, "### SEE ALSO") { |
| 107 | inSeeAlso = true |
| 108 | continue |
| 109 | } |
| 110 | if inSeeAlso { |
| 111 | continue |
| 112 | } |
| 113 | |
| 114 | // Strip all leading '#' characters and spaces from any header line |
| 115 | cleanLine := strings.TrimLeft(trimmed, "# ") |
| 116 | |
| 117 | // Detect underline-style headers (--- or ===) |
| 118 | if i > 0 && isUnderlineHeader(line) { |
| 119 | // Remove previous line (header text) |
| 120 | if len(filtered) > 0 { |
| 121 | prev := filtered[len(filtered)-1] |
| 122 | filtered = filtered[:len(filtered)-1] |
| 123 | // Add heading with dynamic depth + 1 (subsection) |
| 124 | filtered = append(filtered, fmt.Sprintf("%s %s", |
| 125 | strings.Repeat("#", depth+3), // depth+3 for sub-sections |
| 126 | prev)) |
| 127 | } |
| 128 | continue |
| 129 | } |
| 130 | |
| 131 | // For the first line (command title), apply heading with dynamic depth |
| 132 | if i == 0 && cleanLine != "" { |
| 133 | filtered = append(filtered, fmt.Sprintf("%s %s", |
| 134 | strings.Repeat("#", depth+2), // depth+2 to start from H2 for root |
| 135 | cleanLine)) |
| 136 | continue |
| 137 | } |
| 138 | |
| 139 | // Replace $HOME environment variable references |
| 140 | if home := os.Getenv("HOME"); home != "" { |
| 141 | cleanLine = strings.ReplaceAll(cleanLine, home, "$HOME") |
| 142 | } |
| 143 | |
| 144 | filtered = append(filtered, cleanLine) |
| 145 | } |
| 146 | |
| 147 | return strings.Join(filtered, "\n") + "\n\n" |
| 148 | } |
| 149 | |
| 150 | // isUnderlineHeader checks if a line is an underline-style header (--- or ===). |
| 151 | func isUnderlineHeader(line string) bool { |
no test coverage detected