test: add tests for removeMarkdown

This commit is contained in:
Sahilb315
2025-05-04 23:47:25 +05:30
parent 1bd0438146
commit 092176acd6
2 changed files with 62 additions and 7 deletions
+20 -7
View File
@@ -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)
}
+42
View File
@@ -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)
}
}
}