From 6a2c0dc0dc81fcf22c513c5bc54b614ce5fb02f6 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 8 Jul 2025 23:52:43 +0300 Subject: feat: remove unnecessary indentation from code snippets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add removeCommonIndentation function to strip common leading whitespace - Code snippets now start at column 0 when possible - Preserves relative indentation within the code - Works with both spaces and tabs 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/showcase/code_extractor.go | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/internal/showcase/code_extractor.go b/internal/showcase/code_extractor.go index 518e590..d43131b 100644 --- a/internal/showcase/code_extractor.go +++ b/internal/showcase/code_extractor.go @@ -484,5 +484,84 @@ func stripComments(code string) string { result = result[:len(result)-1] } + // Remove unnecessary indentation + result = removeCommonIndentation(result) + return strings.Join(result, "\n") +} + +// removeCommonIndentation removes common leading whitespace from all lines +func removeCommonIndentation(lines []string) []string { + if len(lines) == 0 { + return lines + } + + // Find the common prefix of whitespace + var commonPrefix string + firstNonEmpty := -1 + + // Find first non-empty line to use as reference + for i, line := range lines { + if strings.TrimSpace(line) != "" { + firstNonEmpty = i + break + } + } + + if firstNonEmpty == -1 { + return lines + } + + // Get the whitespace prefix of the first non-empty line + firstLine := lines[firstNonEmpty] + for i, ch := range firstLine { + if ch != ' ' && ch != '\t' { + commonPrefix = firstLine[:i] + break + } + } + + // If the first line has no indentation, return as-is + if commonPrefix == "" { + return lines + } + + // Find the actual common prefix among all non-empty lines + for _, line := range lines { + if strings.TrimSpace(line) == "" { + continue + } + + // Reduce commonPrefix to what this line shares + for i := 0; i < len(commonPrefix); i++ { + if i >= len(line) || line[i] != commonPrefix[i] { + commonPrefix = commonPrefix[:i] + break + } + } + + if commonPrefix == "" { + break + } + } + + // If no common prefix found, return as-is + if commonPrefix == "" { + return lines + } + + // Remove common prefix from all lines + result := make([]string, len(lines)) + prefixLen := len(commonPrefix) + for i, line := range lines { + if strings.TrimSpace(line) == "" { + result[i] = "" + } else if strings.HasPrefix(line, commonPrefix) { + result[i] = line[prefixLen:] + } else { + result[i] = line + } + } + + return result } \ No newline at end of file -- cgit v1.2.3