diff --git a/pkg/common/utils/utils.go b/pkg/common/utils/utils.go index f4aacdf..b51a895 100644 --- a/pkg/common/utils/utils.go +++ b/pkg/common/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "fmt" + "regexp" "strings" ) @@ -78,11 +79,23 @@ func IsInstallCommand(pkgManager, cmd string) bool { } func removeMarkdown(text string) string { - // Remove bold asterisks - text = strings.ReplaceAll(text, "**", "") - // Remove italic asterisks - text = strings.ReplaceAll(text, "*", "") - // Remove backticks - text = strings.ReplaceAll(text, "`", "") - return text + // Remove bold (**bold** or __bold__) + text = regexp.MustCompile(`\*\*(.*?)\*\*`).ReplaceAllString(text, "$1") + text = regexp.MustCompile(`__(.*?)__`).ReplaceAllString(text, "$1") + + // Remove italic (*italic* or _italic_) + text = regexp.MustCompile(`\*(.*?)\*`).ReplaceAllString(text, "$1") + text = regexp.MustCompile(`_(.*?)_`).ReplaceAllString(text, "$1") + + // Remove inline code (`code`) + text = regexp.MustCompile("`([^`]*)`").ReplaceAllString(text, "$1") + + // Remove links [text](url) + text = regexp.MustCompile(`\[(.*?)\]\(.*?\)`).ReplaceAllString(text, "$1") + + // Remove headings (e.g., ### Heading) + text = regexp.MustCompile(`(?m)^#{1,6}\s*`).ReplaceAllString(text, "") + + // Trim leading/trailing whitespace + return strings.TrimSpace(text) } diff --git a/pkg/common/utils/utils_test.go b/pkg/common/utils/utils_test.go index bb82cf7..c3e1048 100644 --- a/pkg/common/utils/utils_test.go +++ b/pkg/common/utils/utils_test.go @@ -168,3 +168,45 @@ func TestParsePackageInfo(t *testing.T) { }) } } + +func TestRemoveMarkdown(t *testing.T) { + tests := []struct { + input string + expected string + }{ + // Bold + {"This is **bold** text", "This is bold text"}, + {"This is __bold__ text", "This is bold text"}, + + // Italic + {"This is *italic* text", "This is italic text"}, + {"This is _italic_ text", "This is italic text"}, + + // Code + {"This is `code` inline", "This is code inline"}, + + // Link + {"Click [here](https://example.com)", "Click here"}, + + // Headings + {"# Heading 1", "Heading 1"}, + {"### Subheading", "Subheading"}, + + // Combined formatting + {"__*bold and italic*__", "bold and italic"}, + {"This is **bold** and `code`", "This is bold and code"}, + + // No markdown + {"Just plain text", "Just plain text"}, + + // Complex mixed + {"### Title\nSome **bold** text and a [link](http://url.com).", "Title\nSome bold text and a link."}, + } + + for _, tt := range tests { + result := removeMarkdown(tt.input) + if result != tt.expected { + t.Errorf("removeMarkdown(%q) = %q; want %q", tt.input, result, tt.expected) + } + } +}