refactor: convert TerminalColors to global var and split markdown utils

This commit is contained in:
Sahilb315
2025-05-05 19:28:13 +05:30
parent 3c803685ee
commit 5b809c7546
4 changed files with 59 additions and 32 deletions
+54
View File
@@ -0,0 +1,54 @@
package utils
import (
"regexp"
"strings"
)
var (
headerBulletRegex = regexp.MustCompile(`(?m)^(#{1,6}\s+|[-*]\s{1,}|\d+\.\s+|>\s+)`)
inlineCodeRegex = regexp.MustCompile("`{1,3}([^`]*)`{1,3}")
horizontalRuleRegex = regexp.MustCompile(`(?m)^\s*(-{3,}|\*{3,}|\_{3,})\s*$`)
boldItalicRegex = regexp.MustCompile(`(?:\*\*\*|___)(.*?)(?:\*\*\*|___)`)
boldRegex = regexp.MustCompile(`(?:\*\*|__)(.*?)(?:\*\*|__)`)
italicRegex = regexp.MustCompile(`(?:\*|_)(.*?)(?:\*|_)`)
strikethroughRegex = regexp.MustCompile(`~~([^~]+)~~`)
inlineLinkRegex = regexp.MustCompile(`\[([^\]]+)\]\((\S+?)\)`)
imageRegex = regexp.MustCompile(`!\[([^\]]*)\]\((\S+?)\)`)
extraSpacesRegex = regexp.MustCompile(`\s+`)
)
func removeMarkdown(text string) string {
// Remove bold italic (***bolditalic*** or ___bolditalic___)
text = boldItalicRegex.ReplaceAllString(text, "$1")
// Remove bold (**bold** or __bold__)
text = boldRegex.ReplaceAllString(text, "$1")
// Remove italic (*italic* or _italic_)
text = italicRegex.ReplaceAllString(text, "$1")
// Remove strikethrough (~~text~~)
text = strikethroughRegex.ReplaceAllString(text, "$1")
// Remove inline code (`code`)
text = inlineCodeRegex.ReplaceAllString(text, "$1")
// Remove links [text](url)
text = inlineLinkRegex.ReplaceAllString(text, "$1")
// Remove images ![alt](url)
text = imageRegex.ReplaceAllString(text, "$1")
// Remove horizontal rules
text = horizontalRuleRegex.ReplaceAllString(text, "")
// Remove headers, blockquotes, bullets (e.g., ### Heading, > Quote, - Item)
text = headerBulletRegex.ReplaceAllString(text, "")
// Normalize extra spaces
text = extraSpacesRegex.ReplaceAllString(text, " ")
// Trim leading/trailing whitespace
return strings.TrimSpace(text)
}
@@ -10,7 +10,6 @@ import (
)
func ConfirmInstallation(maliciousPkgs map[string]string) bool {
colors := NewTerminalColors()
fmt.Printf("\n%s\n", colors.Red("⚠️ WARNING: %d potentially malicious packages detected!", len(maliciousPkgs)))
fmt.Println(colors.Yellow("The following packages have been flagged:"))
+5 -8
View File
@@ -9,12 +9,9 @@ type TerminalColors struct {
Green func(format string, a ...interface{}) string
}
// NewTerminalColors initializes and returns TerminalColors
func NewTerminalColors() *TerminalColors {
return &TerminalColors{
Red: color.New(color.FgRed, color.Bold).SprintfFunc(),
Yellow: color.New(color.FgYellow).SprintfFunc(),
Cyan: color.New(color.FgCyan).SprintfFunc(),
Green: color.New(color.FgGreen).SprintfFunc(),
}
var colors = TerminalColors{
Red: color.New(color.FgRed, color.Bold).SprintfFunc(),
Yellow: color.New(color.FgYellow).SprintfFunc(),
Cyan: color.New(color.FgCyan).SprintfFunc(),
Green: color.New(color.FgGreen).SprintfFunc(),
}
-23
View File
@@ -2,7 +2,6 @@ package utils
import (
"fmt"
"regexp"
"strings"
)
@@ -66,25 +65,3 @@ func IsInstallCommand(pkgManager, cmd string) bool {
}
return false
}
func removeMarkdown(text string) string {
// 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)
}