From df623bfdcecad9fd4c0f5229ce9af5e8416a9558 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 8 Jul 2025 23:30:47 +0300 Subject: feat: add syntax highlighting labels and strip comments from code snippets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add language labels to code blocks for syntax highlighting (e.g., ```go, ```sh) - Map languages to source-highlight compatible identifiers - Strip comments from code snippets to show more actual code - Comments no longer count towards the ~10 line target - Support for multi-line and single-line comment removal - Remove inline comments where appropriate 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/showcase/code_extractor.go | 80 +++++++++++++++++++++++++++++++++++-- internal/showcase/showcase.go | 59 ++++++++++++++++++++++++++- 2 files changed, 134 insertions(+), 5 deletions(-) (limited to 'internal') diff --git a/internal/showcase/code_extractor.go b/internal/showcase/code_extractor.go index e8ba0d3..b51761c 100644 --- a/internal/showcase/code_extractor.go +++ b/internal/showcase/code_extractor.go @@ -165,13 +165,14 @@ func extractSnippetFromFile(filePath string, minLines, maxLines int) (string, er // Try to find the smallest complete function bestFunction := findSmallestCompleteFunction(lines) if bestFunction != "" { - return bestFunction, nil + return stripComments(bestFunction), nil } // If no complete function found, try to find a complete function/method functionStart, functionEnd := findCompleteFunctionOrMethod(lines, minLines, maxLines*2) // Allow larger functions if functionStart >= 0 && functionEnd >= 0 { - return strings.Join(lines[functionStart:functionEnd+1], "\n"), nil + snippet := strings.Join(lines[functionStart:functionEnd+1], "\n") + return stripComments(snippet), nil } // Fallback to finding an interesting start with at least minLines @@ -181,7 +182,8 @@ func extractSnippetFromFile(filePath string, minLines, maxLines int) (string, er if endLine > totalLines { endLine = totalLines } - return strings.Join(lines[interestingStart:endLine], "\n"), nil + snippet := strings.Join(lines[interestingStart:endLine], "\n") + return stripComments(snippet), nil } // Last resort: return first minLines (skip imports if possible) @@ -201,7 +203,8 @@ func extractSnippetFromFile(filePath string, minLines, maxLines int) (string, er endLine = totalLines } - return strings.Join(lines[skipLines:endLine], "\n"), nil + snippet := strings.Join(lines[skipLines:endLine], "\n") + return stripComments(snippet), nil } // findSmallestCompleteFunction finds the smallest complete function in the file @@ -392,4 +395,73 @@ func findInterestingStart(lines []string, snippetSize int) int { // No interesting start found return -1 +} + +// stripComments removes comment lines from code snippets to make them more concise +func stripComments(code string) string { + lines := strings.Split(code, "\n") + var result []string + inMultilineComment := false + + for _, line := range lines { + trimmed := strings.TrimSpace(line) + + // Handle multi-line comments for C-style languages + if strings.Contains(line, "/*") { + inMultilineComment = true + // If comment ends on same line, process the rest + if strings.Contains(line, "*/") { + inMultilineComment = false + // Skip this line entirely if it's just a comment + if strings.TrimSpace(strings.Split(line, "/*")[0]) == "" { + continue + } + } else { + continue + } + } + + if inMultilineComment { + if strings.Contains(line, "*/") { + inMultilineComment = false + } + continue + } + + // Skip single-line comments + if trimmed == "" { + // Keep empty lines for readability + result = append(result, line) + } else if strings.HasPrefix(trimmed, "//") || + strings.HasPrefix(trimmed, "#") && !strings.HasPrefix(trimmed, "#include") && !strings.HasPrefix(trimmed, "#define") || + strings.HasPrefix(trimmed, "